SoFunction
Updated on 2025-04-07

Android RecyclerView usage method analysis

1. Introduction

RecyclerView is a new view group with the goal of providing a similar rendering for any adapter-based view. It is supported in the latest support-V7 version as the successor to ListView and GridView controls. The RecyclerView architecture provides a plug-in experience, highly decoupling, exceptional flexibility, and achieves eye-catching effects by setting the different LayoutManagers, ItemDecoration, ItemAnimator that it provides.

In short, the RecyclerView that ListView and GridView can do can be used, and can achieve waterfall flow effect.

2. Use

The library in the configuration of RecyclerView
compile ':recyclerview-v7:23.3.0'

//Set the adapter of RecyclerViewadapter = new MyRecyclerViewAdapter(,datas);
(adapter);
 
//LayoutManager
(new LinearLayoutManager(, , false));
// (()-1);
 
//Add a split line of RecyclerView(new DividerListItemDecoration(,DividerListItemDecoration.VERTICAL_LIST));
 
//Set animation(new DefaultItemAnimator());

public class MyAdapter extends <> {
 
 private final Context context;
 private final ArrayList<String> datas;
 
 public MyAdapter(Context context,ArrayList<String> datas){
  = context;
  = datas;
 }
 /**
  * It is equivalent to the create holder layout of getView in ListView adapter
  *
  * @param parent
  * @param viewType
  * @return
  */
 @Override
 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 View view = (context, .item_hello, null);
 return new MyViewHolder(view);
 }
 
 @Override
 public void onBindViewHolder(MyViewHolder holder, int position) {
 holder.tv_text.setText((position));
 holder.iv_icon.setBackgroundResource(.ic_launcher);
 
 }
 
 @Override
 public int getItemCount() {
 return ();
 }
 
 class MyViewHolder extends  {
 
 private TextView tv_text;
 private ImageView iv_icon;
 
 public MyViewHolder(View itemView) {
  super(itemView);
  tv_text = (TextView) (.tv_text);
  iv_icon = (ImageView) (.iv_icon);
 
 }
 }
}

3. Set ListView&GridView&Waterflow Type Effect

//Set List type effect(new LinearLayoutManager(,,false));
 
//Set Grid type effect(new GridLayoutManager(, 2, , false));
//(99);
 
//Set the waterfall flow type effect(new StaggeredGridLayoutManager(3,));

4. Dividing line

Reference URL:/lmj623565791/article/details/45059587

//Set the dividing line - the dividing line needs to be customized & you can also customize the style of the dividing line//No default split line is provided(new DividerListItemDecoration(this, DividerListItemDecoration.VERTICAL_LIST));
public class DividerListItemDecoration extends  {
 private static final int[] ATTRS = new int[]{
  
 };

 public static final int HORIZONTAL_LIST = ;

 public static final int VERTICAL_LIST = ;

 private Drawable mDivider;

 private int mOrientation;

 public DividerListItemDecoration(Context context, int orientation) {
 final TypedArray a = (ATTRS);
 mDivider = (0);
 ();
 setOrientation(orientation);
 }

 public void setOrientation(int orientation) {
 if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
  throw new IllegalArgumentException("invalid orientation");
 }
 mOrientation = orientation;
 }

 @Override
 public void onDraw(Canvas c, RecyclerView parent) {
// ("recyclerview - itemdecoration", "onDraw()");

 if (mOrientation == VERTICAL_LIST) {
  drawVertical(c, parent);
 } else {
  drawHorizontal(c, parent);
 }

 }


 public void drawVertical(Canvas c, RecyclerView parent) {
 final int left = ();
 final int right = () - ();

 final int childCount = ();
 for (int i = 0; i < childCount; i++) {
  final View child = (i);
  . v = new .(());
  final  params = () child
   .getLayoutParams();
  final int top = () + ;
  final int bottom = top + ();
  (left, top, right, bottom);
  (c);
 }
 }

 public void drawHorizontal(Canvas c, RecyclerView parent) {
 final int top = ();
 final int bottom = () - ();

 final int childCount = ();
 for (int i = 0; i < childCount; i++) {
  final View child = (i);
  final  params = () child
   .getLayoutParams();
  final int left = () + ;
  final int right = left + ();
  (left, top, right, bottom);
  (c);
 }
 }

 @Override
 public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
 if (mOrientation == VERTICAL_LIST) {
  (0, 0, 0, ());
 } else {
  (0, 0, (), 0);
 }
 }
}

