Android RecycleView2 实现 不同类型ViewType显示、 HeadrView、FooterView 添加

MyAdapter 代码实现:

    1、 返回Item总数据        header.size+footer.size+datat.size
    @Override
    public int getItemCount() {
       return mFooterViewInfos.size()+mHeaderViewInfos.size() +datas.size();
    }
 

2、 getItemViewType 函数调用
    @Override
    public int getItemViewType(int position) {
     
        // 如下代码原理:
        // 假设getItemCount总数返回有12条  1条头  1条尾
        // adjPosition 那么等于 11     0< mHeaderViewInfos.size()  头条
        //  adjPosition = 1-1 =0       0<  10 条内容  返回ContextView
        //  adjPosition = 2-1 =1       1<  10
        // .......
        //   adjPosition = 11-1 =10    10< 10 否
        //  执行else  那么就 返回footerView

        // 12  - 10
        final int adjPosition = position - mHeaderViewInfos.size();    // 3 -0

        int adapterCount = 0;
        adapterCount = datas.size();
        if (position < mHeaderViewInfos.size()) {//是头部
            return TypeItem.TYPE_HEADER;
            // 3 < 10
        } else if (adjPosition < adapterCount) {  //2  是Center中间部分
            Person person = datas.get((position-mHeaderViewInfos.size()));

            if (person.getType() == TypeItem.TYPE_A) {
                return TypeItem.TYPE_A;
            } else if (person.getType() == TypeItem.TYPE_B) {
                return TypeItem.TYPE_B;
            } else {
                return TypeItem.TYPE_CONTEXT;
            }
        }else{
            // 返回footer
            return  TypeItem.TYPE_FOOTER;
        }
    } 
    3、onCreateViewHolder  根据传入的ViewType创建不同类型ViewHolder
    4、 onBindViewHolder:  根据不同类型ViewType处理datas数

MyAdapter 核心代码实现:
package mk.denganzhi.com.recyclefirstdemo_header;

