Java 如何使我的列表视图行具有不同的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18236008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:36:49  来源:igfitidea点击:

How can I make my listview rows different heights

javaandroidlistview

提问by code511788465541441

Right now if there is a list item that is 2 lines in height then all the rows are two lines in height

现在,如果有一个高度为 2 行的列表项,那么所有行的高度都是两行

How can I make the rows' height independent on other rows.

如何使行的高度独立于其他行。

ListView

列表显示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/user_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null" />

</LinearLayout>

Row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="1dp"
    android:paddingTop="1dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:maxLines="2"
        android:ellipsize="middle"
        android:textColor="@android:color/holo_blue_dark"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/points"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:gravity="right"
        android:textStyle="bold" />

</LinearLayout>

I am using a custom adapter extending BaseAdapter. the getView there is nothing special, just a viewholder patern I don't change height or anything

我正在使用扩展 BaseAdapter 的自定义适配器。getView 没有什么特别的,只是一个视图模式我不改变高度或任何东西

回答by Roman Mazur

Looks like you'll have to make visibility of your text views to be View.GONEwhen they are empty. You can do it in adapter implementation.

看起来您必须在文本视图为View.GONE空时使其可见。您可以在适配器实现中做到这一点。

pointsView.setText(pointsText);
pointsView.setVisibility(TextUtils.isEmpty(pointsText) ? View.GONE : View.VISIBLE);

But be careful since your views are in RelativeLayoutand its rules can stop working when an anchored view gets visibility View.GONE.

但是要小心,因为RelativeLayout当锚定视图可见时,您的视图已经存在并且它的规则可能会停止工作View.GONE

In such case you may try setting text size to zero instead of changing the view visibility, but I haven't tried it myself.

在这种情况下,您可以尝试将文本大小设置为零而不是更改视图可见性,但我自己还没有尝试过。

pointsView.setTextSize(TextUtils.isEmpty(pointsText) ? 0 : defaultSize);

回答by Ahmed Ekri

why don't you try making the height = 0dp and weight 1

你为什么不尝试使高度 = 0dp 和重量 1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="1dp"
    android:paddingTop="1dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:maxLines="2"
        android:ellipsize="middle"
        android:textColor="@android:color/holo_blue_dark"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/points"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:gravity="right"
        android:textStyle="bold" />

</LinearLayout>

update:

更新:

check this answer

检查这个答案

https://stackoverflow.com/a/7513423/1932105

https://stackoverflow.com/a/7513423/1932105

enter image description here

在此处输入图片说明

回答by sriramramani

You are restricting it the TextViewto have a maximum of 2 lines. It will never grow more than that. Remove android:maxLines="2". It should work automatically.

您将其限制TextView为最多 2 行。它永远不会增长更多。删除android:maxLines="2"。它应该自动工作。

回答by But I'm Not A Wrapper Class

The trick for me was not setting the height -- but instead setting the minHeight. This must be applied to the root view of whatever layout your custom adapter is using to render each row.

对我来说,诀窍不是设置高度——而是设置 minHeight。这必须应用于您的自定义适配器用于呈现每一行的任何布局的根视图。

Source: https://stackoverflow.com/a/7368439/2498729

来源:https: //stackoverflow.com/a/7368439/2498729

android:textAppearance="?android:attr/textAppearanceLarge" 

seemed no effect.

似乎没有效果。

android:minHeight="?android:attr/listPreferredItemHeight" 

changed the height for me

为我改变了高度

Source: https://stackoverflow.com/a/4274505/2498729

来源:https: //stackoverflow.com/a/4274505/2498729

回答by Yup

If you are not limited with the TextView MaxLine then please remove android:maxLines="2"this will automatically adjust the height of list.
If you are looking for increasing the height of one particular row independent of others. After setting the adapter value for the list. Get the list
final ListView lv = getListView();then form the lvget the particular row by
lv.getItemIdAtPosition(position)or any similar logic that best suited in your code. and change the row appearance programmatically as you looking for. Thanks.

如果您不受 TextView MaxLine 的限制,那么请删除android:maxLines="2"它会自动调整列表的高度。
如果您正在寻找独立于其他行的特定行的高度。为列表设置适配器值后。获取列表
final ListView lv = getListView();然后形成lv获取特定行
lv.getItemIdAtPosition(position)或最适合您的代码的任何类似逻辑。并在您查找时以编程方式更改行外观。谢谢。