1

我的目标是在我的 MainActivity 开始之前使用 ViewPager(如这里)让用户滑过 5 个不同的页面(一个教程)......

我已经从上面的链接下载了 Animations.zip,它几乎是我需要的,除了支持旧版本(我通过将 android.app 引用更改为 android.support.v4.app 来管理)和使用完全不同的布局而不是使用与示例中相同的视图...

我的代码编译并运行,但在手机屏幕上我看不到幻灯片的变化。操作栏出现,它的按钮工作正常,当我到达第一页/最后一页并尝试再次滑动时,我什至可以看到蓝色的光芒......我猜android出于某种原因正在生成默认布局。

在调试时,我注意到片段中的 onCreateView() 永远不会运行。知道为什么吗?我的代码如下:

como_usr_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView style="?android:textAppearanceMedium"
          android:id="@+id/texto_como_usar_fragment"
          android:padding="16dp"
          android:lineSpacingMultiplier="1.2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="aglaglalglaglalgalgalglag"
          android:textColor="@color/branco"
        />
<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
    />

ComoUsarFragment.java

public class ComoUsarFragment extends Fragment {
/**
 * The argument key for the page number this fragment represents.
 */
public static final String ARG_PAGE = "page";

/**
 * The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
 */
private int mPageNumber;

/**
 * Factory method for this fragment class. Constructs a new fragment for the given page number.
 */
public static Fragment create(int pageNumber) {
    Fragment fragment = new Fragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    return fragment;
}

public ComoUsarFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    //this is an attempt to set different views for each page, you'll see I dont use this piece of code yet
    int pagina;
    switch (mPageNumber){
        case 0: pagina = R.layout.como_usar0;
            break;

        case 1: pagina = R.layout.como_usar1;
            break;

        case 2: pagina = R.layout.como_usar2;
            break;

        default: pagina = R.layout.como_usar_fragment;
            break;
    }


    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.como_usar_fragment, container, false);

    // Set the title view to show the page number.
    ((TextView) rootView.findViewById(android.R.id.text1)).setText("aaaaa "+mPageNumber);

    return rootView;
}

/**
 * Returns the page number represented by this fragment object.
 */
public int getPageNumber() {
    return mPageNumber;
}

}

4

1 回答 1

2

您正在返回一个标准片段而不是您自己的子类

public static Fragment create(int pageNumber) {
    Fragment fragment = new Fragment();
    ...

应该

public static Fragment create(int pageNumber) {
    Fragment fragment = new ComoUsarFragment();
    ...
于 2013-10-16T13:49:52.663 回答