Android-RecyclerView width and height adaptive filling

Basic knowledge

  • GridLayoutManager inherits from LinearLayoutManager and is the layout manager of RecyclerView, mainly to realize grid layout.
  • GridLayoutManager.setOrientation(GridLayoutManager.VERTICAL)Or HORIZONTALmay be provided in the extending direction of RecyclerView, VERTICAL is, that extend in the longitudinal direction.
  • new GridLayoutManager(this, row)Or setSpanCount(row)you can set the number of ranks RecyclerView, in particular a row or column, you need to look at setOrientation(), is VERTICAL is refers to the number of rows.
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, column);
gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(gridLayoutManager);

Single direction adaptive filling

  • setOrientation(GridLayoutManager.VERTICAL)+ layout_width="match_parent"Allow RecyclerView to adapt to the width in the horizontal direction;
  • setOrientation(GridLayoutManager.HORIZONTAL)+ layout_height="match_parent"Allows RecyclerView to be highly adaptive in the vertical direction;

Width and height bidirectional adaptive padding (Why am I so cheap, so many methods, I have to use RecyclerView)

  • Take setOrientation(GridLayoutManager.VERTICAL)+ layout_width="match_parent"as an example. At this time, the horizontal direction is adaptive, and the vertical direction is infinitely extended.

Insert picture description here

  • The Item is android:layout_heightset to"match_parent"
  • Custom LinearLayout (each item of mine is a LinearLayout, take this example), write the onMeasure method
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    
    int newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / line, EXACTLY);
    super.onMeasure(widthMeasureSpec, newHeightMeasureSpec);
}
  • Here lineis the number of rows
  • Modify LinearLayout in XML to be customized, the effect is as follows

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36881363/article/details/105813441