“android:textIsSelectable=”true" not working for T

2019-03-17 20:09发布

I know that setting android:textIsSelectable="true" in xml for the TextView will show the native text selection popup and I've been using that in my application. But what I found that it is not working any more when I try to set the same attribute in a view attached to the RecyclerView. Whenever I try to select the text the following log appears -

TextView: TextView does not support text selection. Action mode cancelled.

And I don't know why? Why it works on other screens and not with the RecyclerView. I read multiple posts -

TextView with android:textIsSelectable="true" not working in listview

textview textIsSelectable="true" not working in Listview

android:textIsSelectable="true" for TextView inside Listview does not work

But then I encountered this post -

Android: "TextView does not support text selection. Action mode cancelled"

And the reply by @hungkk worked for me. His solution suggested the TextView width to change to wrap_content from match_parent.

I know I can do this but my question is how this fixed the issue because it looks weird to me. And also, what is the solution if I want to keep the width to match_parent.

Any inputs are welcome.

4条回答
来,给爷笑一个
2楼-- · 2019-03-17 20:44

In the main-parent layout of recyclerview add attribute

android:descendantFocusability="beforeDescendants"

and then in TextView of rowitem layout add

android:textIsSelectable="true"
查看更多
来,给爷笑一个
3楼-- · 2019-03-17 20:45

If you add android:descendantFocusability="blocksDescendants"​ in the recyclerview or listview, then remove it. And after check this

查看更多
够拽才男人
4楼-- · 2019-03-17 20:47

Add In Your RecyclerView Adapter:

public ViewHolder(View itemView) {
            super(itemView);
            txtDate = (TextView) itemView.findViewById(R.id.txtDate);
            txtDate.setTextIsSelectable(true);
}

its worked for me..

查看更多
放我归山
5楼-- · 2019-03-17 20:53

There seems to be many that have problems with this and indications that it may be a bug in the Android code but I don't have a problem. This is what works for me both for an OnClickListener() and the native selection popup. (Tested on KitKat 4.4, Lollipop 5.1 and Nougat 7.1)

In the adapter

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView textView;
    ImageView imageView;

    MyViewHolder(View itemView) {
        super(itemView);
        textView = (TextView) itemView.findViewById(R.id.my_text_view);
        imageView = (ImageView) itemView.findViewById(R.id.my_image_view);

        itemView.setOnClickListener(this);
        textView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // this shows 'my_text_view' when the text is clicked or 
        //     'my_item' if elsewhere is clicked
        Log.d(TAG, "view = " + view.toString());
        switch (view.getId()) {
            case R.id.my_item:
                break;
            case R.id.my_text_view:
                break;
        }
    }
}

And my item layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_item"
    >

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@color/colorPrimary"
        android:id="@+id/my_image_view"
        />

    <!-- this works for me with either "match_parent" or "wrap_content" for width -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:text="My text view"
        android:textIsSelectable="true"
        android:id="@+id/my_text_view"
        />
</LinearLayout>
查看更多
登录 后发表回答