当前位置:网站首页>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 .

原网站

版权声明
本文为[Ant]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012040060548.html