当前位置:网站首页>用recyclerReview展示Banner,很简单
用recyclerReview展示Banner,很简单
2022-07-01 00:40:00 【Fahaxiki丿】
不废话,直接上图

1.布局随便
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_emigrates"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:overScrollMode="never"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toBottomOf="@+id/tv_emigrates_more"
tools:layout_editor_absoluteX="0dp" />2. 打造水平 ItemDecorationHorizontal
import android.graphics.Rect;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.xx.common.utils.ScreenUtil;
public class ItemDecorationHorizontal extends RecyclerView.ItemDecoration {
private int left;
private int top;
private int right;
private int bottom;
private int first;
public ItemDecorationHorizontal(int left, int top, int right, int bottom) {
this.left = (int) ScreenUtil.dp2px(left);
this.top = (int) ScreenUtil.dp2px(top);
this.right = (int) ScreenUtil.dp2px(right);
this.bottom = (int) ScreenUtil.dp2px(bottom);
}
public ItemDecorationHorizontal(int first, int left, int top, int right, int bottom) {
this.first = (int) ScreenUtil.dp2px(first);
this.left = (int) ScreenUtil.dp2px(left);
this.top = (int) ScreenUtil.dp2px(top);
this.right = (int) ScreenUtil.dp2px(right);
this.bottom = (int) ScreenUtil.dp2px(bottom);
}
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int left = this.left;
if (first > 0) {
int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
if (position == 0)
left += first;
}
outRect.set(left, top, right, bottom);
}
}3.配置
emigratesList = new ArrayList<>();
emigratesAdapter = new EmigratesAdapter(getContext(), emigratesList);
binding.recyclerEmigrates.addItemDecoration(new ItemDecorationHorizontal(12, 0, 0, 12, 0));
binding.recyclerEmigrates.setHasFixedSize(true);
binding.recyclerEmigrates.setAdapter(emigratesAdapter);
//请求接口成功后,填充数据
//刷新适配器
emigratesList.addAll(data.getHwEmigrates());
emigratesAdapter.notifyDataSetChanged();4.Adapter ,没有用Bindding的,自己修改下就好了
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xx.common.bean.ListString;
import com.xx.common.bean.NationalProjectDto;
import com.xx.common.route.RoutePath;
import com.xx.module.club365.R;
import com.xx.module.club365.databinding.AdapterEmigratesBinding;
import com.xx.module.club365.databinding.BannerAdapterItemBinding;
import java.util.List;
public class EmigratesAdapter extends RecyclerView.Adapter {
private Context mContext;
private List<NationalProjectDto> mList;
private LayoutInflater mInflater;
public EmigratesAdapter(Context context, List<NationalProjectDto> list) {
this.mContext = context;
this.mList = list;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getItemViewType(int position) {
return 0;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder;
AdapterEmigratesBinding binding = AdapterEmigratesBinding.inflate(mInflater, parent, false);
holder = new EmigratesHolder(binding);
holder.itemView.setOnClickListener(v -> {
int position = holder.getAdapterPosition();
ARouter.getInstance().build(RoutePath.IMMIGRANT_INFO)
.withString("telephone",mList.get(position).getTelephone())
.withString("images", new Gson().toJson(new ListString(mList.get(position).getImages())))
.navigation();
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((EmigratesHolder) holder).binding.brief.setText(mList.get(position).getBrief());
((EmigratesHolder) holder).binding.name.setText(mList.get(position).getName());
Glide.with(mContext).load(mList.get(position).getImage()).placeholder(R.drawable.icon_def)
.error(R.drawable.icon_def).into(((EmigratesHolder) holder).binding.image);
}
@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}
class EmigratesHolder extends RecyclerView.ViewHolder {
AdapterEmigratesBinding binding;
public EmigratesHolder(@NonNull AdapterEmigratesBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
5.adapter布局 布局的大小写法很重要(注意)
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="6dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="252dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/icon_def"
app:layout_constraintDimensionRatio="w,110:252"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/brief"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/image"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="10dp"
android:layout_marginBottom="3dp"
android:textSize="12sp"
android:textColor="@color/white"
android:text="移居+留学,完备你的“美国梦”"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@+id/brief"
app:layout_constraintBottom_toTopOf="@+id/brief"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold"
android:text="美国移居+留学规划"/>
<TextView
android:id="@+id/country"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:padding="10dp"
android:textColor="@color/white"
android:background="@drawable/bg_black_transparent_shape"
android:text="美国留学"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>边栏推荐
猜你喜欢

DLS-20型双位置继电器 220VDC

For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
![[learning notes] double + two points](/img/d4/1ef449e3ef326a91966da11b3c8210.png)
[learning notes] double + two points

写给 5000 粉丝的一封信!

TCP三次握手为什么不是两次或四次

Oracle temporary table explanation

Locking relay ydb-100, 100V

技术人进阶画业务大图,手把手教学来了

初识 Flutter 的绘图组件 — CustomPaint

Windows环境下安装MongoDB数据库
随机推荐
实验八 T-sql,存储过程
解析创客教育实践中的智慧原理
自定义注解实现校验
[learning notes] structure
Locking relay ydb-100, 100V
TCP三次握手为什么不是两次或四次
Koa koa-combine-routers 分路由管理
(learning power + thinking power) x action power, summary of flywheel effect on the growth of technicians
问题解决:如何管理线程私有(thread_local)的指针变量
Mustache syntax
Sword finger offer 19 Regular Expression Matching
C#生成putty格式的ppk文件(支持passphrase)
None of the following candidates is applicable because of a receiver type mismatch
【学习笔记】构造
Join table query select generation
Exploring the road of steam education innovation in the Internet Era
Orb-slam2 source code learning (II) map initialization
写给 5000 粉丝的一封信!
Oracle data integrity
【go】go 实现行专列 将集合进行转列