“android:textIsSelectable=”true" not working for TextView in RecyclerView

前端 未结 7 1036
甜味超标
甜味超标 2021-01-01 09:31

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 i

7条回答
  •  一整个雨季
    2021-01-01 10:05

    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

    
    
        
    
        
        
    
    

提交回复
热议问题