简体   繁体   中英

using textIsSelectable in textView

I use android:textIsSelectable="true" for TextView.

To enabled copy/select/paste For TextView it is OK

Issue

When I open this activity, it scrolls to middle automatically, like this Link

Why did this problem happen?

This is my layout code.

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:bind="http://schemas.android.com/apk/res/android" >

    <data>
            <variable
                name="aboutMbti"
                type="mbtitest.kiars.me.mbti.model.AboutMbti"/>  
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardBackgroundColor="@android:color/white"
            app:cardCornerRadius="2dp"
            app:cardElevation="5dp" >

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:fitsSystemWindows="true"
                android:scrollbarSize="1dp"
                android:scrollbarThumbVertical="@android:color/white">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/img_splash_mtbi"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_margin="3dp"
                        android:adjustViewBounds="true"
                        android:padding="5dp"
                        android:scaleType="centerCrop"
                        bind:imageUrl="@{aboutMbti.imageUrl}"/>

                    <TextView
                        android:id="@+id/txt_content_about_mtbi"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/img_splash_mtbi"
                        android:layout_margin="5dp"
                        android:textIsSelectable="true"
                        android:padding="@dimen/title_padding"
                        android:text="@{aboutMbti.textContent}"
                        android:textColor="@color/ques_title"
                        android:textSize="15sp"
                        android:textStyle="bold"/>

                </RelativeLayout>

            </androidx.core.widget.NestedScrollView>

        </androidx.cardview.widget.CardView>

    </RelativeLayout>

</layout>

Solution

You can add either android:focusableInTouchMode="true" or android:descendantFocusability="blocksDescendants" to the child layout in NestedScrollView

Try below code in your layout:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...... >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true" >

    ...........

Finally, According to https://stackoverflow.com/users/6891563/khemraj comment, I Find

Mey solution ...

Just add android:focusableInTouchMode="true" to ImageView Like this :

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:bind="http://schemas.android.com/apk/res/android" >

    <data>
            <variable
                name="aboutMbti"
                type="mbtitest.kiars.me.mbti.model.AboutMbti"/>  
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardBackgroundColor="@android:color/white"
            app:cardCornerRadius="2dp"
            app:cardElevation="5dp" >

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:fitsSystemWindows="true"
                android:scrollbarSize="1dp"
                android:scrollbarThumbVertical="@android:color/white">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/img_splash_mtbi"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_margin="3dp"
                        android:adjustViewBounds="true"
                        android:padding="5dp"
android:focusableInTouchMode="true"
                        android:scaleType="centerCrop"
                        bind:imageUrl="@{aboutMbti.imageUrl}"/>

                    <TextView
                        android:id="@+id/txt_content_about_mtbi"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/img_splash_mtbi"
                        android:layout_margin="5dp"
                        android:textIsSelectable="true"
                        android:padding="@dimen/title_padding"
                        android:text="@{aboutMbti.textContent}"
                        android:textColor="@color/ques_title"
                        android:textSize="15sp"
                        android:textStyle="bold"/>

                </RelativeLayout>

            </androidx.core.widget.NestedScrollView>

        </androidx.cardview.widget.CardView>

    </RelativeLayout>

</layout>

Tnx Guys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM