FAB和ListView的快速返回模式

时间:2015-02-25 10:08:29

标签: android android-layout android-listview android-5.0-lollipop

我在ViewPager中有一个简单的ListView,在屏幕的右下角有一个浮动操作栏。

我想对它实现快速返回模式,这样当我滚动ListView时,FAB会向下移动到屏幕上,然后出现在相反的滚动上。

提前致谢。

编辑:

list.setAdapter(new QuickReturnAdapter(adapter));
            quickReturnAttacher = QuickReturnAttacher.forView(list);
            quickReturnAttacher.addTargetView(floatMenu, AbsListViewScrollTarget.POSITION_BOTTOM, dpToPx(context, 400));

            if (quickReturnAttacher instanceof AbsListViewQuickReturnAttacher) {
                // This is the correct way to register an OnScrollListener.
                // You have to add it on the QuickReturnAttacher, instead
                // of on the viewGroup directly.
                final AbsListViewQuickReturnAttacher attacher = (AbsListViewQuickReturnAttacher) quickReturnAttacher;
                attacher.addOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {

                    }

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                    }
                });
            }

然后我使用的是quickReturnAttacher.setOnItemClickListener,但这不起作用。

2 个答案:

答案 0 :(得分:2)

此功能已有一个开源库。 https://github.com/felipecsl/QuickReturn

试试这个:

    final AbsListViewQuickReturnAttacher attacher =   (AbsListViewQuickReturnAttacher) quickReturnAttacher;
    attacher.addOnScrollListener(this);
    attacher.setOnItemClickListener(this);

});

对我而言,这是有效的,回调中传递的位置是正确的。 您必须将您的点击监听器注册到attacher,而不是列表。

如何在课程setOnItemClickListener中找不到方法AbsListViewQuickReturnAttacher

答案 1 :(得分:0)

您可以尝试使用此解决方案进行ListViews的快速返回模式:

yourListView.setOnScrollListener(new AbsListView.OnScrollListener() {
         @SuppressLint("NewApi")
         @Override
         public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            int scrollOffset = 0;
            float transitionY;

            if (firstVisibleItem > 0) {
               scrollOffset += headerHeight;
               if (firstVisibleItem > 1) {
                  scrollOffset += (firstVisibleItem - 1) * cellHeight;
               }
            }

            if (yourListView.getChildCount() > 0) {
               scrollOffset += -yourListView.getChildAt(0).getTop();
               scrollOffset = -scrollOffset;
            }

            float scrollDelta = scrollOffset - prevOffset;
            float nextY = mQuickReturnView.getY() + scrollDelta;

            if (nextY < minRawY) {
               transitionY = minRawY;
            }
            else if (nextY > qReturnDelta) {
               transitionY = qReturnDelta;
            }
            else {
               transitionY = nextY;
            }

            mQuickReturnView.setY(transitionY);
            prevOffset = scrollOffset;
         }

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