简体   繁体   English

禁用滚动查看ScrollView中包含的ListView

[英]Disable scrolling of a ListView contained within a ScrollView

I want to show a Profile screen for my users. 我想为我的用户显示一个“个人资料”屏幕。

It must have three views (2 Buttons and a ImageView ) and a ListView to show the content made by that user. 它必须具有三个视图(2个Buttons和一个ImageView )以及一个ListView才能显示该用户制作的内容。

However, I don't want the ListView to scroll. 但是,我不希望ListView滚动。 Instead, I want it to be as big as needed, and to put all my views inside a ScrollView , so the three first views scroll out with the ListView . 相反,我希望它尽可能大,并将所有视图放入ScrollView ,以便使用ListView滚动出前三个视图。 This, of course, does not work as intended. 当然,这不能按预期工作。

All my three items are inside a LinearLayout . 我所有的三个项目都在LinearLayout I thought of making them the first item in the ListView , but this leads to them being selectable as the first item, and having to do some unneeded coding. 我想将它们设置为ListView的第一项,但这导致它们可以被选择为第一项,并且必须进行一些不必要的编码。

Is there a way to do this the easy way or will I have to stick with making the Layout the first item in my ListView? 有没有一种简单的方法可以执行此操作,还是我必须坚持将Layout设置为ListView中的第一项?

I found a very simple solution for this. 我为此找到了一个非常简单的解决方案。 Just get the adapter of the listview and calculate its size when all items are shown. 只需获取列表视图的适配器并在显示所有项目时计算其大小即可。 The advantage is that this solution also works inside a ScrollView. 优点是此解决方案也可以在ScrollView中使用。

Example: 例:

public static void justifyListViewHeightBasedOnChildren (ListView listView) {

    ListAdapter adapter = listView.getAdapter();

    if (adapter == null) {
        return;
    }
    ViewGroup vg = listView;
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, vg);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

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

Call this function passing over your ListView object: 通过您的ListView对象调用此函数:

justifyListViewHeightBasedOnChildren(myListview);

The function shown above is a modidication of a post in: Disable scrolling in listview 上面显示的功能是对以下内容的修改: 禁止在列表视图中滚动

Please note to call this function after you have set the adapter to the listview. 将适配器设置为列表视图后,请注意调用此功能。 If the size of entries in the adapter has changed, you need to call this function as well. 如果适配器中条目的大小已更改,则还需要调用此函数。

You can do this by 你可以这样做

listView.setScrollContainer(false);

for more please check 有关更多信息,请检查

How to get a non scrollable ListView? 如何获得不可滚动的ListView?

Adding them to the ListView as first Item seems like a pretty good solution. 将它们作为第一项添加到ListView似乎是一个很好的解决方案。

To make the View unselectable just get the view and .setClickable(false) . 要使View不可选择,只需获取view和.setClickable(false)

I would add a View with invisible background on top of ListView . 我会在ListView顶部添加一个具有不可见背景的View Set a View.OnTouchListener() to it. 设置一个View.OnTouchListener() And consume the event by returning true in onTouch() method of View.OnTouchListener() . 并通过返回消耗事件trueonTouch()的方法View.OnTouchListener()

When you want the list to be scrolling back again, remove the touch listener set on the transparent View 当您希望列表再次向后滚动时,请删除透明View上设置的触摸侦听器

if you have to show limited number of items in list view and want to stop list view from scrolling then you must keep listview height greater then the items total height. 如果必须在列表视图中显示有限数量的项目,并且要停止滚动列表视图,则必须使listview的高度大于项目的总高度。

for example you want to show 3 items. 例如,您要显示3个项目。 (height of row is 30). (行高为30)。 then items total height becomes 3 x 30dp = 90dp, 那么项目的总高度将变为3 x 30dp = 90dp,

so now you have to set listview height greater then 90. eg: 100dp. 所以现在您必须将listview的高度设置为大于90。例如:100dp。 so now your listview will not scroll in any case. 所以现在您的列表视图在任何情况下都不会滚动。

I think the best way would be to put the 2 buttons and the image view in a LinearLayout (or any layout that suits your need) and add this layout as the list header using the addHeaderView method: 我认为最好的方法是将2个按钮和图像视图放在LinearLayout(或任何适合您的布局)中,然后使用addHeaderView方法将此布局添加为列表标题:

http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View ) http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View

with the following instruction: 使用以下指令:

name_lista.getLayoutParams (). height = new_size

new_size is a variable that you will calculate according to the number of elements of your list, for example: new_size是一个变量,您将根据列表中元素的数量进行计算,例如:

new_size = 100 * list_size;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM