Actually my problem is when I click the item it go to a DetailsFragment. But when I return back to my ItemListFragment the items number is increased and duplication is occurred. Please help me to recover from it.. Thanks in advance....
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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…