import android.content.Context;
import android.preference.PreferenceActivity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yxm2909 on 2016/7/13.
 */
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "MyAdapter";
    private List<Person> datas;
    private LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;

    public interface OnItemClickListener {
        public void onClick(View parent, int position);
    }

    public interface OnItemLongClickListener {
        public boolean onLongClick(View parent, int position);
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        this.mOnItemClickListener = l;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener l) {
        this.mOnItemLongClickListener = l;
    }


    private ArrayList<View> mHeaderViewInfos =null;
    private ArrayList<View> mFooterViewInfos = null;

    public MyAdapter( List<Person> datas) {
        this.datas = datas;

    }

    public MyAdapter(Context context, List<Person> datas,ArrayList<View> headerViewInfos,
                     ArrayList<View> footerViewInfos) {
        if (headerViewInfos == null) {
            mHeaderViewInfos = new ArrayList<View>();
        } else {
            mHeaderViewInfos = headerViewInfos;
        }

        if (footerViewInfos == null) {
            mFooterViewInfos = new ArrayList<View>();
        } else {
            mFooterViewInfos = footerViewInfos;
        }


        this.datas = datas;
        inflater = LayoutInflater.from(context);
    }

    // 中间的ViewHolder显示
    public class ContextViewHolder extends RecyclerView.ViewHolder {
        private View itemView;
        private TextView title;
        CheckBox checkBox;


        public ContextViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            title = (TextView) itemView.findViewById(R.id.rv_main_item_title);
            checkBox=  itemView.findViewById(R.id.rv_main_checkbox);

        }
    }


    class AViewHolder extends  RecyclerView.ViewHolder{
        private TextView textViewA;
        public AViewHolder(View itemView) {
            super(itemView);
            textViewA = (TextView) itemView.findViewById(R.id.item_a);
        }
    }
    class BViewHolder extends  RecyclerView.ViewHolder{
        private TextView textViewB;
        public BViewHolder(View itemView) {
            super(itemView);
            textViewB = (TextView) itemView.findViewById(R.id.item_b);
        }
    }

    class HeaderViewHolder extends  RecyclerView.ViewHolder{
        public HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }
    class FooterViewHolder extends  RecyclerView.ViewHolder{
        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }


    ContextViewHolder contextHolder=null;

    AViewHolder aViewHolder=null;
    BViewHolder bViewHolder=null;

   // 头
    HeaderViewHolder  headerViewHolder;
    FooterViewHolder  footerViewHolder;


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.e(TAG, "onCreateViewHolder:"+ viewType);


        if(TypeItem.TYPE_A == viewType){
            aViewHolder=new AViewHolder(inflater.inflate(R.layout.type_a, parent, false));
              return aViewHolder;
             // B 条目
        }else if(TypeItem.TYPE_B == viewType){

            bViewHolder=new BViewHolder(inflater.inflate(R.layout.type_b, parent, false));

            return bViewHolder;
        }else if(TypeItem.TYPE_HEADER == viewType){

            headerViewHolder=new HeaderViewHolder(inflater.inflate(R.layout.hader, parent, false));

            return headerViewHolder;
        }else if(TypeItem.TYPE_FOOTER == viewType){

            footerViewHolder=new FooterViewHolder(inflater.inflate(R.layout.footer, parent, false));

            return footerViewHolder;
        }else  {
            contextHolder  = new ContextViewHolder(inflater.inflate(R.layout.rv_main_item, parent, false));
            return contextHolder;
        }


    }



    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        Person person=null;

        if(holder.getItemViewType() == TypeItem.TYPE_HEADER || holder.getItemViewType() ==TypeItem.TYPE_FOOTER){

        }else{
            person = datas.get((position-mHeaderViewInfos.size()));
        }



        // 数据绑定

        if(holder.getItemViewType() == TypeItem.TYPE_A){

            aViewHolder= (AViewHolder) holder;

            aViewHolder.textViewA.setText(person.getName());

        }else if(holder.getItemViewType()==TypeItem.TYPE_B){

            bViewHolder= (BViewHolder) holder;

            bViewHolder.textViewB.setText(person.getName());

        }else if(holder.getItemViewType()==TypeItem.TYPE_HEADER){

            headerViewHolder= (HeaderViewHolder) holder;

           // bViewHolder.textViewB.setText(person.getName());

        }
        else if(holder.getItemViewType()==TypeItem.TYPE_FOOTER){

            footerViewHolder= (FooterViewHolder) holder;

          //  bViewHolder.textViewB.setText(person.getName());

        }else{

            contextHolder = (ContextViewHolder)holder;
            Log.e(TAG, "set value to item:" + position);
            contextHolder.title.setText(person.getName());
            if (mOnItemClickListener != null) {
                contextHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int pos = contextHolder.getLayoutPosition();
                        mOnItemClickListener.onClick(contextHolder.itemView, pos);
                    }
                });
            }
            if (mOnItemLongClickListener != null) {
                contextHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        int pos = contextHolder.getLayoutPosition();
                        mOnItemLongClickListener.onLongClick(contextHolder.itemView, pos);
                        return false;
                    }
                });
            }

        }

    }


// 这里返回的Type传递给创建OnViewHolder的时候
    @Override
    public int getItemViewType(int position) {

        // 假设getItemCount总数返回有12条  1条头  1条尾
        // adjPosition 那么等于 11     0< mHeaderViewInfos.size()  头条
        //  adjPosition = 1-1 =0       0<  10 条内容  返回ContextView
        //  adjPosition = 2-1 =1       1<  10
        // .......
        //   adjPosition = 11-1 =10    10< 10 否
        //  执行else  那么就 返回footerView

        // 12  - 10
        final int adjPosition = position - mHeaderViewInfos.size();    // 3 -0

        int adapterCount = 0;
        adapterCount = datas.size();
        if (position < mHeaderViewInfos.size()) {//是头部
            return TypeItem.TYPE_HEADER;
            // 3 < 10
        } else if (adjPosition < adapterCount) {  //2  是Center中间部分
            Person person = datas.get((position-mHeaderViewInfos.size()));

            if (person.getType() == TypeItem.TYPE_A) {
                return TypeItem.TYPE_A;
            } else if (person.getType() == TypeItem.TYPE_B) {
                return TypeItem.TYPE_B;
            } else {
                return TypeItem.TYPE_CONTEXT;
            }
        }else{
            // 返回footer
            return  TypeItem.TYPE_FOOTER;
        }
    }

    @Override
    public int getItemCount() {
       return mFooterViewInfos.size()+mHeaderViewInfos.size() +datas.size();
    }



}
MainActivity 代码实
package mk.denganzhi.com.recyclefirstdemo_header;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;

    private WrapperRecycle wrapperRecycle;

    MyAdapter mMyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_main);
       //  默认垂直滚动
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        View headerView1=View.inflate(this,R.layout.hader,null);
        ArrayList<View> headerViewInfos=new ArrayList<>();
        headerViewInfos.add(headerView1);


        ArrayList<View> footerViewInfos=new ArrayList<>();
        View footerView1=View.inflate(this,R.layout.footer,null);
        footerViewInfos.add(footerView1);


        mMyAdapter = new MyAdapter(MainActivity.this, initData(),headerViewInfos,footerViewInfos);
        mMyAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onClick(View parent, int position) {

            }
        });
        mMyAdapter.setOnItemLongClickListener(new MyAdapter.OnItemLongClickListener() {
            @Override
            public boolean onLongClick(View parent, int position) {

                return false;
            }
        });
        mRecyclerView.setAdapter(mMyAdapter);
    }

    private List<Person> initData() {
        List<Person> datas = new ArrayList<Person>();
        for (int i = 0; i <= 100; i++) {
            if(i%3==0){
                datas.add(new Person(i,"小布什:"+i,TypeItem.TYPE_A));
            }else if(i%5==0){
                datas.add(new Person(i,"奥巴马:"+i,TypeItem.TYPE_B));
            }else{
                datas.add(new Person(i,"特兰普:"+i,TypeItem.TYPE_CONTEXT));
            }

        }
        return datas;
    }
}
/**/
activity_main.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="mk.denganzhi.com.recyclefirstdemo_header.MainActivity">

    <!--  android:background="#000000" -->

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >

    </android.support.v7.widget.RecyclerView>

  

</LinearLayout>

     5. 模仿ListView实现 AddHeader、AddFooter
        实现原理: 
        定义 WrapperRecycle 继承RecyclerView ,定义 addHeaderView 、addFooterView 保存 header、footer
        
        setMyAdapter 的时候刷新新adapdter, 
        问题? : 会调用2次 setAdapter

WrapperRecycle 代码:
package mk.denganzhi.com.recyclefirstdemo_header;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by denganzhi on 2020/3/4.
 */

public class WrapperRecycle extends RecyclerView {


    private ArrayList<View> mHeaderViewInfos = new ArrayList<View>();
    private ArrayList<View> mFooterViewInfos = new ArrayList<View>();
    private Adapter mAdapter;

    public WrapperRecycle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void addHeaderView(View v) {
        mHeaderViewInfos.add(v);


    }

    public void addFooterView(View v) {
        mFooterViewInfos.add(v);

    }


    public void setMyAdapter(Activity activity, List<Person> datass,Adapter adapter) {
        if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) {
            mAdapter = new MyAdapter( activity, datass,mHeaderViewInfos,mFooterViewInfos);
        } else {
            mAdapter = adapter;
        }
        setAdapter(mAdapter);
    }
}
MainActivity 实现代码:
package mk.denganzhi.com.recyclefirstdemo_header;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;

    private WrapperRecycle wrapperRecycle;

    MyAdapter mMyAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wrapperRecycle = (WrapperRecycle) findViewById(R.id.rv_main1);
        wrapperRecycle.setLayoutManager(new LinearLayoutManager(this));
        View headerView1=View.inflate(this,R.layout.hader,null);
        View footerView1=View.inflate(this,R.layout.footer,null);
  // 添加headerView
        wrapperRecycle.addHeaderView(headerView1);
// 添加footerView
        wrapperRecycle.addFooterView(footerView1);
// 第一个newAdapter
        MyAdapter adapter = new MyAdapter(initData());
// setAdapter内部还会调用内部带有header、footerview的adapter
//  new Adapter以后Adapter内部会回调  
// getItemCount 、onCreateViewHolder、onBindViewHolder 对应方法
        wrapperRecycle.setMyAdapter(MainActivity.this, initData(),adapter);
    }

    private List<Person> initData() {
        List<Person> datas = new ArrayList<Person>();
        for (int i = 0; i <= 100; i++) {
            if(i%3==0){
                datas.add(new Person(i,"小布什:"+i,TypeItem.TYPE_A));
            }else if(i%5==0){
                datas.add(new Person(i,"奥巴马:"+i,TypeItem.TYPE_B));
            }else{
                datas.add(new Person(i,"特兰普:"+i,TypeItem.TYPE_CONTEXT));
            }

        }
        return datas;
    }
}
/**/

  上图看效果:

  源码下载:https://download.csdn.net/download/dreams_deng/12234702 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值