【问题标题】:Android - scrolling out/collapsing an element while scrolling in ListViewAndroid - 在 ListView 中滚动时滚动出/折叠元素
【发布时间】:2015-11-17 22:26:32
【问题描述】:

我有以下场景:

ImageView,有两个标签的 TabHost。这两个选项卡里面都有 ListView。

我想要实现的是:

在选项卡的 ListView 中向上/向下滚动时 - 滚动或折叠顶部的 ImageView。

这意味着当我向下滚动时,ImageView 将消失,因为我将向下滚动。如果我向上滚动,ImageView 将再次出现。

我想在不使用 ScrollView 内的 ListView 的情况下实现这一点(到目前为止还没有产生好的结果)。

我将如何实现它?

【问题讨论】:

    标签: android listview android-listview xamarin android-scrollview


    【解决方案1】:

    我知道这可能不是您要寻找的答案,但您可以保留 scoolView(可能您必须这样做)。

    尝试查看this 帖子,该帖子完美地解释了如何在滚动视图中设置一些列表视图。

    希望对你有帮助:)

    编辑:我找到了this。这不完全是您正在寻找的解决方案,但它使用 onScrollListener,我认为您可以调整此答案来解决您的问题,让我知道它是否有效!

    【讨论】:

    • 干杯,我以前也看过这个链接。仍然是我想避免的事情。不过谢谢。
    • @JakubHolovsky 我改进了答案,尝试查看我写的帖子并告诉我它是否有效! :)
    【解决方案2】:

    这是我在滚动视图中使用图像视图和列表视图的示例 您可以在 XML 中添加自己的两个选项卡,不会有任何区别 希望这会有所帮助。

    布局.xml

     <ScrollView
        android:id="@+id/sv_itemRank"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/img_item_image"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:contentDescription="@string/empty"
                android:scaleType="centerCrop" />
    
            <ListView
                android:id="@+id/lv_items_lists"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@color/white"
                android:divider="@null"
                android:dividerHeight="0dp"
                android:scrollbars="none" >
            </ListView>
        </LinearLayout>
    </ScrollView>
    

    在我的情况下填充列表视图后,我使用自定义适配器调用此函数来设置基于子项的列表视图的高度 我在adapter.notifyDataSetChanged(); 之后完成了这个 你根据你的代码改变它。

    setListViewHeightBasedOnChildren(listview) 的代码

    将您的列表视图作为参数传递。

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;
    
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
                MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
                        LayoutParams.WRAP_CONTENT));
    
            view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
    

    【讨论】:

    • 为你的回答干杯,这是我想避免的,不过会认为这是一个答案。
    猜你喜欢
    • 2017-05-24
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多