当前位置:网站首页>Recyclerview usage record
Recyclerview usage record
2022-06-11 14:57:00 【Ant】
RecyclerView Use records
With the company's own use app Client function & More and more complex requirements , The layout of some pages is becoming more and more complex . At the suggestion of former colleagues , Use RecyclerView To achieve .
demand
The general requirements are shown in the figure below , The table layout needs to be displayed through the permissions of the logged in user .
programme
- Request the server login interface , Return user information 、 The role of information 、 Authority, etc
- APP The end assembles the data according to the permission and transfers it to Adapter
- Render view
Realization
Add the following layout code to the appropriate layout of the home page
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/workbench_recycleview"
android:divider="#00000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_bg"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp">
</androidx.recyclerview.widget.RecyclerView>Definition item Layout , For each item Layout style, etc
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="25dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" title "
android:id="@+id/wb_menu_txt_title"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#555555"/>
<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:id="@+id/wb_menu_img_title"
android:src="@mipmap/wk_ck"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="vertical"
android:layout_marginBottom="25dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=" This is a description "
android:id="@+id/wb_menu_txt_info"
android:textSize="14sp"
android:textColor="#999999"/>
</LinearLayout>
</LinearLayout>The preview effect is as follows
Definition Holder class , Inherit RecyclerView.ViewHolder
public class WorkBenchItemViewHolder extends RecyclerView.ViewHolder {
private final TextView txtTitle;
private final TextView txtInfo;
private final ImageView imgTitle;
public WorkBenchItemViewHolder(View view){
super(view);
txtTitle = view.findViewById(R.id.wb_menu_txt_title);
txtInfo = view.findViewById(R.id.wb_menu_txt_info);
imgTitle = view.findViewById(R.id.wb_menu_img_title);
}
public TextView getTxtInfo(){
return this.txtInfo;
}
public ImageView getImg(){
return this.imgTitle;
}
public TextView getTxtTitle(){
return txtTitle;
}
}
Definition Adapter class , Inherit RecyclerView.Adapter<WorkBenchItemViewHolder>
public class WorkBenchItemViewAdapter extends RecyclerView.Adapter<WorkBenchItemViewHolder> {
public List<WorkbenchMenuBean> mDatas;
private final Context mContext;
private OnItemClickListener listener;
public WorkBenchItemViewAdapter(Context context, List<WorkbenchMenuBean> datas) {
this.mContext = context;
this.mDatas = datas;
}
@NonNull
@Override
public WorkBenchItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new WorkBenchItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.home_workbench_item_view, parent,
false));
}
@Override
public void onBindViewHolder(WorkBenchItemViewHolder holder, final int position) {
holder.getTxtTitle().setText(mDatas.get(position).getTxtTitle());
holder.getTxtInfo().setText(mDatas.get(position).getTxtInfo());
holder.getImg().setImageResource(mDatas.get(position).getImgTitle());
if (listener != null) {
holder.itemView.setOnClickListener(view -> listener.onItemClick(view, position));
}
}
@Override
public int getItemCount() {
return mDatas.size();
}
// The second step , Write a public way
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
}Add processing logic at the corresponding page initialization page logic
Define related properties
private RecyclerView recyclerView; private WorkBenchItemViewAdapter adapter; private List<WorkbenchMenuBean> listData = new ArrayList<>();
Define the method to get the list
// Judge by the role obtained after login
// Here is a super account
boolean superAccount = loginInfo.isSuperAccount();
if (superAccount) {
listData.add(new WorkbenchMenuBean("pb", " function 1", " function 1 describe ", R.mipmap.wk_pb));
listData.add(new WorkbenchMenuBean("zj", " function 2", "...", R.mipmap.wk_zj));
listData.add(new WorkbenchMenuBean("sc", " function 3", "...", R.mipmap.wk_sc));
listData.add(new WorkbenchMenuBean("ck", " function 4", "...", R.mipmap.wk_ck));
listData.add(new WorkbenchMenuBean("sk", " function 5", "...", R.mipmap.wk_sk));
return;
}The better way to deal with this is these data It also returns through the server , The client just handles
call recyclerView Other api
// Set up LayoutManager
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
recyclerView.addItemDecoration(new CommonDecoration(Objects.requireNonNull(getContext())));
adapter = new WorkBenchItemViewAdapter(getContext(), listData);
// Set click event
adapter.setOnItemClickListener((view, position) -> {
WorkbenchMenuBean workbenchMenuBean = adapter.mDatas.get(position);
onWorkbenchMenuItemClick(workbenchMenuBean);
});
recyclerView.setAdapter(adapter);Common methods
1、 obtain recyclerView Content height
// obtain recyclerView Content height int recyclerViewRealHeight = recyclerView.computeVerticalScrollRange();
We go through recyclerView.getHeight The height obtained by the method is RecyclerView Height of control , Not content height
2、 obtain adapter Medium item Total number
int size = recyclerView.getAdapter().getItemCount();
3、 obtain recyclerView Visible item Number
int childCount = recyclerView.getChildCount();
4、 Get a Item Reality position
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); // Get its in adapter Position in int position = params.getViewLayoutPosition(); // This method can also int position = recyclerView.getChildAdapterPosition(view);
5、 according to position Get the corresponding Item Of View, It should be noted that , If at present position Corresponding View invisible , Acquired View by null.
// llm For the corresponding LayoutManager View itemView = llm.findViewByPosition(position);
6、 Get the first visible Item Of position
int firstPosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); // In this way, you can also get View childFirst = recyclerView.getChildAt(0); RecyclerView.LayoutParams paramsFirst = (RecyclerView.LayoutParams) childFirst.getLayoutParams(); int firstPosition1 = paramsFirst.getViewLayoutPosition();
7、 Get the first completely visible Item Of position
int firstCompletelyVisibleItemPosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
8、 Get the last visible Item Of position
int lastPosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findLastVisibleItemPosition(); // In this way, you can also get int childCount = recyclerView.getChildCount(); View childLast = recyclerView.getChildAt(childCount - 1); RecyclerView.LayoutParams paramsLast = (RecyclerView.LayoutParams) childLast.getLayoutParams(); int lastPosition1 = paramsLast.getViewLayoutPosition();
9、 Get the last completely visible Item Of position
int lastCompletelyVisibleItemPosition = ((LinearLayoutManager)parent.getLayoutManager()
Reference material
Sharing plans
Blog content will be synchronized to Tencent cloud + Community , Invite everyone to join us :https://cloud.tencent.com/
license agreement
In this paper A signature - Noncommercial use - Share in the same way 4.0 The international license agreement , Reprint please indicate the source .
边栏推荐
- Analyse approfondie de la conception du système relationnel du Groupe de cercles
- 大道至简 | 设计 ViT 到底怎么配置Self-Attention才是最合理的?
- Tencent interviewers share their interview experience, how to evaluate the interviewers' technical and personal comprehensive quality, and give you some suggestions on the interview
- [team learning] task06:for, if, and while
- Hot seek tiger, a list of eco economic models
- Hamad application layout scheme 05 of hashicopy (visit the web page)
- Illustration of tiger international quarterly report: revenue of USD 52.63 million continued to be internationalized
- 一些经典的嵌入式C面试题汇总
- Repository Manager之Nexus配置yum仓库
- 大道至簡 | 設計 ViT 到底怎麼配置Self-Attention才是最合理的?
猜你喜欢

Seven parameters of thread pool and reject policy

Sum of two leetcode numbers

大道至簡 | 設計 ViT 到底怎麼配置Self-Attention才是最合理的?

19. 二叉搜索树的插入删除修剪

你违规了吗?

树莓派知识大扫盲

Hot seek tiger, a list of eco economic models

Avenue to Jane | Comment concevoir un vit pour configurer l'auto - attraction est - il le plus raisonnable?

Database optimization

Elk log analysis system
随机推荐
当开源遇见 KPI,全球化 VS 本土化,开源的理想与现实该如何和解?
线程池的七个参数与拒绝策略
High number_ Chapter 6 infinite series__ Marklaurin series
深度解读:分布式系统韧性架构压舱石OpenChaos
Small open source projects based on stm32f1
Live800: several ways for intelligent customer service to improve customer experience
【Azure 应用服务】NodeJS Express + MSAL 实现API应用Token认证(AAD OAuth2 idToken)的认证实验 -- passport.authenticate()
Dynamically set the layoutinflater of the layout
How to play seek tiger, which has attracted much attention in the market?
In depth research and analysis report on ready to eat meat market for vacuum low temperature cooking in the world and China
In depth research and analysis report on global and Chinese spray drying machinery market
Avenue to Jane | Comment concevoir un vit pour configurer l'auto - attraction est - il le plus raisonnable?
leetcode每日一题——搜索插入位置
Why do I need the public static void main (string[] args) method?
汤峥嵘:CTO 是商业思维和技术思维交汇的那个点
Determine whether a string contains the specified string (verified)
化“被动”为“主动”,如何构建安全合规的智能产品 | Q推荐
Sum of two leetcode numbers
Analyse approfondie de la conception du système relationnel du Groupe de cercles
In depth research and analysis report on global and Chinese one component liquid rubber Market