简体   繁体   中英

Recycler view Click Listener

I'm working in the application of Android approached to finish it but I found the problem in recyclerView, and i didn't know how to implement a click listener on the textview of an Item of the recyclerView ? (when i click in the item)

I have a multiselect recyclerview...only when i select the recyclerview item,the textview becomes vissible

Here use this:

My recyclerView row has 2 textViews :

 public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView name,price;

        public MyViewHolder(View view){
            super(view);
            name = (TextView) view.findViewById(R.id.name);
            price= (TextView) view.findViewById(R.id.price);

            name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("Test","Name clicked : "+getAdapterPosition());
                }
            });


        }
    }

from this position you can get the value of particular item using your dataList.

Hope it helps!!!

在回收器适配器的textView上实现View.OnClickListener。

您可以使用适配器和适配器,您可以为特定组件(即TextView)提供单击侦听器。

this might help you....

 public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{


        TextView textview;


        public MyViewHolder(View itemView) {
            super(itemView);
                itemView.setOnClickListener(this);

                textview = (TextView) itemView.findViewById(R.id.textview);


        }

        @Override
        public void onClick(View view) {
           textview = (TextView) itemView.findViewById(R.id.textview);
             //do your actions here

        }
    }

Okay I think I have solution to your problem:

First of all , in your RecyclerView's item layout , make the parent layout clickable and your TextView's visibility as gone:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:layout_gravity="center"
    android:gravity="center"
    >
<TextView
  android:id="@+id/text_title"
  android:layout_width="match_parent"
  android:layout_height="50dp"/>
</LinearLayout>

Making your parent layout clickable is necessary, and declare this LinearLayout in your ViewHolder class as well like this:

class ViewHolder extends RecyclerView.ViewHolder {
 private LinearLayout itemLayout;
 private TextView textItem;
 ViewHolder(final View itemView) {
 super(itemView);
 itemLayout=(LinearLayout) itemView.findViewById(R.id.movie_item);
 textItem=(TextView)itemView.findViewById(R.id.text_title);
}

Then instead of itemView.setOnClickListener, use itemLayout.setOnClickListener() as below:

itemlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
      //make your textView or your items Visible 
      //make your textview clickable
      //set Your itemLayoutClickable as false

    }
            });

Then finally, make onClickListener for your TextView:

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

//perform your text click action
}
});

For making the TextView Invisible again, you might wanna add some extra logic, such as setting flag , checking flag and making the items visible or invisible and notifying the adapter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM