美文网首页高级UI
Android开发之RecyclerView实现多种item布局

Android开发之RecyclerView实现多种item布局

作者: 移动端_小刚哥 | 来源:发表于2019-10-14 15:20 被阅读0次

    无论是iOS开发还是Android开发,列表都是很常用的功能,几乎没有哪一个app是没有列表的,那么今天我就将我这两天学习的列表知识记录下来,与君共勉!

    我是iOS中途过来开发Android的,对比UITableView来学习会简单很多

    RecyclerView配合SpringView实现列表加下拉上拉

    RecyclerView和SpringView布局代码

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".message.fragment.MessageFragment">
    
        <!--消息模块列表的上拉和下拉-->
        <com.liaoinstan.springview.widget.SpringView
            android:id="@+id/springView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="50dp">
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/messageListView"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            </android.support.v7.widget.RecyclerView>
    
        </com.liaoinstan.springview.widget.SpringView>
    
    </FrameLayout>
    

    有内容的item布局

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    
    
    
        <!--标题-->
        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#333333"
            android:textSize="16sp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="15dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="100dp"
            android:lines="1"
            android:text="新资源提醒"
            />
    
        <TextView
            android:id="@+id/timeTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:lines="1"
            android:textColor="#b3b3b3"
            android:textSize="13sp"
            android:gravity="right"
            android:text="12月23日"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp"/>
    
    
    
    
        <TextView
            android:id="@+id/contentTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/titleTextView"
            android:layout_marginTop="10dp"
            android:textColor="#b3b3b3"
            android:textSize="13sp"
            android:text=""
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="15dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp"
            />
    
    
    
        <!--底部分割线-->
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_below="@+id/contentTextView"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="10dp"
            android:background="#e6e6e6"/>
    
    </RelativeLayout>
    

    无数据item

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
    
        <TextView
            android:layout_centerInParent="true"
            android:textSize="20dp"
            android:textColor="#333333"
            android:id="@+id/emptyText"
            android:textAlignment="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
    
        </TextView>
    
    
    
    </RelativeLayout>
    
    

    我们在fragment中获取RecyclerView对象

    recyclerView = contentView.findViewById(R.id.messageListView);
    

    给recyclerView赋值(有数据)

               List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
            Map<String,Object> map01 = new HashMap<>();
            map01.put("title","title-01");
            map01.put("content","内容");
            list.add(map01);
    
            Map<String,Object> map02 = new HashMap<>();
            map02.put("title","title-02");
            map02.put("content","内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
            list.add(map02);
    
            Map<String,Object> map03 = new HashMap<>();
            map03.put("title","title-03");
            map03.put("content","内容");
            list.add(map03);
    
            Map<String,Object> map04 = new HashMap<>();
            map04.put("title","title-04");
            map04.put("content","内容内容内容内容内容内容内容内容内容内容内容内容内容");
            list.add(map04);
    
            Map<String,Object> map05 = new HashMap<>();
            map05.put("title","title-05");
            map05.put("content","内容");
            list.add(map05);
    
            Map<String,Object> map06 = new HashMap<>();
            map06.put("title","title-06");
            map06.put("content","内容内容内容内容内容内容内容内容内容内容内容内容内容");
            list.add(map06);
    
            Map<String,Object> map07 = new HashMap<>();
            map07.put("title","title-07");
            map07.put("content","内容");
            list.add(map07);
    
            Map<String,Object> map08 = new HashMap<>();
            map08.put("title","title-08");
            map08.put("content","内容内容内容内容内容内容内容内容内容内容内容内容内容");
            list.add(map08);
    
            Map<String,Object> map09 = new HashMap<>();
            map09.put("title","title-09");
            map09.put("content","内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
            list.add(map09);
    
            messageAdapter = new MessageAdapter(list,getContext());
            recyclerView.setAdapter(messageAdapter);
    
            //线性
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
    
    haveData.jpg

    给recyclerView赋值(无数据)

            messageAdapter = new MessageAdapter(new ArrayList<Map<String,Object>>(),getContext());
            recyclerView.setAdapter(messageAdapter);
    
            //线性
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    
            recyclerView.setLayoutManager(linearLayoutManager);
    
    noData.jpg

    adapter

    package com.example.jizhigang.crm_android_j.message.adapter;
    
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import com.example.jizhigang.crm_android_j.R;
    
    import java.util.List;
    import java.util.Map;
    
    public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
    
        private int EMPTY_ITEM = 0; //无数据
        private int DEFAULT_ITEM = 1; //有数据
        private List<Map<String,Object>> messages;
        private Context context;
        public MessageAdapter(List<Map<String,Object>> messages, Context context){
            this.messages = messages;
            this.context = context;
        }
    
    
        /*给item一个唯一标识*/
        @Override
        public long getItemId(int position) {
            return super.getItemId(position);
        }
    
    
        /*如果是多种item,那么在这里判断是哪一种item*/
        @Override
        public int getItemViewType(int position) {
            if(messages.size() == 0){//展示无数据的item
                return EMPTY_ITEM;
            }else if(messages.size() > 0) {
                return DEFAULT_ITEM;
            }else {
                return super.getItemViewType(position);
            }
        }
    
    
        /*根据item类型去获取对应种类的item对象*/
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            if(i==EMPTY_ITEM){//无数据item
                View emptyView = LayoutInflater.from(context).inflate(R.layout.empty_recycler,viewGroup,false);
                return new EmptyHolder(emptyView);
            }else {//有数据item
                //获取cell对象
                View itemView = LayoutInflater.from(context).inflate(R.layout.message_view_item,viewGroup,false);
                return new MessageHolder(itemView);
            }
        }
    
    
        /*给对应种类的item赋值*/
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            if (viewHolder instanceof EmptyHolder){ //显示空item
                EmptyHolder emptyHolder = (EmptyHolder) viewHolder;
                emptyHolder.textView.setText("暂无数据");
            }else if (viewHolder instanceof MessageHolder){//显示正常item
                MessageHolder messageHolder = (MessageHolder) viewHolder;
                //获取model对象
                Map<String,Object> map = messages.get(i);
                String title = map.get("title").toString();
                String content = map.get("content").toString();
                //根据model对象给cell的组件赋值
                messageHolder.titleTextView.setText(title);
                messageHolder.contentTextView.setText(content);
            }
        }
    
    
        /*获取item数量*/
        @Override
        public int getItemCount() {
            if (messages.size()>0){
                //获得cell的数量
                return messages.size();
            }else { //显示无数据item
                return 1;
            }
    
        }
    
        /*有数据的holder*/
        public class MessageHolder extends RecyclerView.ViewHolder{
    
            private TextView titleTextView;
            private TextView contentTextView;
    
            //itemView实际上就是cell对象
            public MessageHolder(@NonNull View itemView) {
                super(itemView);
                //获得cell对象上的组件
                titleTextView = (TextView) itemView.findViewById(R.id.titleTextView);
                contentTextView = (TextView) itemView.findViewById(R.id.contentTextView);
            }
        }
    
    
        /*无数据的空item*/
        public class EmptyHolder extends RecyclerView.ViewHolder{
            private TextView textView;
            public EmptyHolder(@NonNull View itemView) {
                super(itemView);
                textView = (TextView)itemView.findViewById(R.id.emptyText);
            }
        }
    }
    
    

    需要注意的是getItemCount方法,没有数据要返回1

        /*获取item数量*/
        @Override
        public int getItemCount() {
            if (messages.size()>0){
                //获得cell的数量
                return messages.size();
            }else { //显示无数据item
                return 1;
            }
    
        }
    
    pull.jpg push.jpg

    相关文章

      网友评论

        本文标题:Android开发之RecyclerView实现多种item布局

        本文链接:https://www.haomeiwen.com/subject/jdnhuctx.html