如何停止recyclerView android中的项目重复
Posted
技术标签:
【中文标题】如何停止recyclerView android中的项目重复【英文标题】:how to stop the items duplication in recyclerView android 【发布时间】:2017-05-09 09:04:23 【问题描述】:实际上我的问题是当我点击它转到DetailsFragment 的项目时。但是当我返回到我的 ItemListFragment 时,项目编号会增加并发生重复。请帮助我从中恢复..提前谢谢....
项目列表片段
public class ItemsListFragment extends Fragment
public static final String SINGLE_ITEMS = "single_items";
String[] mNames;
String[] mPrices;
int[] mImages;
Context mContext;
ArrayList<Data> mArrayList = new ArrayList<Data>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_items_list, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.items_list_recycler);
String allFields = getArguments().getString(CategoryFragment.ALL_FIELD);
assert allFields != null;
switch (allFields)
case "women_clothing":
mNames = getResources().getStringArray(R.array.women_clothing_names);
mPrices = getResources().getStringArray(R.array.women_clothing_prices);
mImages = DataValues.women_clothing_images;
break;
case "women_shoes":
mNames = getResources().getStringArray(R.array.women_shoes_names);
mPrices = getResources().getStringArray(R.array.women_shoes_prices);
mImages = DataValues.women_shoes_images;
break;
case "women_bags":
mNames = getResources().getStringArray(R.array.women_bags_names);
mPrices = getResources().getStringArray(R.array.women_bags_prices);
mImages = DataValues.women_bags_images;
break;
case "women_watches":
mNames = getResources().getStringArray(R.array.women_watches_names);
mPrices = getResources().getStringArray(R.array.women_watches_prices);
mImages = DataValues.women_watches_images;
break;
case "women_sunglasses":
mNames = getResources().getStringArray(R.array.women_sunglasses_names);
mPrices = getResources().getStringArray(R.array.women_sunglasses_prices);
mImages = DataValues.women_sunglasses_images;
break;
case "women_accessories":
mNames = getResources().getStringArray(R.array.women_accessories_names);
mPrices = getResources().getStringArray(R.array.women_accessories_prices);
mImages = DataValues.women_accessories_images;
break;
int i = 0;
for (String name : mNames)
Data data = new Data(name, mPrices[i], mImages[i]);
mArrayList.add(data);
i++;
ItemListGridAdapter adapter = new ItemListGridAdapter(getActivity(), mArrayList, allFields);
recyclerView.setAdapter(adapter);
RecyclerView.LayoutManager manager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
return view;
ItemListGridAdapter
public class ItemListGridAdapter extends RecyclerView.Adapter<ItemListGridAdapter.ItemListViewHolder>
public static final String POSITION = "position";
private Context mContext;
private ArrayList<Data> mArrayList = new ArrayList<Data>();
private String mAllFields;
public ItemListGridAdapter(Context context, ArrayList<Data> arrayList, String allfields)
mContext = context;
mArrayList = arrayList;
mAllFields = allfields;
@Override
public ItemListViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_itemlist, parent, false);
ItemListViewHolder viewHolder = new ItemListViewHolder(view);
return viewHolder;
@Override
public void onBindViewHolder(ItemListViewHolder holder, final int position)
Data data = mArrayList.get(position);
holder.mName.setText(data.getName());
holder.mPrice.setText(data.getPrice());
holder.mImage.setImageResource(data.getImageId());
final Bundle bundle = new Bundle();
holder.mRelativeLayout.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
switch (mAllFields)
case "women_clothing":
DetailsFragment fragment = new DetailsFragment();
bundle.putInt(POSITION, (position + 10));
fragment.setArguments(bundle);
FragmentManager fragmentManager = ((Activity) mContext).getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.home_main_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
case "women_shoes":
DetailsFragment fragment1 = new DetailsFragment();
bundle.putInt("position", (position + 10));
fragment1.setArguments(bundle);
FragmentManager fragmentManager1 = ((Activity) mContext).getFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.replace(R.id.home_main_frame, fragment1);
fragmentTransaction1.addToBackStack(null);
fragmentTransaction1.commit();
break;
case "women_bags":
DetailsFragment fragment2 = new DetailsFragment();
bundle.putInt("position", (position + 10));
fragment2.setArguments(bundle);
FragmentManager fragmentManager2 = ((Activity) mContext).getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
fragmentTransaction2.replace(R.id.home_main_frame, fragment2);
fragmentTransaction2.addToBackStack(null);
fragmentTransaction2.commit();
break;
case "women_watches":
DetailsFragment fragment3 = new DetailsFragment();
bundle.putInt("position", (position + 10));
fragment3.setArguments(bundle);
FragmentManager fragmentManager3 = ((Activity) mContext).getFragmentManager();
FragmentTransaction fragmentTransaction3 = fragmentManager3.beginTransaction();
fragmentTransaction3.replace(R.id.home_main_frame, fragment3);
fragmentTransaction3.addToBackStack(null);
fragmentTransaction3.commit();
break;
case "women_sunglasses":
DetailsFragment fragment4 = new DetailsFragment();
bundle.putInt("position", (position + 10));
fragment4.setArguments(bundle);
FragmentManager fragmentManager4 = ((Activity) mContext).getFragmentManager();
FragmentTransaction fragmentTransaction4 = fragmentManager4.beginTransaction();
fragmentTransaction4.replace(R.id.home_main_frame, fragment4);
fragmentTransaction4.addToBackStack(null);
fragmentTransaction4.commit();
break;
case "women_accessories":
DetailsFragment fragment5 = new DetailsFragment();
bundle.putInt("position", (position + 10));
fragment5.setArguments(bundle);
FragmentManager fragmentManager5 = ((Activity) mContext).getFragmentManager();
FragmentTransaction fragmentTransaction5 = fragmentManager5.beginTransaction();
fragmentTransaction5.replace(R.id.home_main_frame, fragment5);
fragmentTransaction5.addToBackStack(null);
fragmentTransaction5.commit();
break;
);
@Override
public int getItemCount()
return mArrayList.size();
public class ItemListViewHolder extends RecyclerView.ViewHolder
private TextView mName;
private TextView mPrice;
private ImageView mImage;
private RelativeLayout mRelativeLayout;
public ItemListViewHolder(View itemView)
super(itemView);
mName = (TextView) itemView.findViewById(R.id.item_name_text);
mPrice = (TextView) itemView.findViewById(R.id.item_price_text);
mImage = (ImageView) itemView.findViewById(R.id.item_image);
mRelativeLayout = (RelativeLayout) itemView.findViewById(R.id.item_click);
DetailsFragment
public class DetailsFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_details, container, false);
TextView mTitle = (TextView) view.findViewById(R.id.item_details_title);
TextView mPrice = (TextView) view.findViewById(R.id.item_details_price);
ImageView mImage = (ImageView) view.findViewById(R.id.item_details_image);
TextView mDescription = (TextView) view.findViewById(R.id.item_details_description);
int position = getArguments().getInt(ItemListGridAdapter.POSITION);
Toast.makeText(getActivity(), ""+position, Toast.LENGTH_SHORT).show();
return view;
【问题讨论】:
【参考方案1】:更多帮助:
bundle.putInt("position", position);
bundle.putString("text", your_text_field);
...
fragment.setArguments(bundle);
...
【讨论】:
【参考方案2】:这样做
int i = 0;
mArrayList.clear(); //Clear your array list before adding new data in it
for (String name : mNames)
Data data = new Data(name, mPrices[i], mImages[i]);
mArrayList.add(data);
i++;
通过这样做,您的数据将不再重复。 试试看,让我知道它是否适合你。
【讨论】:
请提供更多帮助 请提供更多帮助 DetailsFragment 片段 = 新的 DetailsFragment(); bundle.putInt(位置,(位置));片段.setArguments(捆绑); FragmentManager fragmentManager = ((Activity) mContext).getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.home_main_frame, 片段); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); 我们可以传递两个 bundle 一个是 int 另一个是 string ,那有可能吗? @Alwin 不,您不能通过 2 个捆绑包。但是您可以在一个包中传递 2 个键值。通过bundle.putString(..)
以上是关于如何停止recyclerView android中的项目重复的主要内容,如果未能解决你的问题,请参考以下文章
如何从 ScrollView 内的 RecyclerView 中移除焦点?
如何在android的Recyclerview中显示更多[重复]
平滑滚动不适用于 Android RecyclerView 的初始滚动