skip to Main Content

I have attached an image of my recyclerview, in my onClick method I show or hide a linearlayout with some image buttons however I want to hide linearlayout when I click on another recyclerview item, basically the linearlayout with the buttons should only show in one item. Can anyone guide me?

below see how it is now, which I do not want to happen.

[![enter image description here][1]][1]

public class AdapterExpensesRecyclerView extends RecyclerView.Adapter<AdapterExpensesRecyclerView.ExpenseViewHolder> {

private final RecyclerViewInterface recyclerViewInterface;
List<Expense> expenseList;

public AdapterExpensesRecyclerView(List<Expense> expenseList, RecyclerViewInterface recyclerViewInterface) {
    this.expenseList = expenseList;
    this.recyclerViewInterface = recyclerViewInterface;

}

@NonNull
@Override
public ExpenseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.retrieve_layout, parent, false);
    ExpenseViewHolder holder = new ExpenseViewHolder(v, recyclerViewInterface);

    return holder;

}

@Override
public void onBindViewHolder(@NonNull ExpenseViewHolder holder, int position) {

    Expense expense = expenseList.get(position);

    holder.item.setText("" + expense.getItem());
    holder.category.setText("" + expense.getCategory());
    holder.date.setText("" + expense.getDate());
    holder.amount.setText("$ " + formatNumberCurrency(String.valueOf(expense.getAmount())));

}

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

public void filterList(ArrayList<Expense> filteredList) {
    expenseList = filteredList;
    notifyDataSetChanged();
}

public static class ExpenseViewHolder extends RecyclerView.ViewHolder {

    TextView item;
    TextView category;
    TextView date;
    TextView amount;

    LinearLayout single_item_options;
    private Boolean gone_selection = true;

    public ExpenseViewHolder(@NonNull View itemView, RecyclerViewInterface recyclerViewInterface) {
        super(itemView);

        item = itemView.findViewById(R.id.list_item_name);
        category = itemView.findViewById(R.id.list_item_category);
        date = itemView.findViewById(R.id.list_item_date);
        amount = itemView.findViewById(R.id.list_item_amount);

        //recyclerView single options
        single_item_options = itemView.findViewById(R.id.single_item_options);
        ImageView edit_btn = itemView.findViewById(R.id.edit_btn);
        ImageView duplicate_btn = itemView.findViewById(R.id.duplicate_btn);
        ImageView delete_btn = itemView.findViewById(R.id.delete_btn);

        single_item_options.setVisibility(View.GONE);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (recyclerViewInterface != null) {
                    int pos = getAdapterPosition();

                    if (pos != RecyclerView.NO_POSITION) {
                        recyclerViewInterface.onItemClick(pos);

                        if (gone_selection) {
                            single_item_options.setVisibility(View.VISIBLE);
                            gone_selection = false;

                        } else {
                            single_item_options.setVisibility(View.GONE);
                            gone_selection = true;
                        }
                    }
                }
            }
        });

        edit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (recyclerViewInterface != null) {
                    int pos = getAdapterPosition();

                    if (pos != RecyclerView.NO_POSITION) {
                        recyclerViewInterface.onEdit(pos);
                    }
                }
                single_item_options.setVisibility(View.GONE);
                gone_selection = true;
            }
        });

        duplicate_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (recyclerViewInterface != null) {
                    int pos = getAdapterPosition();

                    if (pos != RecyclerView.NO_POSITION) {
                        recyclerViewInterface.onDuplicate(pos);
                    }
                }
                single_item_options.setVisibility(View.GONE);
                gone_selection = true;
            }
        });

        delete_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (recyclerViewInterface != null) {
                    int pos = getAdapterPosition();

                    if (pos != RecyclerView.NO_POSITION) {
                        recyclerViewInterface.onDelete(pos);
                    }
                }
                gone_selection = true;
            }
        });
    }
}

3

Answers


  1. Try this approach.
    Step 1: Get the view position
    Step 2: Compare the clicked view position if its is the same as the position you get earlier
    Step 3: perform the logic based on the positions

    Login or Signup to reply.
  2. You’re interested in two ViewHolders (one of both which may not be visible at any given time): the current ViewHolder which shows the the linear layout; and the new ViewHolder which you want to show the linear layout.

    Call RecyclerView.findViewHolderForItemId (long id) or RecyclerView.findViewHolderForAdapterPostion() for each of the respective items. Once you have them, call appropiate custom methods on your ViewHolder class to show or hide the linear layout for the bound ViewHolder. If either of the findViewHolderForItemId calls return null, your item isn’t visible, so there’s no change to be made. Just make sure that you show or hide the LinearLayout appropriately when the focused item does become visible, in your ViewHolder.bind() method.

    Login or Signup to reply.
  3. You can do it with the following steps.

    declare one variable like ‘visiblePosition’ in adapter class and assign the value of position in OnClick

    then in onBindViewHolder check the below condition

    if(visiblePosition == position){
       //Show You Layout
    }else{
       //Hide You Layout
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search