转到其他片段后,数据列表重复(滑动选项卡)

时间:2016-01-16 10:13:40

标签: android-recyclerview pagerslidingtabstrip

共有3个标签: ONE || 两个 ||的 TRI

示例: myList = 1,2,3

问题:

在我转到页面 TWO 之后 myList = 1,2,3,1,2,3(双重复制)

如果我转到页面 TRI myList = 1,2,3,1,2,3,1,2,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 页面 TWO 返回后,如何使列表数据不再重复

我的代码oneFragment.javaViewPagerAdapter.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",String.valueOf(R.drawable.icon), "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);
    }
}

2 个答案:

答案 0 :(得分:4)

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",String.valueOf(R.drawable.icon), "test 1"));
    mFoodModel.add(new FoodModel("2",String.valueOf(R.drawable.icon), "test 2"));

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

答案 1 :(得分:2)

只需使用此方法清除片段数据。

@Override
public void onDestroyView() {
    super.onDestroyView();
    tutPojo.clear();
}