I am developing app for my collage project. I started building app for online food ordering app . So I created a layout with the recyclerview that shows the items. Now I stuck to add that Items to the cart layout . So can anybody help me how to add those items and which methods should use in the both the layouts. And the use case is when ever the quantity(increment) button is pressed in the ItemsfFragment class that layout should add to the cart.
I have attached the image of itemsFragments class layout below .
I have attached the image of itemsFragments class layout below .
Java:
public class ItemsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycler_view, container, false);
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView picture ;
public TextView name;
public TextView price;
public TextView quantity;
public int mQuantity = 0;
public ViewHolder(View itemView, TextView name, TextView price, int mQuantity) {
super(itemView);
this.name = name;
this.price = price;
this.mQuantity = mQuantity;
}
public TextView getName() {
return name;
}
public TextView getPrice() {
return price;
}
public int getmQuantity() {
return mQuantity;
}
public void setName(TextView name) {
this.name = name;
}
public void setPrice(TextView price) {
this.price = price;
}
public void setmQuantity(int mQuantity) {
this.mQuantity = mQuantity;
}
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item, parent, false));
picture = (ImageView) itemView.findViewById(R.id.item_image);
name= (TextView) itemView.findViewById(R.id.item_name);
price = (TextView) itemView.findViewById(R.id.item_price);
quantity =(TextView)itemView.findViewById(R.id.quantity);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_POSITION, getAdapterPosition());
context.startActivity(intent);
}
});
ImageButton addImageButton =
(ImageButton) itemView.findViewById(R.id.add_button);
addImageButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (mQuantity == 5) {
return;
}
quantity.setText(String.valueOf(++mQuantity));
}
});
ImageButton minusImageButton = (ImageButton) itemView.findViewById(R.id.minus_button);
minusImageButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (mQuantity == 0) {
return;
}
quantity.setText(String.valueOf(--mQuantity));
}
});
}
}
/**
* Adapter to display recycler view.
*/
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of Card in RecyclerView.
private static final int LENGTH = 11;
private final String[] mNames;
private final String[] mPrice;
private final Drawable[] mItemPics;
public ContentAdapter(Context context) {
Resources resources = context.getResources();
mNames = resources.getStringArray(R.array.ItemNames);
mPrice = resources.getStringArray(R.array.ItemPrice);
TypedArray a = resources.obtainTypedArray(R.array.item_picture);
mItemPics = new Drawable[a.length()];
for (int i = 0; i < mItemPics.length; i++) {
mItemPics[i] = a.getDrawable(i);
}
a.recycle();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.picture.setImageDrawable(mItemPics[position % mItemPics.length]);
holder.name.setText(mNames[position % mNames.length]);
holder.price.setText(mPrice[position % mPrice.length]);
}
@Override
public int getItemCount() {
return LENGTH;
}
}
}