Recyclerview动态textview高度

时间:2019-07-07 20:04:29

标签: java android android-recyclerview textview android-linearlayout

我想动态更改textviews的高度以适合其他linearlayout中的单元格。

我试图通过这种方式做到这一点:

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

我有很好的结果,但是我的问题是我必须向下滚动并向上滚动列表才能“加载”这个高度,我不知道该如何告诉我,所以我向您展示了屏幕截图。

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>

1 个答案:

答案 0 :(得分:0)

创建另一个名为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);

现在像这样,您不必担心单元格的高度。

相关问题