Set emptyview empty page by rewriting RecyclerView.Adapte

We know that the emergence of RecyclerView has allowed developers to customize the layout they want to a greater extent. RecyclerView can display different lists by introducing different ViewTypes. For example: instant messaging chat records generally have left The layout is the same as the layout on the right, then there are two different ViewTypes, and different ViewTypes are introduced according to different situations. (Haha, I have done this too.) Well, then, we can also introduce the layout according to our situation. For example, if the data is empty, can I introduce a layout like emptyView in our Adapter? .

package com.example.zq.recyclerviewdemo.module;
/**
 * 适配器,模拟列表
 */
public class EmptyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    
    

    private static final String TAG = "EmptyAdapter";

    private EmptyAdapter.onRecyclerViewListener onRecyclerViewListener;
    private List<Person> mList;

    /**
     * viewType--分别为item以及空view
     */
    public static final int VIEW_TYPE_ITEM = 1;
    public static final int VIEW_TYPE_EMPTY = 0;

    public EmptyAdapter(List<Person> datas) {
    
    
        mList = datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
    
        //在这里根据不同的viewType进行引入不同的布局
        if (viewType == VIEW_TYPE_EMPTY) {
    
    
            View emptyView = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view_tab, parent, false);

            return new RecyclerView.ViewHolder(emptyView) {
    
    

            };

        }
        //其他的引入正常的
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_item_person_info, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    
    
        if (holder instanceof MyViewHolder) {
    
    
            MyViewHolder hd = (MyViewHolder) holder;
            Person person = mList.get(position);
            hd.ivEditHead.setTag(position);
            hd.ivDelete.setTag(position);
            hd.tvPersonInfo.setText(person.getAddress());
            hd.ivEditHead.setImageResource(person.getHeadIconId());
            hd.ivEditHead.setOnClickListener(this);
            hd.ivDelete.setOnClickListener(this);
        }
    }

    @Override
    public int getItemCount() {
    
    
        //同时这里也需要添加判断,如果mData.size()为0的话,只引入一个布局,就是emptyView
        // 那么,这个recyclerView的itemCount为1
        if (mList.size() == 0) {
    
    
            return 1;
        }
        //如果不为0,按正常的流程跑
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
    
    
        //在这里进行判断,如果我们的集合的长度为0时,我们就使用emptyView的布局
        if (mList.size() == 0) {
    
    
            return VIEW_TYPE_EMPTY;
        }
        //如果有数据,则使用ITEM的布局
        return VIEW_TYPE_ITEM;
    }


    @Override
    public void onClick(View v) {
    
    
        switch (v.getId()) {
    
    
            case R.id.iv_edit_head:
                int position = (int) v.getTag();
                Log.i(TAG, "onClick: position:::" + position);
                onRecyclerViewListener.editHeadIcon(position);
                break;
            case R.id.iv_delete:
                int position1 = (int) v.getTag();
                onRecyclerViewListener.deleteInfo(position1);
                break;
            default:
                break;
        }
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
    
    

        ImageView ivEditHead;
        TextView tvPersonInfo;
        ImageView ivDelete;

        public MyViewHolder(View itemView) {
    
    
            super(itemView);
            ivEditHead = itemView.findViewById(R.id.iv_edit_head);
            tvPersonInfo = itemView.findViewById(R.id.tv_person_info);
            ivDelete = itemView.findViewById(R.id.iv_delete);
        }
    }

    public void setOnRecyclerViewListener(EmptyAdapter.onRecyclerViewListener onRecyclerViewListener) {
    
    
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    public interface onRecyclerViewListener {
    
    
        void editHeadIcon(int position);

        void deleteInfo(int position);
    }
}

When using the third method, when setting an empty layout, it is found that the layout_height="match_parent" of the child view in the RelativeLayout does not work.

Let's post the empty layout code first.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:gravity="center">


    <ImageView
        android:id="@+id/iv_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@mipmap/empty_view_tab" />

    <TextView
        android:id="@+id/empty_view_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv_empty"
        android:layout_gravity="center"
        android:text="暂无内容下拉刷新"
        android:textColor="@android:color/darker_gray"
        android:textSize="12sp" />

</RelativeLayout>

insert image description here
Hey, it’s so ugly, you will find that the android:layout_height="match_parent" in the root layout does not work. First of all, you don’t have to doubt that the height of the red area is not equal to the height of the item of the set child. This point I Already confirmed. In addition, if a height is specified (as long as it is not match_parent), then the layout will work. Looking for an answer from Du Niang, the previous article always said that it should be used when loading an empty layout

   View emptyView = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view_tab, parent, false);

instead of using

   View emptyView = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_view_tab, null);

etc.

In general, it is no problem to load the layout in this way, but I have already used the layout of inflate(R.layout.empty_view_tab, parent, false); above, but it still doesn't work.

Highlights:
Reasons for this situation:

In fact, it is not that the layout_height="match_parent" of the child view in RelativeLayout does not work, but the layout_height of relativeLayout does not work.

The height of the item of listview (we use RecyclerView here) is adapted to the maximum height of the accommodated view inside the item, that is to say, he cannot set the height by himself, which is similar to marginTop (or bottom) and does not work.

The match_parent of the subview is adapted to the maximum height of the parent, so the height of the subview becomes the maximum height of all subviews in the item, and finally everyone is wrap_parent.

Well, the article gives the answer, so I won't explain it anymore. So how can the third option be used normally?
Answer: Change the empty layout.


```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv_empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/empty_view_tab" />

        <TextView
            android:id="@+id/empty_view_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="暂无内容下拉刷新"
            android:textColor="@android:color/black"
            android:textSize="12sp" />

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

That's it, just add a TextView that can use the **android:layout_alignParentBottom="true"** attribute, so that the system will think that the height of our empty layout is the height of the filled screen. that's it.
If not, you can also use the android:drawableTop="@drawable/ic_launcher" attribute of TextView to add pictures.
insert image description here

I believe everyone will be thinking, which method is better to use depends on individual needs, but I prefer to use the second method, because Google has provided many good resources for us developers to use in the past two years. The hottest is some new controls in the support-design package, such as tabLayout, toolbar, CoordinatorLayout, etc., but if you want to better use some of their dazzling effects, you have to have a good understanding of the NestedScrollingParent interface. Google provides these The interface handles the processing of event distribution very well, and RecyclerView implements these interfaces, and can use its special effects well with support-design. What do you mean?
Simple and crude, that is, RecyclerView is used a lot, if you can move RecyclerView as little as possible, otherwise you may encounter other problems when using it in the future, so change the Adapter.

—————————————————
Copyright statement: This article is the original article of CSDN blogger "Roasted Kidney in the Wandering World", following the CC 4.0 BY-SA copyright agreement, please attach The link to the source of the original text and this statement.
Original link: https://blog.csdn.net/zhangqunshuai/article/details/81238767

Guess you like

Origin blog.csdn.net/huangerbian/article/details/131199679