gpt4 book ai didi

android - onCreateView() 从不运行(尝试将 ViewPager 用于屏幕幻灯片)

转载 作者:行者123 更新时间:2023-11-29 21:31:21 24 4
gpt4 key购买 nike

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

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

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

在调试时,我注意到 fragment 中的 onCreateView() 从未运行。知道为什么吗?我的代码如下:

como_usar_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;
}

最佳答案

您返回的是标准 fragment 而不是您自己的子类

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

应该是

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

关于android - onCreateView() 从不运行(尝试将 ViewPager 用于屏幕幻灯片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19405200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com