有3個選項卡:ONETWO || TRI轉到其他片段後的數據列表重複(Sliding Tab)
例如: myList = 1,2,3
問題:
後我會去頁兩個 myList = 1,2,3,1,2,3
(雙副本)
如果我去頁面TRI myList = 1,2,3,1,2,3,1,2,3
(tri PLE副本)
後,我在尋找互聯網上的解決方案,我發現這個代碼(在適配器類):
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
請,建議我如何使清單數據不再複製後,bac從頁的兩個 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",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);
}
}
是發佈您的適配器和片段代碼 – chandil03
@ chandil03我已編輯我的代碼,請查看我的代碼。 –