安卓:与ListView制作的列表视图滚动的顶视图在一起 [英] Android: Make view on top of listview scroll together with the listview

查看:156
本文介绍了安卓:与ListView制作的列表视图滚动的顶视图在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView这显然是滚动的。该列表视图中包含某种形式的问题。一旦用户submitts的形式我们把邮票般寻找自定义视图在列表视图的顶部(和prevent从回答问题的用户,但它不事在这里)。

I have a listview which is obviously scrollable. The listview contains some form questions. Once the user submitts the form we put a stamp-like looking custom view on top of the listview (and prevent user from answering questions, but it doesn't matter here).

邮票应该出现在列表视图的顶部(在屏幕顶部),并滚动速度相同的列表视图,也就是说,它应该消失,当列表视图项目而最初它下面的顶部消失当滚动屏幕。

The stamp should appear at the top of the listview (as in top of the screen) and be scrollable at the same speed as the listview, i.e. it should disappear when the listview items which were initially underneath it disappear at the top of the screen when scrolled.

不知道如何实现这一目标?

Any idea how to achieve this?

推荐答案

我找到了解决办法。不知道这是太高雅了,但它的工作原理,以便将离开它,除非有人来了更好的东西。

I found a solution. Not sure if it is too elegant, but it works so will leave it unless somebody comes up with something better.

我要与名单一起滚动视图是一个自定义视图,它必须了解列表视图。所以,我执行就可以了setListView(ListView控件ListView控件)方法:

The view which I want to scroll together with the list is a custom view, which has to know about the list view. So I implement a setListView(ListView listView) method on it:

private int scrollY; //1
private Map<Integer, Integer> listViewItemHeights = new Hashtable<>();
public void setListView(final ListView listView) {
    listView.setOnScrollListener(new OnScrollListener() {  //2
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {} //3

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //4
            View c = listView.getChildAt(0);
            if (c != null) {
                int oldScrollY = scrollY;
                scrollY = -c.getTop();

                listViewItemHeights.put(listView.getFirstVisiblePosition(), c.getHeight());
                for (int i = 0; i < listView.getFirstVisiblePosition(); ++i) {
                    if (listViewItemHeights.get(i) != null)
                        scrollY += listViewItemHeights.get(i);
                }
                scrollBy(0, scrollY - oldScrollY);
            }
        }
    });
}

注释第一:这是一个变量让我跟踪当前滚动位置的

Comment No.1: this is a variable letting me keep track of the current scroll position.

注释二:设置一个新的滚动监听器让我的自定义视图知道什么时候该列表

Comment No.2: setting a new on scroll listener to let my custom view know when the list

注释3号:这并不需要在此情况下实施

Comment No.3: this does not need to be implemented in this case.

点评四:这就是魔术发生。请注意,在最后我滚动我的看法由scrollY - oldScrollY,让我先此位第一。 oldScrollY是保持滚动位置,scrollY是新的。我需要通过它们之间的差值进行滚动。至于如何scrollY的计算方法请您看看我这里的回答:<一href="http://stackoverflow.com/questions/10808387/android-getting-exact-scroll-position-in-listview/13415273#13415273">Android凑的ListView 精确滚动位置,这是多么滚动位置的计算在列表视图中。

Comment No.4: this is where magic happens. Note in the end I scroll my view by scrollY - oldScrollY, let me start with this bit first. oldScrollY is the kept scroll position, scrollY is the new one. I need to scroll by the difference between them. As for how scrollY is calculated I refer you to my answer here: Android getting exact scroll position in ListView, it is how the scroll position is calculated in list view.

这篇关于安卓:与ListView制作的列表视图滚动的顶视图在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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