如何使列表视图在不滚动的情况下显示所有项目? [英] How to make a listview display all items without scrolliing?

查看:77
本文介绍了如何使列表视图在不滚动的情况下显示所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道将ListView放入ScrollView并不是推荐的方法.但是我必须使用ListView,仅仅是因为我正在使用适配器和其他各种方法.

I understand that putting a ListView inside a ScrollView is not recommmender. But I must use a ListView, simply because I'm using an adapter and various other methods.

我使用了这段代码,该代码是我在StackOverflow上找到的,它向我显示了清单的大部分内容.但是,在纵向模式下,某些项目从底部被切除,在横向模式下相同.

I have used this code, which I found on StackOverflow and it shows me the majority of my list. However, in portrait mode some items are cut off from the bottom and in landscape mode the same.

该如何纠正?为了澄清,我有一个ScrollView,它包含一个包含一些元素的LinearLayout和XML中的ListView. 此外,如果我要进行新的活动,那么返回后,我可以短暂地看到列表视图的滚动条显示并消失.怎么也可以纠正?

How can this be rectified? To clarify, I've got a ScrollView containing a LinearLayout containing some elements and my ListView in my XML. In addition, if I go to a new activity, then return I can briefly see the scroll bars for listview show up and disappear. How can this be rectified too?

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

    }

推荐答案

我认为您的问题有点误导.只要我们正在处理这些小巧的移动屏幕,listview怎么能不滚动地显示其所有项目?

I think your question is a bit misleading. How on earth can a listview displays its all items without scrolling as long as we are dealing with these tiny mobile screens?

也许您不希望显示scroll bar,然后可以在ScrollView上这样设置attribute:

Maybe you want that scroll bar to not show up, then you can set attribute like this on your ScrollView:

android:scrollbars="none"

这篇关于如何使列表视图在不滚动的情况下显示所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