gpt4 book ai didi

android - PagerAdapter 类被多次调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:00:05 27 4
gpt4 key购买 nike

我正在开发一个包含 ViewPager View 的应用程序, 我创建了一个 PagerAdapter,它具有 View ,PagerAdapter 的 instantiateItem() 方法在 create() 中被调用了两次 我不知道为什么,有人可以帮我解决这个问题吗?

这是我的代码,

         View PagerView;
MyPagerAdapter adapter;
ViewPager pager;

adapter = new MyPagerAdapter();
pager.setAdapter(adapter);
pager.setCurrentItem(0);

public class MyPagerAdapter extends PagerAdapter {

@Override
public Object instantiateItem(final View collection, final int position) {
Log.d("Inside", "Pager");
PagerView = new View(collection.getContext());
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PagerView = inflater.inflate(R.layout.tablemenu, null, false);
tbMenuDetails = (TableLayout) PagerView
.findViewById(R.id.Menutable1);
scrollview = (ScrollView) PagerView.findViewById(R.id.scrollView1);
tbMenuDetails.removeAllViews();
removeTableRows();
createTableLayout(position);
String str[][] = datasource.GetSubMenuDetailsFromMenuId(MenuIdlst
.get(position).trim());
Log.d("Str", "" + str.length);
for (int i = 0; i < str.length; i++) {
addRows(str[i][1], str[i][2], str[i][0], str[i][3], position);
Log.d("Message", "Pos " + position + " SubMenuName" + str[i][2]
+ " SubMenuId" + " " + str[i][0] + " TypeID" + " "
+ str[i][3]);
}

// View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(PagerView, 0);

return PagerView;
}

@Override
public void destroyItem(final View arg0, final int arg1,
final Object arg2) {
((ViewPager) arg0).removeView((View) arg2);

}

@Override
public boolean isViewFromObject(final View arg0, final Object arg1) {
return arg0 == ((View) arg1);

}

@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub

}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub

}

@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}

@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return MenuIdlst.size();
}

}

请帮忙

最佳答案

如果您决定使用 Fragment,请不要实现 PagerAdapter。相反,扩展 FragmentPagerAdapterFragmentStatePagerAdapter:

private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public int getCount() {
return NUMBER_OF_PAGES_IN_PAGER;
}

@Override
public Fragment getItem(int position) {
// Implement the static method newInstance in MyFragment.java yourself.
// It should return you a brand new instance of MyFragment, basically using
// the code you had in your original instantiateItem method.
return MyFragment.newInstance(position, ... etc ...);
}
}

然后在您的 Activity 中:

myPagerAdapter = new MyPagerAdapter(getFragmentManager());
// or myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
myViewPager.setAdapter(myPagerAdapter);

要获得有关如何使用 Fragments 和 ViewPagers 的更多信息: https://developer.android.com/reference/android/app/Fragment.html https://developer.android.com/reference/android/support/v4/view/ViewPager.html https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

关于android - PagerAdapter 类被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14892991/

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