简体   繁体   中英

How to make TextView maximum number of lines - if more - scrolable

I have recyclerView, each itemView contain this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    app:cardCornerRadius="8dp"
    android:layout_marginBottom="4dp"
    android:layout_height="wrap_content">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_weight="10"
            android:layout_height="wrap_content">

        <View
                android:id="@+id/divider"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="?android:attr/listDivider"/>

        <ScrollView android:layout_width="match_parent"
                    android:layout_height="100sp">
            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/answer"/>

        </ScrollView>
    </LinearLayout>

</android.support.v7.widget.CardView>

By using this kotlin file I made TextViews to be scrollable inside of RecyclerView (textView are scrolable only if they do not fit to 100sp).

            itemView.answer.setOnTouchListener{v,event ->
            // Disallow the touch request for parent scroll on touch of child view
            v.parent.requestDisallowInterceptTouchEvent(true)
            false
        }

Same file in java format:

            itemView.answer.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // Disallow the touch request for parent scroll on touch of child view
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

The problem is that, then TextView contains 1 or 2 lines of text only, there are a lot of empty space inside of that TextView. I want to do something like this: when there are 1-5 line of text inside of TextView its height is just "wrap_content" and it is not scrollable. But when there are 6 of more lines of text, it fits 5 lines only, to see other lines user needs to scroll that TextView.

How can I do this? Screenshot of current RecyclerView: 在此处输入图片说明

You should never include scrolling views inside of your ViewHolder s. It will lead to unpredictable behavior and end users will be frustrated when they'll try to scroll the list and they end up scrolling contents of your ViewHolder . A match better way would be implementing expandable ViewHolder s which at collapsed state would show eg 3 lines of text, and all of them in the expanded state.

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