In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use RecyclerView to achieve high adaptation of waterfall flow". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to use RecyclerView to achieve high adaptation of waterfall flow".
Background: when using a custom ScrollView nested outside the RecyclerView, we need to make the RecyclerView highly adaptive, because many methods found in the waterfall stream format can not be realized or the dynamic calculation is highly inaccurate. I guess you all know that the height of recyclerview content is not controlled by recyclerview but set by LayoutManager. Let me talk about my solution:
Use in layout
Method one
I think the simplest and most effective way to solve the problem is the final choice.
Official website blog translation materials:
The RecyclerView control provides a flexible basic solution for creating lists and grids, and also supports animation. This version brings a very exciting new feature to LayoutManager API: automatic measurement! Let RecyclerView adjust itself to the size of its content. This means that previous scenarios with no solution, such as using WRAP_CONTENT for a dimension of RecyclerView, are possible. You will find that all built-in LayoutManager now support automatic measurement.
Because of this change, you have to carefully check the layout parameters of item view: layout parameters, which used to be automatically ignored (such as MATCH_PARENT in scrolling direction), is now fully taken into account. If you have a custom LayoutManager and do not inherit from the built-in LayoutManager, then this is an optional API-you need to call setAutoMeasureEnabled (true) and make some minor changes as described in the method's Javadoc.
Note that although RecyclerView can animate its own child View, it cannot animate its own boundary changes. If you want to animate the boundary changes of RecyclerView, you can use Transition API.
Version of the configuration:
Compile 'com.android.support:recyclerview-v7:23.2.1'
If you want the content to change with height, you need to set it: (note: this method was introduced in Android Support Library 23.2. If the previous version is configured, an error will be reported.)
LayoutManager.setAutoMeasureEnabled (true)
Use in Activity:
Recyclerview= ViewFindUtils.find (view, R.id.recyclerview); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager (2); layoutManager.setAutoMeasureEnabled (true); recyclerview.setLayoutManager (layoutManager); recyclerview.setHasFixedSize (true); recyclerview.setNestedScrollingEnabled (false); SpacesItemDecoration decoration = new SpacesItemDecoration (6); recyclerview.addItemDecoration (decoration); method 2
The custom CustomStaggeredGridLayoutManager class inherits from StaggeredGridLayoutManager. (note: the calculation of this method in my project is highly inaccurate, and there is always a gap at the back of the list, so I gave up ~ but you can try, it may be my layout nesting problem.)
Version of the configuration (the latest version is recommended):
Compile 'com.android.support:recyclerview-v7:23.1.1'
Use in Activity:
Recyclerview= ViewFindUtils.find (view, R.id.recyclerview); CustomStaggeredGridLayoutManager layoutManager = new CustomStaggeredGridLayoutManager (2StaggeredGridLayoutManager. Vertical); recyclerview.setLayoutManager (layoutManager); recyclerview.setHasFixedSize (true); recyclerview.setNestedScrollingEnabled (false); SpacesItemDecoration decoration = new SpacesItemDecoration (6); recyclerview.addItemDecoration (decoration)
SpacesItemDecoration.class
Public class SpacesItemDecoration extends RecyclerView.ItemDecoration {private int space; public SpacesItemDecoration (int space) {this.space = space;} @ Override public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {outRect.top = space; outRect.bottom = space; outRect.left = space; outRect.right = space;}}
CustomStaggeredGridLayoutManager.class
Package com.sunny.demo.widget;import android.support.v7.widget.RecyclerView;import android.support.v7.widget.StaggeredGridLayoutManager;import android.view.View;import android.view.ViewGroup;import com.parkmecn.ehc.utils.LogUtil;/** * solves the problem that RecyclerView needs to be highly adaptive when ScrollView nesting RecyclerView * / public class CustomStaggeredGridLayoutManager extends StaggeredGridLayoutManager {public CustomStaggeredGridLayoutManager (int spanCount, int orientation) {super (spanCount, orientation) } / / size array, [0] is wide, [1] is high private int [] measuredDimension = new int [2]; / / used to compare peer / column item crime width / height private int [] dimension; @ Override public void onMeasure (RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {/ / wide mode+size final int widthMode = View.MeasureSpec.getMode (widthSpec) Final int widthSize = View.MeasureSpec.getSize (widthSpec); / / High mode + size final int heightMode = View.MeasureSpec.getMode (heightSpec); final int heightSize = View.MeasureSpec.getSize (heightSpec); / / initial value of width and height int width = 0; int height = 0; / / number of item int count = getItemCount () / / the number of columns of item int span = getSpanCount (); / / create an array dimension = new int [span] based on the number of rows or columns; for (int I = 0; I
< count; i++) { measureScrapChild(recycler, i, View.MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), View .MeasureSpec.makeMeasureSpec(i, View.MeasureSpec.UNSPECIFIED), measuredDimension); // 如果是竖直的列表,计算item的高,否则计算宽度// LogUtil.d("LISTENER", "position " + i + " height = " + measuredDimension[1]); if (getOrientation() == VERTICAL) { dimension[findMinIndex(dimension)] += measuredDimension[1]; } else { dimension[findMinIndex(dimension)] += measuredDimension[0]; } } if (getOrientation() == VERTICAL) { height = findMax(dimension); } else { width = findMax(dimension); } switch (widthMode) { // 当控件宽是match_parent时,宽度就是父控件的宽度 case View.MeasureSpec.EXACTLY: width = widthSize; break; case View.MeasureSpec.AT_MOST: break; case View.MeasureSpec.UNSPECIFIED: break; } switch (heightMode) { // 当控件高是match_parent时,高度就是父控件的高度 case View.MeasureSpec.EXACTLY: height = heightSize; break; case View.MeasureSpec.AT_MOST: break; case View.MeasureSpec.UNSPECIFIED: break; } // 设置测量尺寸 setMeasuredDimension(width, height); LogUtil.e("setMeasuredDimension(width, height)--->Height== "+ height);} private void measureScrapChild (RecyclerView.Recycler recycler, int position, int widthSpec, int heightSpec, int [] measuredDimension) {/ / traverse all item if (position) one by one
< getItemCount()) { try { View view = recycler.getViewForPosition(position);//fix 动态添加时报IndexOutOfBoundsException if (view != null) { RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) view.getLayoutParams(); int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight (), lp.width); LogUtil.e(position + "--->HeightSpec= "+ heightSpec +"; getPaddingTop () = "+ getPaddingTop () +"; "+" getPaddingBottom () "+ getPaddingBottom () +"; lp.height= "+ lp.height); int childHeightSpec= ViewGroup.getChildMeasureSpec (heightSpec, getPaddingTop () + getPaddingBottom (), lp.height); LogUtil.e (position +"-- > viewchildHeightSpec= "+ childHeightSpec) / the sub-view is measured, and then the measured width and height are similar to view.measure (childWidthSpec, childHeightSpec) through getMeasuredWidth (); / / put the width and height of item into the array measuredDimension [0] = view.getMeasuredWidth () + lp.leftMargin + lp.rightMargin / / FIXME the height calculated here is always higher than the actual height, resulting in the final RecycerView height calculation is not correct, leaving a blank, temporarily did not find the problem, waiting for the great god to solve ah ~ measuredDimension [1] = view.getMeasuredHeight () + lp.topMargin + lp.bottomMargin LogUtil.e (position + "--> view.getMeasuredHeight () =" + view.getMeasuredHeight () + "; lp" + ".topMargin =" + lp.topMargin + "; lp.bottomMargin=" + lp.bottomMargin); recycler.recycleView (view);}} catch (Exception e) {e.printStackTrace () }} private int findMax (int [] array) {int max = array [0]; for (int value: array) {if (value > max) {max = value;}} return max } / * get the subscript of the smallest element in the array * * @ param array * @ return * / private int findMinIndex (int [] array) {int index = 0; int min = array [0]; for (int I = 0; I < array.length) If +) {if (array [I] < min) {min = array [I]; index = I;} return index }} Thank you for your reading. the above is the content of "how to use RecyclerView to achieve high adaptability of waterfall flow". After the study of this article, I believe you have a deeper understanding of how to use RecyclerView to achieve high self-adaptation of waterfall flow. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.