【问题标题】:android set listview height dynamicallyandroid动态设置listview高度
【发布时间】:2013-10-07 16:08:22
【问题描述】:

我在ScrollView 内有ExpandableListview,我知道这不好,但我也有,显示整个列表的唯一解决方案是使用layoutParams 通过代码设置其高度

RelativeLayout.LayoutParams 参数 = 新 相对布局.LayoutParams(LayoutParams.MATCH_PARENT, ListViewData.length());

这个解决方案很好,但我无法确定我应该在Params 中给出的正确高度,所以有没有办法从数组的大小中知道实际大小

编辑: 我想出了一个解决方案,每次我扩展一组列表时,我都会改变高度以适应新的身高

【问题讨论】:

  • 嘿,你会写你的解决方案吗?

标签: android listview height params


【解决方案1】:

您可以在尺寸文件中为不同的屏幕尺寸设置一个变量,然后将其乘以您要显示的列表项数。

【讨论】:

  • 我尝试使用固定变量,然后将其乘以项目数,但它在不同的屏幕尺寸下不起作用
  • @alaa7731 这就是我说的,你需要为不同的屏幕尺寸设置不同的值。
【解决方案2】:

试试这个,使用基于 listview 的 Child。 setListViewHeightBasedOnChildren() 这将设置您的基于列表视图子项的高度

 public class Utils {

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}



}

【讨论】:

  • mmmmmmmm 好的,我现在试试
  • 它正在工作,但您应该添加这些解决方案的来源。
  • @Murali Ganesan 我想将上面的代码用于gridview,替换什么代码来获得Grideview.Thanx的DividerHeight()
  • listView.requestLayout()listView.setLayoutParams(params) 被调用时已经被调用并且应该被移除。
  • 我必须说它的逻辑很清晰,而且大部分时间它都有效。但很少有它设置的高度远大于它应该设置的高度。不知道为什么,但这段代码在少数情况下有一些缺陷。
【解决方案3】:

在这种情况下实际上不需要您的 ListView。您也可以遍历适配器的项目,然后将它们添加到 ScrollView 内的垂直 LinearLayout。

如果您不想更改大量代码:

ListView.setAdapter 替换为

LinearLayout ll; //this should be the vertical LinearLayout that you substituted the listview with
for(int i=0;i<adapter.getCount();i++) {
    View v = adapter.getView(position, null, null);
    ll.addView(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}

如果您已经使用了 OnItemClickListener,请在 View v = adapter.getView(position, null, null); 之后添加以下内容

final int position = i;
v.setOnClickListener(new OnClickListener() {
    public onClick(View v) {
        yourOnItemClickListener.onItemClick(null, v, position, 0);
    }
});

在这种情况下,您不必担心高度计算错误。

【讨论】:

  • 我在 listView 的情况下想出了这个解决方案,但现在它会变成 ExpandableListView :/ ,所以在这种情况下它也必须动态完成
  • 我认为您应该为该编辑创建一个新问题。您特别要求提供列表视图。
  • 你好,来自黎巴嫩 (:
  • 这最终是更好的解决方案。破解ListView 并不是在每种情况下都可以使用行可能包含的各种元素,例如动态多行TextViews 或子ListViews
【解决方案4】:

试试这个,它适用于我同样的问题

public static boolean setListViewHeightBasedOnItems(ListView listView) {

ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {

    int numberOfItems = listAdapter.getCount();

    // Get total height of all items.
    int totalItemsHeight = 0;
    for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
        View item = listAdapter.getView(itemPos, null, listView);
        item.measure(0, 0);
        totalItemsHeight += item.getMeasuredHeight();
    }

    // Get total height of all item dividers.
    int totalDividersHeight = listView.getDividerHeight() * 
            (numberOfItems - 1);

    // Set list height.
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalItemsHeight + totalDividersHeight;
    listView.setLayoutParams(params);
    listView.requestLayout();

    return true;

} else {
    return false;
}}

requestLayout() 方法在视图上被调用,因为某些改变导致其布局无效 - 强制重绘。

【讨论】:

  • NPE 被抛出item.measure(0, 0);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 2015-03-16
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 2016-04-12
相关资源
最近更新 更多