RecyclerView usage record
As the function & requirements of the company's own app client become more and more complex, the layout of some pages becomes more and more complex. At the suggestion of former colleagues, use RecyclerView to implement it.
demand
The general requirements are shown in the figure below. The table layout needs to be displayed through the permissions of the login user.
programme
- Request the server login interface and return user information, role information, permissions, etc
- The APP end assembles the data according to the permission and transfers it to the Adapter
- Render view
realization
Add the following layout code to the appropriate layout of the home page layout
<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>
Define the item layout, representing the layout style of each item, 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
Define the Holder class and 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; } }
Define the adapter class and 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 is to write a public method 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 attributes
private RecyclerView recyclerView; private WorkBenchItemViewAdapter adapter; private List<WorkbenchMenuBean> listData = new ArrayList<>();
Define how to get a list
// Judging 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 Description", 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 processing method here is that these data are also returned through the server, and the client only processes them
Call other APIs of recyclerView
// Set 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 method
1. Get recyclerView content height
// Get recyclerView content height int recyclerViewRealHeight = recyclerView.computeVerticalScrollRange();
We use RecyclerView The height obtained by the getHeight method is the height of the RecyclerView control, not the content height
2. Get the total number of item s in the adapter
int size = recyclerView.getAdapter().getItemCount();
3. Get the number of item s visible to recyclerView
int childCount = recyclerView.getChildCount();
4. Get the actual position of an Item
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); //Gets its location in the adapter int position = params.getViewLayoutPosition(); // This way is also OK int position = recyclerView.getChildAdapterPosition(view);
5. Get the View of the corresponding Item according to the position. It should be noted that if the View corresponding to the current position is not visible, the obtained View is null.
// llm is the corresponding LayoutManager View itemView = llm.findViewByPosition(position);
6. Gets the position of the first visible Item
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. Gets the position of the first fully visible Item
int firstCompletelyVisibleItemPosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
8. Gets the position of the last visible Item
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. Gets the position of the last fully visible Item
int lastCompletelyVisibleItemPosition = ((LinearLayoutManager)parent.getLayoutManager()
reference material
Sharing plan
Blog content will be synchronized to Tencent cloud + community , we invite you to join us: https://cloud.tencent.com/
license agreement
This paper adopts Signature - non commercial use - share 4.0 international in the same way License agreement, please indicate the source for reprint.