Guillermo Barreiro
Guillermo Barreiro

Reputation: 1405

Highlight selected item on a RecyclerView

My app consists on two fragments (let's call them A and B). Fragment A shows a RecyclerView and Fragment B the content of the selected item on that Recycler. On small and normal screens each Fragment is shown on its Activity, but on large screens both fragments show at the same time, so I would like that in the multi-pane activity when the user clicks on an item from the RecyclerView it gets highlighted. I've found lots of questions like this on StackOverflow but they don't work with my adapter.

final FragmentCallback activity;
final FragmentA fragment; //The fragment with the RecyclerView

Cursor cursor;
int idColumnIndex;
int titleColumnIndex;
int balanceColumnIndex;

public MoneyboxesAdapter(FragmentCallback activity, FragmentA frag){
    this.activity = activity;
    this.fragment = frag;
}

public void swapCursor(Cursor c){
    // Method called whenever the data stored on the DB has changed
    this.cursor = c; //Updates the current cursor
    if(cursor!=null){
        cursor.moveToFirst();
        idColumnIndex = cursor.getColumnIndex(MoneyboxProvider.COLUMN_MONEYBOX_ID);
        titleColumnIndex = cursor.getColumnIndex(MoneyboxProvider.COLUMN_MONEYBOX_TITLE);
        balanceColumnIndex = cursor.getColumnIndex(MoneyboxProvider.COLUMN_MONEYBOX_BALANCE);
    }
    notifyDataSetChanged();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    CardView v = (CardView) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.moneybox_item, viewGroup, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {

    // Sets up the views
    cursor.moveToPosition(i);
    final long id = getItemId(i);
    final String moneyboxTitle = cursor.getString(titleColumnIndex);
    final float balance = cursor.getFloat(balanceColumnIndex);
    viewHolder.title.setText(moneyboxTitle);
    viewHolder.balance.setText(balance);


    viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity.onLoadMoneybox(id);
            //TODO: Show selected on multipane
        }

    });


}

@Override
public int getItemCount() {
    return cursor!=null?cursor.getCount():0;
}

public long getItemId(int position){
    // Gets the ID in the DB of a moneybox
    cursor.moveToPosition(position);
    return cursor.getLong(idColumnIndex);
}


static class ViewHolder extends RecyclerView.ViewHolder{

    CardView cardView;
    TextView title;
    TextView balance;
    ImageView threeDots;

    public ViewHolder(CardView itemView) {
        super(itemView);
        cardView = itemView;
        title = (TextView) itemView.findViewById(R.id.moneybox_item_title);
        balance = (TextView) itemView.findViewById(R.id.moneybox_item_balance);
        threeDots = (ImageView)itemView.findViewById(R.id.three_dots_moneybox);
    }


}

Upvotes: 2

Views: 2143

Answers (2)

karthik
karthik

Reputation: 139

  • You can do like below snippet .

            class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {

                public ViewHolder(View view) {
                    // your code
                    view.setOnClickListener(this)
                }

                @Override
                public void onClick(View v) {
                    int position = getPosition();
                    // your changes for changing color
                    notifyDataSetChanged();
                }
            }

Upvotes: 1

Arun Mehra
Arun Mehra

Reputation: 559

create a selector for the list item and set the view.setSelected(true); for the specific position that has been selected and keep a track of the last selected view which was selected and set that to false if the user clicks on a new row item.

Upvotes: 1

Related Questions