tm1701
tm1701

Reputation: 7581

Setting 'textIsSelectable' on (larger) Textview makes RecyclerView scroll to the start of the TextView

The app has a screen with a RecyclerView of larger TextViews. Each Textview can contain 2 - 100 lines. Yes, for the app this is a good solution.

The user has a number of options on the TextView: singleTap, doubleTap, etc. The user can e.g. select a 'textSelection' option. Then the following code is executed:

aTextview.setOnTouchListener(null); // preventing the singletap, double tap action, works fine
aTextview.setFocusable(true);
aTextview.setLongClickable(true);
aTextview.setTextIsSelectable(true);

The latter statement will make the RecyclerView scroll to the start/top of the current textview. Suppose the user wants to select words at line 55 of the current textview, when starting to select text, the recyclerview will scroll the current textview line 0 at the top of the screen. That is really annoying because then the user will have to search the text (at line 55) again for selection (and copy, paste, etc).

When I remove the 'setTextIsSelectable' statement, there is no scrolling.

How can I prevent 'setTextIsSelectable' to start scrolling to the start of the textview?

UPDATE: A solution is to use recyclerView.setLayoutFrozen( true); ... and unfreeze the recyclerView afterwards. The disadvantage of this solution is that the textSelection cannot go beyond the screen, because there is no scrolling ;-)

Upvotes: 0

Views: 340

Answers (1)

tm1701
tm1701

Reputation: 7581

Solution 1: move the focus first to another (!) item. Then do the aTextview.setTextIsSelectable(true). Why - changing the contents will do a re-layout and thus move to the top.

How? focusDistractorView.requestFocus();

An example of such a dummy-focus-distractor is:

<LinearLayout
    android:id="@+id/move_focus_here_editable"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="0px"
    android:layout_height="0px"
    android:orientation="vertical"/>

You could restore the focus after your action.

Solution 2 is to use recyclerView.setLayoutFrozen( true); ... and unfreeze the recyclerView afterwards. The disadvantage of this solution is that the textSelection cannot go beyond the screen, because there is no scrolling.

Upvotes: 1

Related Questions