1

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

5 Answers5

5

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!!!

sumit
  • 1,012
  • 1
  • 8
  • 14
  • My recycler view is a multiselect recycler view...ie)when i long press the current view item get selected....only now the text view is vissible...I tried your code its deselecting the item due to the GestureDetector ..What to do – ANITHA SUNDARAMURTHY Jul 19 '17 at 09:40
  • do you have any function on single tap of recyclerview row?? – sumit Jul 19 '17 at 09:44
  • Ya..i have a funtion..it opens another activity – ANITHA SUNDARAMURTHY Jul 19 '17 at 09:48
  • I have just tested the functionality it's detecting both the clicks on the particular textView and the complete recyclerView row. Can you share your code where you have implemented the functionality?? – sumit Jul 19 '17 at 09:52
  • ok let me know if I understood your problem correctly. what you want is on singletap on recylerView row you're going to a new activity but if you long press on the row a textview will be visible and on tap of that textview you want to have some other function not the normal switching of the activity. – sumit Jul 19 '17 at 10:02
  • no..not exactly..when i longpress on the item,the item gets highlighted and mentee ,mentor textview gets vissible ...now i want to click on mentee or mentor to do some corresponding function – ANITHA SUNDARAMURTHY Jul 19 '17 at 10:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149578/discussion-between-sumit-and-anitha-sundaramurthy). – sumit Jul 19 '17 at 10:08
0

Implement View.OnClickListener on the textView in the recycler adapter.

Paras Watts
  • 2,105
  • 3
  • 14
  • 41
0

You can use adapter and from adapter you can give click listener for particular component(i.e., TextView) .

  • My recycler view is a multiselect recycler view...ie)when i long press the current view item get selected....only now the text view is vissible...I tried your code its deselecting the item due to the GestureDetector ..What to do – ANITHA SUNDARAMURTHY Jul 19 '17 at 09:40
0

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

        }
    }
0

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

Nishan Khadka
  • 331
  • 2
  • 12