是有可能在一个RecyclerView的最后一个项目,以停靠在底部,如果没有必要滚动? [英] Is it possible to have the last item in a RecyclerView to be docked to the bottom if there is no need to scroll?

查看:827
本文介绍了是有可能在一个RecyclerView的最后一个项目,以停靠在底部,如果没有必要滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个购物车RecyclerView,在一个RecyclerView显示购物车中的所有项目,以及它在汇总车底部的附加视图(总因,优惠券的折扣,如果适用,等等)。

如果有> 3项中的车,它看起来细如用户将必须滚动至底部查看摘要视图。但是,如果有1或2项中,第一项的出现,比摘要视图,然后是空格。我宁愿第一个项目,然后是空格,最后总结观点。

我试着加入空的项目,但是,这取决于设备的分辨率,看起来不协调。

当前的外观如果少于3个项目(即如果不需要滚动):

  -----------
|项目1 |
| ------- |
|项目2 |
| ------- |
|摘要|
| ------- |
| |
| |
| |
| |
 ----------
 

所需的外观:

  -----------
|项目1 |
| ------- |
|项目2 |
| ------- |
| |
| |
| |
| |
| ------- |
|摘要|
 ----------
 

解决方案

我在想你的任务,并最终把一些code,你可能会发现有用的。有在这个舞台上的一个问题,但。

我所做的是增加了一个项目的装饰到回收的观点:

  recyclerView.addItemDecoration(新StickySummaryDecoration());
 

和这里是我实现的基本装饰的(坦率地说,这是我与项目的装饰第一次的经验,所以它可能不是最优的或完全正确的,但我尽力了):

公共类StickySummaryDecoration扩展RecyclerView.ItemDecoration {     @覆盖     公共无效onDrawOver(帆布C,RecyclerView父母,RecyclerView.State州){         INT childCount = parent.getAdapter()getItemCount()。         INT lastVisibleItemPosition =                 ((LinearLayoutManager)parent.getLayoutManager())findLastVisibleItemPosition()。         INT firstVisiblePosition =                 ((LinearLayoutManager)parent.getLayoutManager())                         .findFirstCompletelyVisibleItemPosition();         如果((firstVisiblePosition == 0)及及(lastVisibleItemPosition ==(childCount - 1))){             查看summaryView = parent.getChildAt(parent.getChildCount() - 1);             INT topOffset = parent.getHeight() - summaryView.getHeight();             INT左偏移=                     ((RecyclerView.LayoutParams)summaryView.getLayoutParams())LEFTMARGIN。             c.save();             c.translate(左偏移,topOffset);             summaryView.draw(C);             c.restore();             summaryView.setVisibility(View.GONE);         }     } }

那么,我得到这一点。

在短名单底停靠摘要:

您需要向下滚动查看摘要中的长长的名单:

现在的一个问题。这在长长的名单滚动备份时的副作用是什么,我还没有解决呢。

我的实验上传到 GitHub库以防万一:)

修改

我刚才来找我,在回收视图中缺失的行元素是我回收摘要视图持有人具有 GONE 的知名度。应该有一种方法来把它带回来...

编辑2

我重新考虑我的第一个解决方案,改变了codeA点点占一长串的情况下(在这种情况下,该项目的装修被禁用(我认为这是更接近你想要的东西来实现)),这种自动解析缺少行的问题。

I'm building a shopping cart RecyclerView that displays all the items in the cart in a RecyclerView, as well as it has an additional view at the bottom that summarizes the cart (total owing, coupon discount if applicable, etc).

If there's > 3 items in the cart, it looks fine as the user will have to scroll to the bottom to view the "summary view". However, if there's 1 or 2 items, the first items appear, than the summary view, then whitespace. I'd rather the first items, then whitespace, then the summary view.

I've tried adding empty items, however, depending on the device's resolution, it looks inconsistent.

Current appearance if less than 3 items (ie if no scrolling required):

-----------
|  Item1  |
| ------- |
|  Item2  |
| ------- |
| Summary |
| ------- |
|         |
|         |
|         |
|         |
 ----------

Desired appearance:

-----------
|  Item1  |
| ------- |
|  Item2  |
| ------- |
|         |
|         |
|         |
|         |
| ------- |
| Summary |
 ----------

解决方案

I was thinking about your task and eventually put together some code that you may find useful. There's a problem on this stage though.

What I did is added an item decorator to a recycler view:

recyclerView.addItemDecoration(new StickySummaryDecoration());

And here's my implementation of the basic decorator (frankly, it's my first experience with item decorators, so it may not be optimal or totally correct but I did my best):

public class StickySummaryDecoration extends RecyclerView.ItemDecoration {

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int childCount = parent.getAdapter().getItemCount();
        int lastVisibleItemPosition =
                ((LinearLayoutManager) parent.getLayoutManager()).findLastVisibleItemPosition();
        int firstVisiblePosition =
                ((LinearLayoutManager) parent.getLayoutManager())
                        .findFirstCompletelyVisibleItemPosition();
        if ((firstVisiblePosition == 0) && (lastVisibleItemPosition == (childCount - 1))) {
            View summaryView = parent.getChildAt(parent.getChildCount() - 1);
            int topOffset = parent.getHeight() - summaryView.getHeight();
            int leftOffset =
                    ((RecyclerView.LayoutParams) summaryView.getLayoutParams()).leftMargin;
            c.save();
            c.translate(leftOffset, topOffset);
            summaryView.draw(c);
            c.restore();
            summaryView.setVisibility(View.GONE);
        }
    }
}

So what I get with this.

Bottom-docked summary in the short list:

You need to scroll down to see the summary in the long list:

Now about a problem. This side-effect when scrolling back up in the long list is what I haven't solved yet.

My experiments are uploaded to a github repo just in case :)

EDIT

Just now I came to me that the missing row element in the recycler view is my recycled summary view holder that has GONE visibility. There should be a way to bring it back...

EDIT 2

I reconsidered my first solution and changed the code a little bit to account for the long list case (in which case the item decoration is disabled (I think it's closer to what you wanted to achieve)), this automatically resolves the missing row problem.

这篇关于是有可能在一个RecyclerView的最后一个项目,以停靠在底部,如果没有必要滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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