Java 如何使TextView的最大行数(如果更多)可更改

Java 如何使TextView的最大行数(如果更多)可更改,java,android,xml,kotlin,Java,Android,Xml,Kotlin,我有recyclerView,每个itemView都包含以下内容: <?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:too

我有recyclerView,每个itemView都包含以下内容:

<?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>
java格式的相同文件:

            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;
            }
        });
问题是,如果TextView只包含1或2行文本,则该TextView中有大量空白。我想这样做:当TextView中有1-5行文本时,其高度仅为“wrap_content” 而且它是不可滚动的。 但当有6行以上的文本时,它只能容纳5行,用户需要滚动该文本视图才能看到其他行

我该怎么做? 当前RecyclerView的屏幕截图:

您不应该将滚动视图包含在您的
ViewHolder
s中。这将导致不可预测的行为,最终用户在尝试滚动列表时会感到沮丧,最终会滚动您的
视图持有者的内容。更好的匹配方法是实现可扩展的
视图夹
s,在折叠状态下,它将显示例如3行文本,并且所有文本都处于展开状态

            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;
            }
        });