1.單一佈局的效果圖,2列android
這種邊距很是簡單:a.在activity的主佈局中,爲ByRecyclerView設置一個layout_marginRight緩存
<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" />
複製代碼
b.在ByRecyclerView適配器引入的佈局中,設置layout_marginLeft和layout_marginTop,便可實現上面的卡片等邊距的效果bash
<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">
複製代碼
可是呢!!!!!若是是多佈局,position=0的佈局是單列,並且不要邊距,要怎麼辦呢? 本篇的重點來咯~~~ 1.首先在activity的佈局文件中,設置ByRecyclerViewapp
<me.jingbin.library.ByRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
複製代碼
適配器中代碼ide
//根據條件返回條目的類型
@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;
//若是條目的類型等於1,加載輪播圖,不然加載文章列表
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);
/**
* 用此方法margin無效果
*/
// View view = LayoutInflater.from(activity).inflate(R.layout.item_layout_like1, null);
if (holder == null) {
holder = new ArticleViewHolder(view);
}
}
return holder;
}
複製代碼
home_tab.xml,代碼以下:佈局
重點是:position=0的佈局沒有設置marginui
<?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>
複製代碼
item_layout_like1.xml,代碼以下:this
下面列表佈局也沒有設置marginspa
<?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>
複製代碼
重點來咯~code
咱們在設配器綁定數據的時候,動態爲兩列布局設置margin
1.首先要計算好position,由於頂部position=0佔了一個佈局,因此下面列表的實際position應該要-1
2.判斷左邊項和右邊項:經過跟2求餘來判斷(newPosition%2)
3.左邊項設置:leftMargin,bottomMargin
4.右邊項設置: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問題
* 2.多佈局,因此newPosition = i - 1;
* 3.判斷右邊列:newPosition%2!=0 奇數
*/
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);//使設置好
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() + "人付款");
articleViewHolder.tv_title.setText(TextViewUtils.noEmpty(bean.getGoods_name()));
LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) articleViewHolder.layout_img.getLayoutParams(); //取控件textView當前的佈局參數
linearParams.height = DensityUtil.getDisplayWidth()/2 - (int)activity.getResources().getDimension(R.dimen.dp_10)*3;// 控件的高強制設成
articleViewHolder.layout_img.setLayoutParams(linearParams); //使設置好的佈局參數應用到控件
/**
* 解決滑動recyclerview致使glide圖片加載閃爍
* 1.利用setTag
* 2.Glide設置skipMemoryCache(true),跳過緩存
*/
if (!bean.getImage().equals(articleViewHolder.iv.getTag(R.id.iv_hot))) {
// 加載圖片
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());
});
}
}
複製代碼
好了,就這樣完美解決了!