6

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

RelativeLayout.LayoutParams 参数 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, ListViewData.length());

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

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

4

4 回答 4

16

试试这个,使用基于列表视图的子级。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();
}



}
于 2013-10-01T13:20:52.547 回答
10

Your ListView is effectively unneeded in this case. You can as well loop over your adapter's items and just add them to a vertical LinearLayout inside your ScrollView.

In case you do not want to change a lot of code:

Replace ListView.setAdapter with

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

If you already use an OnItemClickListener add after View v = adapter.getView(position, null, null); the following

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

In this case you do not have to worry about any miscalculation in the height.

于 2013-10-01T13:23:03.983 回答
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()方法,因为发生了一些变化,使其布局无效 - 强制重绘。

于 2015-08-05T07:18:12.883 回答
0

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

于 2013-10-01T13:19:46.027 回答