Android-recycleview Inside Scrollview
In my activity , I want to put a slideShow above my page and then put recycleview after that, to do so ,I'v written this code in my layout:
Solution 1:
Three steps to do this:
- Replace your scrollview with NestedScrollView with layout behaviour set to
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Keep all your widgets under a linear/relative layout(because nested scroll view accepts only one child) 3.create CustomLinearLayoutManager.java with this code:
publicclassCustomLinearLayoutManagerextendsLinearLayoutManager { publicCustomLinearLayoutManager(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; recycler.recycleView(view); } }
}
set adapter to your recyclerview like this:
CustomLinearLayoutManagercustomLinearLayoutManager=newCustomLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
updatesRecyclerView.setHasFixedSize(true);
updatesRecyclerView.setLayoutManager(customLinearLayoutManager);
updatesRecyclerView.setAdapter(yourAdapter);
updatesRecyclerView.setNestedScrollingEnabled(false);
That's it , it works.
Post a Comment for "Android-recycleview Inside Scrollview"