Recyclerview 动态文本视图高度

Recyclerview dynamic textview height

我想动态更改文本视图的高度以适合其他线性布局中的单元格。

我试过这样做:

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {



         holder.numberCage.setText(cages.get(position).getNumberCage());
         holder.eggsDescription.setText(cages.get(position).getEggsDescription());
         holder.eggsFrom.setText(cages.get(position).getNumberCageFrom());

         if(cages.get(position).getBasicEggs().toString().equals("true"))
         holder.basicEggs.setText("Tak");
         else
             holder.basicEggs.setText("Nie");

         holder.dateEggs.setText(cages.get(position).getDateEggs());

         holder.description.setText(cages.get(position).getDescription());
         holder.pigeonsInside.setText(cages.get(position).getPigeonsInside());


         holder.numberCageTitle.setHeight(holder.numberCage.getHeight());
         holder.eggsDescriptionTitle.setHeight(holder.eggsDescription.getHeight());
         holder.eggsFromTitle.setHeight(holder.eggsFrom.getHeight());
         holder.basicEggsTitle.setHeight(holder.basicEggs.getHeight());
         holder.dateEggsTitle.setHeight(holder.dateEggs.getHeight());
         holder.descriptionTitle.setHeight(holder.description.getHeight());
         holder.pigeonsInsideTitle.setHeight(holder.pigeonsInside.getHeight());
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
         TextView numberCage,pigeonsInside,description, dateEggs, basicEggs, eggsFrom, eggsDescription, numberCageTitle, pigeonsInsideTitle, descriptionTitle, dateEggsTitle, basicEggsTitle, eggsFromTitle, eggsDescriptionTitle;
         LinearLayout linearLayout, linearLayoutLeft, linearLayoutRight;
         public MyViewHolder(View view){
             super(view);
             numberCage = view.findViewById(R.id.numberCage);
             pigeonsInside = view.findViewById(R.id.pigeonsInside);
             description = view.findViewById(R.id.description);
             dateEggs = view.findViewById(R.id.dateEggs);
             basicEggs = view.findViewById(R.id.basicEggs);
             eggsFrom = view.findViewById(R.id.eggsFrom);
             eggsDescription = view.findViewById(R.id.eggsDescription);


             numberCageTitle = view.findViewById(R.id.numberCageTitle);
             pigeonsInsideTitle = view.findViewById(R.id.pigeonsInsideTitle);
             descriptionTitle = view.findViewById(R.id.descriptionTitle);
             dateEggsTitle = view.findViewById(R.id.dateEggsTitle);
             basicEggsTitle = view.findViewById(R.id.basicEggsTitle);
             eggsFromTitle = view.findViewById(R.id.eggsFromTitle);
             eggsDescriptionTitle = view.findViewById(R.id.eggsDescriptionTitle);


            linearLayout = view.findViewById(R.id.linearLayout);
         }
    }

我有很好的结果,但我的问题是我必须向下滚动和向上滚动我的列表到 "load" 这个高度,我不知道怎么说所以我给你看截图。

left table is not showing 向下滚动后:left table after scroll

这是我的内容xml:

<LinearLayout 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"
    android:layout_height="wrap_content"
    android:baselineAligned="true"
    android:weightSum="2"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".CageList"
    tools:showIn="@layout/activity_cage_list"
    android:divider="@android:color/black"
    android:dividerPadding="1dip"
    android:showDividers="end"
    android:id="@+id/linearLayout"
    >

    <LinearLayout
        android:id="@+id/linearLayoutLeft"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.25"
        android:orientation="vertical">

        <TextView
            android:id="@+id/numberCageTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Numer klatki:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/pigeonsInsideTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Gołębie w klatce:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/descriptionTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Opis klatki:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/dateEggsTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Data złożenia jaj:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/basicEggsTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Jajka macierzyste?"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/eggsFromTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Jajka przełożone skąd? (klatka)"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/eggsDescriptionTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Opis do jajek:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
        <View
            android:layout_width="200dp"
            android:layout_height="1dp"
            android:background="@color/colorPrimary"
        />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayoutRight"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.75"
        android:orientation="vertical">

        <TextView
            android:id="@+id/numberCage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/pigeonsInside"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/dateEggs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/basicEggs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/eggsFrom"
            android:layout_width="match_parent"
            android:layout_height="35.65dp" />

        <TextView
            android:id="@+id/eggsDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorPrimary"/>
    </LinearLayout>

</LinearLayout>

创建另一个名为 sub_layout.xml 的文件,其中包含以下代码:-

 <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/txtTitleStatic"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.45"
            android:text="Jajka przełożone skąd? (klatka)"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".55" />
    </LinearLayout>

然后使用以下代码包含在主布局中(您的内容 XML 文件):-

<include
        android:id="@+id/numberCageLayout"
        layout="@layout/sub_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


<include
        android:id="@+id/pigeonsInsideLayout"
        layout="@layout/sub_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

<include
        android:id="@+id/descriptionLayout"
        layout="@layout/sub_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

现在在您的 Java 文件中获取它们的定义:-

View numberCageLayout =  findViewById(R.id.numberCageLayout);
View pigeonsInsideLayout =  findViewById(R.id.pigeonsInsideLayout);
View descriptionLayout =  findViewById(R.id.descriptionLayout);

TextView numberCageStatic =numberCageLayout.findViewById(R.id.txtTitleStatic);
TextView numberCage =numberCageLayout.findViewById(R.id.txtTitle);


TextView pigeonsInsideStatic =pigeonsInsideLayout.findViewById(R.id.txtTitleStatic);
TextView pigeonsInside =pigeonsInsideLayout.findViewById(R.id.txtTitle);

这样你就不用担心单元格的高度了。