android-recyclerview – 转到其他片段后重复数据列表(滑动标签)

前端之家收集整理的这篇文章主要介绍了android-recyclerview – 转到其他片段后重复数据列表(滑动标签)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
共有3个标签:ONE ||两个|| TRI

例如:
myList = 1,2,3

问题 :

在我去第二页之后
myList = 1,3,1,3(双重复制)

如果我去TRI页面
myList = 1,3(三重复)

在我在互联网上寻找解决方案后,我发现这个代码(在Adapter类上):

public void swap(List<FoodModel> datas){
        datas = new ArrayList<>();//updated
        if(mListFood !=null || mListFood.size() !=0){
            mListFood.clear();
            mListFood.addAll(datas);
        }else{
            mListFood = datas;
        }
        notifyDataSetChanged();
    }

我在oneFragment.java中使用了这样的:

mListFoodAdapter = new ListFoodAdapter(getContext(),mFoodModel);
        mListFoodAdapter.swap(mFoodModel);
        mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
        mRecyclerViewListFood.setAdapter(mListFoodAdapter);
        mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());

但它给了我nullPointer有时在RecyclerView中没有数据显示

拜托,建议我从TRI的第二页回来后,如何使列表数据不再重复

请看我的代码oneFragment.java和ViewPagerAdapter.java

oneFragment.java

public class FoodFragment extends Fragment {

    private RecyclerView mRecyclerViewListFood;
    private List<FoodModel> mFoodModel = new ArrayList<>();
    private ListFoodAdapter mListFoodAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_foods,container,false);
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        FragmentActivity fragmentActivity = getActivity();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(fragmentActivity);
        mRecyclerViewListFood = (RecyclerView) view.findViewById(R.id.recyclerView_list_foods);
        mFoodModel.add(new FoodModel("1",String.valueOf(R.drawable.icon),"test 1"));
        mFoodModel.add(new FoodModel("2","test 2"));

        mListFoodAdapter = new ListFoodAdapter(getContext(),mFoodModel);
        mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
        mRecyclerViewListFood.setAdapter(mListFoodAdapter);
        mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());



    }
}

ViewPagerAdapter.java:

public class ViewPagerDetailStandAdapter extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerDetailStandAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment,String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

解决方法

你在onViewCreated()方法中有一个问题.
每次创建片段视图后都会调用OnViewCreated()方法.但是片段如果在内存中则被重用.这就是你的mFoodModel列表没有重新初始化的原因.因此,您需要重新初始化mFoodModel以创建以前的数据.

所以这是你修改过的OnCreateView()`

@Override
public void onViewCreated(View view,Bundle savedInstanceState) {
    super.onViewCreated(view,savedInstanceState);
    FragmentActivity fragmentActivity = getActivity();
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(fragmentActivity);
    mRecyclerViewListFood = (RecyclerView) view.findViewById(R.id.recyclerView_list_foods);
    mFoodModel = new ArrayList<>(); // initialize you list
    mFoodModel.add(new FoodModel("1","test 1"));
    mFoodModel.add(new FoodModel("2","test 2"));

    mListFoodAdapter = new ListFoodAdapter(getContext(),mFoodModel);
    mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
    mRecyclerViewListFood.setAdapter(mListFoodAdapter);
    mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());
}
原文链接:/android/309808.html

猜你在找的Android相关文章