当前位置:网站首页>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>
边栏推荐
- uniapp官方组件点击item无效,解决方案
- Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images
- Open3D 点云颜色渲染
- TypeError: Argument ‘angle‘ can not be treated as a double
- DC學習筆記正式篇之零——綜述與基本流程介紹
- [Deepin] 常用集合
- JS to convert numbers into Chinese characters for output
- ORB-SLAM2源码学习(二)地图初始化
- 1175. Prime Arrangements
- 【学习笔记】倍增 + 二分
猜你喜欢
Introduction and principle analysis of cluster and LVS
[go] go implements row column conversion of sets
Docker 部署 MySQL 8
Interpreting the scientific and technological literacy contained in maker Education
2021电赛F题openmv和K210调用openmv api巡线,完全开源。
DC學習筆記正式篇之零——綜述與基本流程介紹
WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
数字IC设计流程总结
图的连通性基础
蒹葭苍苍,白露为霜。
随机推荐
Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base
OCR的一些项目
Analyze the maker education path integrating the essence of discipline
使用 C# 创造 ASCII 艺术
Docker deployment MySQL 8
3dsmax插件开发遍历节点对象和Object获取及INode变换矩阵说明
The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
TypeError: Argument ‘angle‘ can not be treated as a double
生意和投资的思考
MFC TCP通信服务端客户端Demo备忘vs2019
双位置继电器DLS-5/2 DC220V
About vctk datasets
【Qt5-基础篇】随机数显示屏展示
【学习笔记】简单dp
None of the following candidates is applicable because of a receiver type mismatch
Orb-slam2 source code learning (II) map initialization
C# 自定义并动态切换光标
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
分割链表[先取next再斩断链表防止断链]
Visual studio 2019 shortcut notes