android - Is it possible to have the last item in a RecyclerView to be docked to the bottom if there is no need to scroll? -
i'm building shopping cart recyclerview displays items in cart in recyclerview, has additional view @ bottom summarizes cart (total owing, coupon discount if applicable, etc).
if there's > 3 items in cart, looks fine user have scroll bottom view "summary view". however, if there's 1 or 2 items, first items appear, summary view, whitespace. i'd rather first items, whitespace, summary view.
i've tried adding empty items, however, depending on device's resolution, looks inconsistent.
current appearance if less 3 items (ie if no scrolling required):
----------- | item1 | | ------- | | item2 | | ------- | | summary | | ------- | | | | | | | | | ----------
desired appearance:
----------- | item1 | | ------- | | item2 | | ------- | | | | | | | | | | ------- | | summary | ----------
i thinking task , put code may find useful. there's problem on stage though.
what did added item decorator recycler view:
recyclerview.additemdecoration(new stickysummarydecoration());
and here's implementation of basic decorator (frankly, it's first experience item decorators, may not optimal or totally correct did 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 this.
bottom-docked summary in short list:
you need scroll down see summary in long list:
now problem. side-effect when scrolling in long list haven't solved yet.
my experiments uploaded github repo in case :)
edit
just came me missing row element in recycler view recycled summary view holder has gone
visibility. there should way bring back...
edit 2
i reconsidered first solution , changed code little bit account long list case (in case item decoration disabled (i think it's closer wanted achieve)), automatically resolves missing row problem.
Comments
Post a Comment