DividerListItemDecoration

Set the split line style:

① Application settings

<!-- Base application theme. -->
<style name="AppTheme" parent="">
 <!-- Customize your theme here. -->
 <item name="colorPrimary">@color/colorPrimary</item>
 <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 <item name="colorAccent">@color/colorAccent</item>
 <item name="android:listDivider">@drawable/divider_bg</item>
</style>

②In activity

<activity android:name="."
 android:theme="@style/listDividerTheme"/>


<style name="listDividerTheme" parent="">
 <!-- Customize your theme here. -->
 <item name="colorPrimary">@color/colorPrimary</item>
 <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 <item name="colorAccent">@color/colorAccent</item>
 <item name="android:listDivider">@drawable/divider_bg</item>
</style>

5. Customize the click event of the item

RecyclerView has no click event by default, and you need to customize click event
Use knowledge points: interface, getLayoutPosition()

public class MyAdapter extends &lt;&gt;{
 
 private final Context context;
 private final ArrayList&lt;String&gt; datas;
 
 //Set the monitoring of clicking on an item public interface OnItemClickListener{
  void onItemClick(View view,int position,String content);
 }
 
 private OnItemClickListener onItemClickListener;
 public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
  = onItemClickListener;
 }
 
 //Set click on the picture public interface OnImageViewClickListener{
 void onImageViewClick(View view,int position);
 }
 
 private OnImageViewClickListener onImageViewClickListener;
 public void setOnImageViewClickListener(OnImageViewClickListener onImageViewClickListener) {
  = onImageViewClickListener;
 }
 
 ...................................
 
 class MyViewHolder extends  {
 
 private TextView tv_text;
 private ImageView iv_icon;
 
 public MyViewHolder(View itemView) {
  super(itemView);
  tv_text = (TextView) (.tv_text);
  iv_icon = (ImageView) (.iv_icon);
 
  //Set click event  (new () {
  @Override
  public void onClick(View v) {
   if(onItemClickListener != null){
   (v,getLayoutPosition(),(getLayoutPosition()));
   }
  }
  });
 
  //Set up the monitoring  iv_icon.setOnClickListener(new () {
  @Override
  public void onClick(View v) {
   if(onImageViewClickListener != null){
   (v,getLayoutPosition());
   }
  }
  });
 }
 }
}

Use custom click events in Activity

//Set the click event of clicking item(new () {
 @Override
 public void onItemClick(View view, int position, String content) {
 (, "content=="+content+",--position=="+position, Toast.LENGTH_SHORT).show();
 }
});
 
//Set the click event of clicking on a certain image(new () {
 @Override
 public void onImageViewClick(View view, int position) {
 (, "position=="+position+",view=="+(), Toast.LENGTH_SHORT).show();
 }
});

6. Delete and add data

1_Add two new methods to add and delete in the adapter

public class MyAdapter extends <> {
 
 ........................
 
 public void addData(int position,String content){
 (position,content);
 notifyItemInserted(position);
 }
 
 public void removeData(int position){
 (position);
 notifyItemRemoved(position);
 }
}

2_Activity

btn_add.setOnClickListener(new () {
 @Override
 public void onClick(View v) {
 (0,"Content NetData");
 //Position to the 0th position (0);
 }
});
 
btn_remove.setOnClickListener(new () {
 @Override
 public void onClick(View v) {
 (0);
 }
});

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.