Force Recyclerview To Bottom Of Page In Layout
Solution 1:
Many thanks to everyone who contributed, but I have found a programmatic solution outside of the layout
files. In case anyone else is looking for a solution to this problem, I found one here.
It appears as if there is an issue with RecyclerView
currently where it doesn't wrap content. The answer is to construct a custom class that extends LinearLayoutManager
. I have posted the solution that worked for me below - most of it is copy and pasted from the answer given in the link I quoted. The only small issue is that it doesn't account for the extra space added by decorations, which is why I had to make a small tweak to the following line near the end of the code:
//I added the =2 at the end.
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;
Here is the code in its entirety:
publicclassHomeLinearLayoutManagerextendsLinearLayoutManager{
HomeLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
privateint[] mMeasuredDimension = newint[2];
@OverridepublicvoidonMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
finalintwidthMode= View.MeasureSpec.getMode(widthSpec);
finalintheightMode= View.MeasureSpec.getMode(heightSpec);
finalintwidthSize= View.MeasureSpec.getSize(widthSpec);
finalintheightSize= View.MeasureSpec.getSize(heightSpec);
intwidth=0;
intheight=0;
for (inti=0; i < getItemCount(); i++) {
measureScrapChild(recycler, i,
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED),
mMeasuredDimension);
if (getOrientation() == HORIZONTAL) {
width = width + mMeasuredDimension[0];
if (i == 0) {
height = mMeasuredDimension[1];
}
} else {
height = height + mMeasuredDimension[1];
if (i == 0) {
width = mMeasuredDimension[0];
}
}
}
switch (widthMode) {
case View.MeasureSpec.EXACTLY:
width = widthSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
switch (heightMode) {
case View.MeasureSpec.EXACTLY:
height = heightSize;
case View.MeasureSpec.AT_MOST:
case View.MeasureSpec.UNSPECIFIED:
}
setMeasuredDimension(width, height);
}
privatevoidmeasureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
Viewview= recycler.getViewForPosition(position);
if (view != null) {
RecyclerView.LayoutParamsp= (RecyclerView.LayoutParams) view.getLayoutParams();
intchildWidthSpec= ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
intchildHeightSpec= ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin + 2;
recycler.recycleView(view);
}
}
}
Thanks again.
Solution 2:
Put the two in a RelativeLayout and make ImageView fill parent:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"/><ImageViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_above="@id/recyclerView"
/></RelativeLayout>
Edit: Wrote TextView by accident. Fixed.
Solution 3:
The only solution I can think of is using layout weight. Specify 70% of the screen for the image and 30% for the Recyclerview
as you said you have just 3 rows. Use adjustViewByBounds
to ensure the images aspect ratio is maintained.
My code below:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:src="@drawable/ic_round_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:adjustViewBounds="true"android:layout_weight=".9"/><android.support.v7.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight=".1"/></LinearLayout>
Post a Comment for "Force Recyclerview To Bottom Of Page In Layout"