使用公共滚动将两个列表视图彼此合并 [英] Merging two listviews one above another with a common scroll

查看:66
本文介绍了使用公共滚动将两个列表视图彼此合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个应用程序,在该应用程序中,我必须考虑两个ListView,一个在另一个之上,并且还要使用一个普通的滚动条.我知道这不是最佳实践,相反,我应该只使用一个ListView,但是问题是单ListView似乎无法实现布局.实际上,我的第一个ListView将包含我可以接受或忽略的朋友请求的列表,这些请求不是静态的并且可以无限制地变化,第二个列表将包含我的朋友的变化,也可能是变化的,即不是静态的.如果我只需要显示这两件事,那么我应该首选单个ListView并且可以轻松完成,但是当我必须对第二个列表(即使用边栏的朋友列表)按字母顺序排序时,实际的问题就出现了,这是适用的仅用于我的第二个列表,而不是第一个.这是没有侧栏的屏幕截图

I am currently working on an application in which I have to taken into account two ListViews one above another and that too with a common scroll. I know this is not the best practice, instead I should have taken just a single ListView but the problem is with the layouts that doesn't seem to be possible with a sinlge ListView. Actually my first ListView will contain list of friend requests that I can accept or ignore and that are not static and can vary with no limit and second list will contain my friends which may vary too i.e not static. If i have to only show these two things, then I should have preferred single ListView and can be easily done but the actual problem arises when i have to add an Alphabetical sorting of my second list i.e my friends list using side bar and that is applicable only for my second list not for first one. This one is the screenshot without side bar

,但按字母顺序排列的侧栏将出现在此处,用于进行新的连接.因此,我不认为这可以用单个列表实现,因为按字母顺序排列的侧边栏将添加到整个列表中,而不是列表的一部分.所以现在我正试图避免自己为两个ListView使用ScrollView,因为那会非常昂贵.所以我需要最好的方法来处理这种类型的结构.应该在ScrollView中使用两个ListView,或者可以使用单个ListView来完成,这将是更可取的.

but alphabetical side bar will be there for new connections. So I don't think, this can be possible with a single list since alphabetical side bar will be added to the whole list not the part of a list. so right now I am trying to save myself from using ScrollView for my two ListViews since that will be very expensive. so I need the best way as how to go with this type of structure. Should take two ListView inside ScrollView or it can be done with a single ListView that will be more preferred.

请倒出您的建议.提前致谢.

Please pour your suggestions. Thanks in advance.

推荐答案

如何将两个ListView一个接一个地放入LinearLayout容器中,然后将该容器放入ScrollView中呢?然后(以编程方式)计算出ListView用来包装所有内容所需的总高度,因此您的ListView不会滚动,而您的ScrollView会滚动.这是个主意:

What about putting your two ListViews one after the other inside a LinearLayout container and you put that container inside a ScrollView? Then (programatically) you calculate the total height that the ListView needs to be in order to wrap all it's content, so your ListViews will never scroll but your ScrollView will. Here is the idea:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <!-- Container -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <!-- Your list views -->
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
        <ListView
            android:id="@+id/listView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>
</ScrollView>

代码

private void resizeListView(ListView listView) {
    ListAdapter adapter = listView.getAdapter(); 
    int count = adapter.getCount();
    int itemsHeight = 0;
    // Your views have the same layout, so all of them have
    // the same height
    View oneChild = listView.getChildAt(0);
    if( oneChild == null)
        return;
    itemsHeight = oneChild.getHeight();
    // Resize your list view
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)listView.getLayoutParams();
    params.height = itemsHeight * count;
    listView.setLayoutParams(params);
}

您可能(实际上需要)等待ListView被绘制,以便getChildAt(int index)实际上可以返回View并避免获取null.您可以通过将其添加到您的onCreate:

You may want to (actually you need to) wait for the ListView to get drawn so getChildAt(int index) can actually return a View and avoid getting a null. You can achieve this adding this to your onCreate:

ViewTreeObserver listVTO = listView.getViewTreeObserver();
listVTO.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        resizeListView(listView);
    }
});

仅是一个建议,您可能需要尝试新的 RecyclerView 因为它可以处理不同类型的View s

Just a suggestion, you may want to try the new RecyclerView because it can handle recycling with different types of Views

这篇关于使用公共滚动将两个列表视图彼此合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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