当前位置:网站首页>Using recyclerreview to show banner is very simple
Using recyclerreview to show banner is very simple
2022-07-01 01:28:00 【Fahaxiki 丿】
Don't bullshit , Directly above

1. The layout is casual
<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. Build level 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. To configure
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);
// After the request interface is successful , Fill in the data
// Refresh adapter
emigratesList.addAll(data.getHwEmigrates());
emigratesAdapter.notifyDataSetChanged();4.Adapter , of no avail Bindding Of , Just revise it yourself
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 Layout The size of the layout is very important ( Be careful )
<?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=" Emigration + To study abroad , Complete your “ The American dream ”"/>
<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=" Emigration to the United States + Study abroad planning "/>
<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=" Studying in the United States "/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>边栏推荐
猜你喜欢

Q弹松软的大号吐司,带来更舒服的睡眠

neo4j安装、运行以及项目的构建和功能实现

ESP8266 RC522

基础知识之一——STA基础概述

Poor students can also play raspberry pie

Principes de formation de la programmation robotique

闭锁继电器YDB-100、100V
![Split the linked list [take next first and then cut the linked list to prevent chain breakage]](/img/eb/708ab20c13df75f4dbd2d6461d3602.png)
Split the linked list [take next first and then cut the linked list to prevent chain breakage]

Q play soft large toast to bring more comfortable sleep

Solve idea:class' xxx 'not found in module' xxx‘
随机推荐
Impact relay zc-23/dc220v
二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
visual studio 2019 快捷键备忘
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
Q play soft large toast to bring more comfortable sleep
解读创客教育所蕴含的科技素养
Inspire students' diversified thinking with steam Education
Call the classic architecture and build the model based on the classic
OCR的一些项目
生意和投资的思考
Open3D 点云包围盒
Q弹松软的大号吐司,带来更舒服的睡眠
StrictMode分析Activity泄漏-StrictMode原理(3)
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
One of the basics - overview of sta Basics
Kongyiji's first question: how much do you know about service communication?
XJY-220/43AC220V静态信号继电器
Solve idea:class' xxx 'not found in module' xxx‘
Docker 部署 MySQL 8
ORB-SLAM2源码学习(二)地图初始化