Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textIsSelectable() has missing items in "floating text selection toolbar" for TextViews in recyclerView

I have one TextView (sub_text) on the main fragment, and another RecyclerView (rv) containing TextView items.

I get different number of items when i try to select text in RecyclerView. It is missing the selections from other apps that implements ACTION_PROCESS_TEXT.

My selected items are only partial - unlike other questions on stackoverflow ( e.g. "android:textIsSelectable="true" not working for TextView in RecyclerView), where the selection is totally missing or not working.

How do I make it such that the items in the rv's textView are the same as the fragment's textView?

on my TextView, I get the following floating text selection toolbar

enter image description here

However when i try to select text from within the Recycler view, I get the following floating text selection toolbar

enter image description here

Notice that there's only 2 selectable items within the RV?

the xml for the sub_text is

<TextView
    android:id="@+id/sub_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" 
    android:textSize="16sp" 
    android:padding="8dp"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:textColor="@color/primary"
    android:textIsSelectable="true"
    android:visibility="gone" />

the xml for the rv is

    <android.support.v7.widget.RecyclerView 
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:scrollbars="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

The xml for the textview within the rv is

 <TextView 
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:text="经" 
        android:textColor="@color/primary_dark"
        android:textIsSelectable="true" />
like image 578
Angel Koh Avatar asked Oct 17 '22 23:10

Angel Koh


1 Answers

Text View need to be enabled, focusable, longClickable and textIsSelectable

    <TextView
                android:id="@+id/textViewHeading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:enabled="true"
                android:textIsSelectable="true"
                android:focusable="true"
                android:longClickable="true" />

use bellow recycler view code

    <android.support.v7.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/recyclerViewRecordList"
        app:layout_constraintTop_toBottomOf="@+id/relativeLayoutHeader"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/view_bottom"
        android:layout_margin="10dp"
        android:focusable="true"
        android:focusableInTouchMode="true">
    </android.support.v7.widget.RecyclerView>

use SpeedyLinearLayoutManager in code instead of xml

SpeedyLinearLayoutManager linearLayoutManager = new SpeedyLinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myRecyclerViewList.setLayoutManager(linearLayoutManager);
RecordAdapter myCustomerAdapter = new RecordAdapter(this, myRecordListData);
myRecyclerViewList.setAdapter(myCustomerAdapter);


public class SpeedyLinearLayoutManager extends LinearLayoutManager {

        private static final float MILLISECONDS_PER_INCH = 2f; //default is 25f (bigger = slower)

        public SpeedyLinearLayoutManager(Context context) {
            super(context);
        }

        public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }

        public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

            final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
                }

                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
                }
            };

            linearSmoothScroller.setTargetPosition(position);
            startSmoothScroll(linearSmoothScroller);
        }
    }
like image 115
jessica Avatar answered Oct 21 '22 02:10

jessica