0

When I select an item from recyclerView on the right side, I need to show that object's information on the left side. I was stuck at onClick in recyclerViewAdapter class. I created a fragment but I couldn't manage it. Here is the structure that I was trying to do: enter image description here

I need to put the selected item's information in this XML file and show it on the left side.

public class ProductRwAdapter extends RecyclerView.Adapter<ProductRwAdapter.ProductHolder> {

    ArrayList<Product> productArrayList;

    public ProductRwAdapter(ArrayList<Product> productArrayList) {
        this.productArrayList = productArrayList;
    }

    @NonNull
    @Override
    public ProductHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerRowBinding recyclerRowBinding = RecyclerRowBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);

        return new ProductHolder(recyclerRowBinding);
    }

    @Override
    public int getItemCount() {
        return productArrayList.size();
    }

    @Override
    public void onBindViewHolder(@NonNull ProductRwAdapter.ProductHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.recyclerRowBinding.productName.setText(productArrayList.get(position).getName());
        holder.recyclerRowBinding.productAmount.setText(Integer.toString(productArrayList.get(position).getAmount()));
        Picasso.get().load(productArrayList.get(position).getImageUri()).into(holder.recyclerRowBinding.productImage);
        holder.recyclerRowBinding.textViewCardTotal.setText(Long.toString(productArrayList.get(position).getAmount() * productArrayList.get(position).getPrice()) + " TL ");

        
        holder.recyclerRowBinding.imageViewClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                productArrayList.remove(position);
                notifyDataSetChanged();


            }
        });

        
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(holder.itemView.getContext(), ""+position, Toast.LENGTH_SHORT).show();

                AppCompatActivity activity = (AppCompatActivity) view.getContext();
                Fragment myFragment = new fragmentInfo();
                activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, myFragment).addToBackStack(null).commit();



            }
        });
        holder.recyclerRowBinding.buttonIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               int productAmountInt = Integer.parseInt(holder.recyclerRowBinding.productAmount.getText().toString());
               productAmountInt++;
               String produtAmountString = Integer.toString(productAmountInt);
               productArrayList.get(position).setAmount(productAmountInt);
               holder.recyclerRowBinding.productAmount.setText(produtAmountString);
               holder.recyclerRowBinding.textViewCardTotal.setText(Long.toString(productArrayList.get(position).getAmount() * productArrayList.get(position).getPrice()) + " TL ");
            }
        });
        holder.recyclerRowBinding.buttonDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int productAmountInt = productArrayList.get(position).getAmount();

                //if amount of product is greater than 1 (2 or more) decrease it otherwise don't do anything
                if(productAmountInt > 1){
                    productAmountInt--;
                    String produtAmountString = Integer.toString(productAmountInt);
                    productArrayList.get(position).setAmount(productAmountInt);
                    holder.recyclerRowBinding.productAmount.setText(produtAmountString);
                    holder.recyclerRowBinding.textViewCardTotal.setText(Long.toString(productArrayList.get(position).getAmount() * productArrayList.get(position).getPrice()) + " TL ");

                }

            }
        });



    }

    public class ProductHolder extends RecyclerView.ViewHolder {

       private RecyclerRowBinding recyclerRowBinding;

        public ProductHolder(RecyclerRowBinding  recyclerRowBinding) {
            super(recyclerRowBinding.getRoot());

            this.recyclerRowBinding = recyclerRowBinding;
        }
    }

}
LW001
  • 2,452
  • 6
  • 27
  • 36
Metehan
  • 21
  • 2

1 Answers1

0

You can use ViewModel to passing data between two fragments. Create a Viewmodel and observe it in left side fragment, when item selected set value in ViewModel and you can show informations of item selected.

Sharing data between fragments using new architecture component ViewModel

aref behboodi
  • 164
  • 2
  • 11