【问题标题】:Non-scrollable ListView inside ScrollView with different size items具有不同大小项目的 ScrollView 内的不可滚动 ListView
【发布时间】:2014-04-16 06:52:16
【问题描述】:

具有不同大小项目的 ScrollView 内不可滚动的 ListView。实际上,我尝试获取列表项的数量并给出高度=项目数量*第一项的高度,但问题是我在列表中有不同高度的项目,所以最后一项有时不可见。 这是我的自定义列表视图:

public class ExpandedListView  extends ListView {

    private android.view.ViewGroup.LayoutParams params;
    private int old_count = 0;
    int limit=0;
    public ExpandedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getCount() != old_count) {
            old_count = getCount();
            params = getLayoutParams();
            int height=0;

            int h =getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);

            int minHeight=setListViewHeightBasedOnChildren(this);
            if (h>minHeight) {
                params.height=h+8 * (getCount() - 1)+25;
                setLayoutParams(params);
            }

            super.onDraw(canvas);
        }
    }

    public static int setListViewHeightBasedOnChildren(ListView listView) {

        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return 0;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; 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))+100;

        listView.setLayoutParams(params);
        return params.height;
    }

}

【问题讨论】:

  • 向我们展示你的努力。贴一些代码。
  • 这段代码似乎没问题。尝试将Margin bottom 设置为您的列表视图并检查
  • 问题是如果我有 20 dp 的第一项和 80 dp 和 60 dp 的第一项,则列表将需要 60 dp 高度(20 * 3),但它应该需要 20+80+60=160dp。我该怎么办???
  • listItem.getMeasuredHeight(); 将获得单个项目的高度,无论它是什么。它将添加高度并更新!不是通过在高度上再增加 100 dp 来工作吗?
  • 我们只能通过 getChildAt(1).getHeight() 或在其他位置给 null 来获取第一项的高度。如果我在适配器中使用它,它会返回 0。

标签: android listview


【解决方案1】:

我不明白你的问题正是你想要的。

如果您想让 listview 以每个 listview 项的相同高度和宽度进行滚动,请使用 inflater.make 单行并使用 inflater 在数组中使用它。

如果您的问题是这样,那么我将向您展示每个列表视图项的高度和宽度相同的列表视图代码。

【讨论】:

  • 我不想让列表视图可滚动,因为由于某些要求,列表视图被放置在滚动视图中。我希望列表视图在没有任何滚动的情况下完全展开。但是我有不同高度的不同物品。
猜你喜欢
  • 2013-09-19
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
相关资源
最近更新 更多