2016-04-22 189 views
20

我知道在的xml中設置android:textIsSelectable="true"會顯示原生文本選擇彈出窗口,並且我一直在我的應用程序中使用它。它不工作任何更多的時候我嘗試設置在連接到RecyclerView 每當我儘量選擇出現以下日誌文​​本視圖中的相同屬性 -「android:textIsSelectable =」true「對於RecyclerView中的TextView不起作用

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

而且我不知道爲什麼?爲什麼它在其他屏幕上工作,而不是與RecyclerView。我看了多個職位 -

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

但後來我遇到了這個帖子 -

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

而且通過@hungkk回覆爲我工作。他的解決方案建議TextView寬度從match_parent更改爲wrap_content

我知道我可以做到這一點,但我的問題是如何解決這個問題,因爲它看起來很奇怪。此外,如果我想將寬度保持爲match_parent,解決方案是什麼。

歡迎任何輸入。

+1

什麼是你該選擇做什麼? (Marty或Shadab)。對於'match_parent'或'wrap_content',使用'View.OnClickListener()'我沒有任何問題。 – Gary99

+0

Wierd但我讀過幾篇文章,當回收者的視圖重用單元格時,如果TextView設置爲match_parent,則禁用可選文本功能。您是否已嘗試將android:inputType =「textMultiLine」而不是'android:textIsSelectable =「true」'設置爲解決方法? – fmaccaroni

回答

2

如果在recyclerview或listview中添加android:descendantFocusability =「blocksDescendants」,請將其刪除。 並在檢查後

0

似乎有很多與此有關的問題和跡象表明,它可能是Android代碼中的錯誤,但我沒有問題。這對我而言既適用於OnClickListener(),也適用於本地選擇彈出窗口。 (測試奇巧4.4,棒棒糖5.1和牛軋糖7.1)

在適配器

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; 
     } 
    } 
} 

我的項目佈局

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

在recyclerview的主父佈局添加屬性

android:descendantFocusability="beforeDescendants"

然後在TextView的rowitem佈局中添加

android:textIsSelectable="true" 
0

添加在您的RecyclerView適配器:

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

它的工作對我來說..

相關問題