gpt4 book ai didi

android - FragmentPagerAdapter 错误地更新了页面?

转载 作者:行者123 更新时间:2023-11-30 03:17:02 26 4
gpt4 key购买 nike

我在 FragmentStatePagerAdapter 中遇到奇怪的问题。当我向前滑动时,它工作正常,当我向后滑动时它跳过 2 个 fragment 。如何解决这个问题?有什么办法可以得到当前商品编号吗?

NavigationPagerAdapter

     public static class NavigationPagerAdapter extends FragmentStatePagerAdapter {

public NavigationPagerAdapter(android.support.v4.app.FragmentManager fm ) {
super(fm);

}

@Override
public Fragment getItem(int i) {

Fragment fragment = new NaviagtionFragment();

Bundle args = new Bundle();




args.putInt("position", i); // Our object is just an integer :-P
fragment.setArguments(args);
return fragment;
}

@Override
public int getCount() {
// For this contrived example, we have a 100-object collection.
return 100;
}

@Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position );
}

在 fragment 中

         public static class NaviagtionFragment extends Fragment {




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_pager, container, false);



Bundle args = getArguments();
int m= args.getInt("position");


Toast.makeText(c, ""+m, Toast.LENGTH_SHORT).show();//here i tracked the position of fragments when i swipe front ,its increasing 1,2,3,4,5,6,7 but when i swipe back it will go directly 7 -5 th position..


}}}

最佳答案

Toast 非常慢,当你滑动 View 时它不是一个好的解决方案,因为它在屏幕上停留的时间更长并且无法显示所有消息,所以最好检查 LogCat 中的位置。试试这个项目的位置,并用日志查看位置:

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("position") : 1;
Log.i("tag", "Item clicked: " + mNum);
}

关于android - FragmentPagerAdapter 错误地更新了页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19891427/

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