当用户向上滚动列表视图时隐藏顶部栏,并在用户向下滚动时再次使其可见。

3

当用户向上滚动列表视图时,我希望隐藏顶部栏,并在用户向下滚动时立即使其可见。这与谷歌加底部菜单出现的方式相同,当我们向下滚动时它会出现,当我们向上滚动时它会消失。我已经尝试使用on scroll,但它只能给我第一个可见项目,这对我没有帮助,因为它需要将列表视图项目完全滚动以获取前一个第一个可见项。请任何人帮我,我被这个问题困住了。

1个回答

0
一种可能的解决方案是在您的ListView下添加一个View(一个带有textView或ImageView的布局):
<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>
<LinearLayout
    android:id="@+id/viewid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0" />

权重很重要,以显示两个组件。 然后,实现listView的onScrollListener。

setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //get the showed item num, and if its equal to total, you can hide the view
        //get a reference to your viewid
        viewid.setVisibility(View.GONE);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }
});

当到达最后一行时,为底部视图设置setVisibility(View.GONE)。 如果您想在用户向上滚动时再次显示此视图,请修改代码。当然,您可以使用另一个布局、textView或其他东西。

更新

我稍微调整了一下布局,有另一种解决方案适用于您的布局文件,其中底部视图覆盖在listView上,因此删除此textView是平稳的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:background="#ffffff"
        android:layout_alignParentBottom="true"
        android:text="..." />
</RelativeLayout>

请参考这里获取更多详细信息


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接