1. Single layout renderings, 2 columns

This margin is very simple:

A. In the main layout of the activity, set a layout_marginRight for ByRecyclerView

    <me.jingbin.library.ByRecyclerView
     android:id="@+id/recycler_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_marginRight="10dp"
      android:overScrollMode="never" />
Copy the code

B. Set layout_marginLeft and layout_marginTop in the layout introduced by ByRecyclerView adapter to achieve the effect of card equalization

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/colorWhite"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="0dp">
Copy the code

But what about !!!!! If the layout is multiple and position=0 is a single column with no margins, what can be done? 1. In the activity layout file, set ByRecyclerView

     <me.jingbin.library.ByRecyclerView
      android:id="@+id/recycler_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:overScrollMode="never" />
Copy the code

Code in adapter

@override public int getItemViewType(int position) {if (position == 0) {return ITEM_TYPE_1; } else { return ITEM_TYPE_2; } } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { RecyclerView.ViewHolder holder = null; If (I == ITEM_TYPE_1) {View viewTop = // If (I == ITEM_TYPE_1) {View viewTop = LayoutInflater.from(activity).inflate(R.layout.home_tab, viewGroup, false); if (holder == null) { holder = new BannnerViewHolder(viewTop); } } else if (i == ITEM_TYPE_2) { View view = LayoutInflater.from(activity).inflate(R.layout.item_layout_like1, viewGroup, false); // View View = layoutinflater.from (activity).inflate(r.layout.item_layout_like1, null); if (holder == null) { holder = new ArticleViewHolder(view); } } return holder; }Copy the code

Home_tab.xml, with the following code:

Position =0 layout does not set margin

<? The XML version = "1.0" encoding = "utf-8"? > <layout xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="10dp" android:orientation="vertical"> <RelativeLayout android:id="@+id/search" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="220dp" android:layout_marginTop="-1dp" android:layout_alignParentTop="true"> ..... </RelativeLayout> </LinearLayout> </layout>Copy the code

Item_layout_like1.xml, the code is as follows:

The following list layout also has no margin set

<? The XML version = "1.0" encoding = "utf-8"? > <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardBackgroundColor="@color/colorWhite" app:cardCornerRadius="5dp" app:cardElevation="0dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> .... </LinearLayout> </android.support.v7.widget.CardView>Copy the code

Here’s the point

We dynamically set margins for the two column layout while the setters bind data

1. Calculate position first, since position=0 at the top occupies a layout, the actual position in the list below should be -1

2. Determine the left and right items by complementing 2 (newPosition%2)

3. Set the left item: leftMargin, bottomMargin

4. Set the right item: leftMargin, bottomMargin, rightMargin

public class HomeAdapter3 extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private int margin; public HomeAdapter3(Activity context) { this.activity = context; margin = (int) activity.getResources().getDimension(R.dimen.dp_10); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { if (viewHolder instanceof BannnerViewHolder) { onBindTopViewHolder((BannnerViewHolder) viewHolder, position); } else if (viewHolder instanceof ArticleViewHolder) { onBindMainViewHolder((ArticleViewHolder) viewHolder, position); } } private void onBindMainViewHolder(ArticleViewHolder articleViewHolder, int position) { /** * 1. Recyclerview margin problem * 2. Multiple layout, so newPosition = i-1; * 3. Judge the right column: newPosition%2! =0 odd */ int newPosition = position; if (listData.size() > 0) { newPosition = position - 1; } GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) articleViewHolder.itemView.getLayoutParams(); if (newPosition % 2 ! = 0) { layoutParams.rightMargin = margin; } else { layoutParams.rightMargin = 0; } layoutParams.topMargin = 0; layoutParams.bottomMargin = margin; layoutParams.leftMargin = margin; articleViewHolder.itemView.setLayoutParams(layoutParams); // Set final ListData bean = listData.get(newPosition); articleViewHolder.tv_price_title.setText(FormatUtils.getMoneySign()); articleViewHolder.tvPrice.setText(TextViewUtils.noEmpty(bean.getGoods_min_price())); Articleviewholder.tv_goods_sales.settext (bean.getGoods_sales() + "payer "); articleViewHolder.tv_title.setText(TextViewUtils.noEmpty(bean.getGoods_name())); LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) articleViewHolder.layout_img.getLayoutParams(); / / take control textView linearParams current layout parameters. The height = DensityUtil. GetDisplayWidth () / 2 - (int)activity.getResources().getDimension(R.dimen.dp_10)*3; // The high force of the control is set to articleViewholder.layout_img.setLayOutParams (linearParams); // Make the set layout parameters applied to the control /** * solve the sliding recyclerView caused glide picture loading flicker * 1. SkipMemoryCache (true), skip cache */ if (! Bean. The getImage () equals (articleViewHolder. Iv. GetTag (R.i d.i v_hot))) {/ / loading pictures Glide.with(activity).load(bean.getImage()).skipMemoryCache(true) // .apply(options) .into(articleViewHolder.iv); articleViewHolder.iv.setTag(R.id.iv_hot, bean.getImage()); } articleViewHolder.itemView.setOnClickListener(view -> { GoodDetailActivity.start(activity, bean.getId()); }); }}Copy the code

All right, that’s it!