Home > database >  how to handle click event for each item in recyclerview child in nested recyclerview
how to handle click event for each item in recyclerview child in nested recyclerview

Time:05-11

I have a nested recyclerview to show the list of foods in each category. When the user clicks the increase or decrease button, the quantity will be updated to the quantity textview and synced to the cart stored in the database, but currently I don't know how to listen for the event when the user clicks these buttons.

Here is my current UI when run: enter image description here

Here is my fragment:

public class TabOrderFragment extends Fragment {

    private static final String ARG_RESTAURANT_ID = "restaurantId";
    private int restaurantId;

    View view;
    RecyclerView menuRecyclerView;
    RecyclerView.LayoutManager layoutManager;

    ArrayList<Menu> menuArrayList;
    MenuAdapter menuAdapter;

    public TabOrderFragment() { }

    public static TabOrderFragment newInstance(int restaurantId) {
        TabOrderFragment fragment = new TabOrderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_RESTAURANT_ID, restaurantId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            restaurantId = getArguments().getInt(ARG_RESTAURANT_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tab_order, container, false);

        if(restaurantId != 0) {
            DatabaseHandler databaseHandler = DatabaseHandler.getInstance(getActivity());
            menuArrayList = new ArrayList<Menu>();
            menuArrayList = databaseHandler.getAllMenuFoodsByIdRestaurant(restaurantId);
            menuRecyclerView = view.findViewById(R.id.rcv_foods_menu);
            menuAdapter = new MenuAdapter(getActivity().getApplicationContext(), restaurantId);
            layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
            menuRecyclerView.setLayoutManager(layoutManager);
            menuAdapter.setData(menuArrayList);
            menuRecyclerView.setAdapter(menuAdapter);
        }

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d("TabOrderFragment", "Restaurant Id = "   restaurantId);
    }

Here is my MenuAdapter:

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuViewHolder> {
    Context context;
    private int restaurantId;

    private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
    ArrayList<Menu> menuList;

    public MenuAdapter(ArrayList<Menu> menuList) {
        this.menuList = menuList;
    }

    public MenuAdapter(Context context, int restaurantId) {
        this.context = context;
        this.restaurantId = restaurantId;
    }

    public void setData(ArrayList<Menu> menuList) {
        this.menuList = menuList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_foods_menu, parent, false);
        return new MenuViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MenuViewHolder holder, int position) {
        Menu menu = menuList.get(position);
        holder.txtMenuTitle.setText(menu.getTitle().concat(" ("   String.valueOf(menu.getQuantity())   ")"));

        LinearLayoutManager layoutManager = new LinearLayoutManager(holder.rcvListFoods.getContext(), LinearLayoutManager.VERTICAL, false);
        layoutManager.setInitialPrefetchItemCount(menu.getFoods().size());
        FoodAdapter foodAdapter = new FoodAdapter(menu.getFoods(), restaurantId);

        holder.rcvListFoods.setLayoutManager(layoutManager);
        holder.rcvListFoods.setAdapter(foodAdapter);
        holder.rcvListFoods.setRecycledViewPool(viewPool);
    }

    @Override
    public int getItemCount() {
        if(menuList != null) {
            return menuList.size();
        }
        return 0;
    }

    public class MenuViewHolder extends RecyclerView.ViewHolder {

        private TextView txtMenuTitle;
        private RecyclerView rcvListFoods;

        public MenuViewHolder(@NonNull View itemView) {
            super(itemView);
            txtMenuTitle = itemView.findViewById(R.id.txv_menu_title);
            rcvListFoods = itemView.findViewById(R.id.rcv_list_foods);
        }
    }
}

Here is my FoodAdapter:

public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder> {

    private ArrayList<Food> foodList;
    private int restaurantId;
    private int userId;

    SharedPreferences sharedPreferences;

    public FoodAdapter(ArrayList<Food> foodList, int restaurantId) {
        this.foodList = foodList;
        this.restaurantId = restaurantId;
    }

    @NonNull
    @Override
    public FoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        sharedPreferences = fragmentActivity.getSharedPreferences("currentUser", Context.MODE_PRIVATE);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_food, parent, false);
        return new FoodViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FoodViewHolder holder, int position) {
        Food food =foodList.get(position);
        Picasso.get().load(food.getThumbImage()).into(holder.foodThumbImage);
        holder.foodName.setText(food.getName());
        holder.foodDescription.setText(food.getDescription());
        double price = Double.parseDouble(food.getPrice());
        DecimalFormat formatter = new DecimalFormat("#,###");
        holder.foodPrice.setText(formatter.format(price)   "đ");

        

    }

    @Override
    public int getItemCount() {
        if(foodList != null) {
            return foodList.size();
        }
        return 0;
    }

    public class FoodViewHolder extends RecyclerView.ViewHolder {

        private ImageView foodThumbImage;
        private TextView foodName;
        private TextView foodDescription;
        private TextView foodPrice;
        private TextView quantity;
        private MaterialButton increaseBtn;
        private MaterialButton decreaseBtn;

        private ItemClickListener itemClickListener;

        public FoodViewHolder(@NonNull View itemView) {
            super(itemView);
            foodThumbImage = itemView.findViewById(R.id.image_food);
            foodName = itemView.findViewById(R.id.txv_food_name);
            foodDescription = itemView.findViewById(R.id.txv_food_description);
            foodPrice = itemView.findViewById(R.id.txv_food_price);
            quantity = itemView.findViewById(R.id.txv_quantity);
            increaseBtn = itemView.findViewById(R.id.btn_increase);
            decreaseBtn = itemView.findViewById(R.id.btn_decrease);
        }


    }

CodePudding user response:

in your food adapter's onBindViewHolder method use

holder.increaseBtn.setOnClickListener{
    onIncreaseClick()
}

while create an interface like

interface Listener{
    public void onIncreaseClick();
    public void onDecreaseClick();
}

and implement your interface in your activity or fragment or viewmodel where you can listen for the click event

CodePudding user response:

What you can do generally when you have something like that, is to create an interface for the interaction with the Recycler view:

interface Interaction {
    public void onFoodClick(Food food)
}

Then you add an instance of this interface in your recycler view adapter, and you let your activity or fragment implement this interface, please find a good explanation here: https://stackoverflow.com/a/31671289/7334951

  • Related