RecyclerView的多条目和单列双列的实现

用RecycleView来实现布局形式,默认只能指定一种布局格式,但是实际中我们的布局经常会用到多种类型的布局方式。如何实现呢?

今天来说下常用的2钟方式。

第一种:

通过自定义addHeadView方法来添加头布局

RecycleViewWithHead.Java

[java] view plain copy
print ?
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.support.v7.widget.GridLayoutManager;  
  4. import android.support.v7.widget.GridLayoutManager.SpanSizeLookup;  
  5. import android.support.v7.widget.RecyclerView;  
  6. import android.view.View;  
  7.   
  8. public class RecycleViewWithHead extends Activity {  
  9.   
  10.     private RecyclerView rcv;  
  11.     // 当前的条目是recyclerView的头布局  
  12.     public static final int HEADER_RECYCLER_VIEW_ITEM = 0;  
  13.     // 当前的条目是普通recyclerView的条目  
  14.     public static final int NORMAL_RECYCLER_VIEW_ITEM = 1;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         // TODO Auto-generated method stub  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_recycle);  
  21.         rcv = (RecyclerView) findViewById(R.id.rcv);  
  22.         // 设置布局管理  
  23.         GridLayoutManager manager = new GridLayoutManager(this2);  
  24.         // 设置布局管理一条数据占用几行,如果是头布局则头布局自己占用一行  
  25.         manager.setSpanSizeLookup(new SpanSizeLookup() {  
  26.             @Override  
  27.             public int getSpanSize(int postion) {  
  28.                 if (postion == 0) {  
  29.                     return 2;  
  30.                 } else {  
  31.                     return 1;  
  32.                 }  
  33.             }  
  34.         });  
  35.         rcv.setLayoutManager(manager);  
  36.         MyRecycleAdapter adapter = new MyRecycleAdapter(  
  37.                 RecycleViewWithHead.this20);  
  38.         View view = View.inflate(this, R.layout.head, null);  
  39.         adapter.addHeadView(view);  
  40.         rcv.setAdapter(adapter);  
  41.     }  
  42. }  
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.GridLayoutManager.SpanSizeLookup;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class RecycleViewWithHead extends Activity {

    private RecyclerView rcv;
    // 当前的条目是recyclerView的头布局
    public static final int HEADER_RECYCLER_VIEW_ITEM = 0;
    // 当前的条目是普通recyclerView的条目
    public static final int NORMAL_RECYCLER_VIEW_ITEM = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle);
        rcv = (RecyclerView) findViewById(R.id.rcv);
        // 设置布局管理
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        // 设置布局管理一条数据占用几行,如果是头布局则头布局自己占用一行
        manager.setSpanSizeLookup(new SpanSizeLookup() {
            @Override
            public int getSpanSize(int postion) {
                if (postion == 0) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
        rcv.setLayoutManager(manager);
        MyRecycleAdapter adapter = new MyRecycleAdapter(
                RecycleViewWithHead.this, 20);
        View view = View.inflate(this, R.layout.head, null);
        adapter.addHeadView(view);
        rcv.setAdapter(adapter);
    }
}


布局文件activity_recycle.xml

[html] view plain copy
print ?
  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=“http://schemas.android.com/tools”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”>  
  5.   
  6.   
  7.     <android.support.v7.widget.RecyclerView  
  8.         android:id=“@+id/rcv”  
  9.         android:layout_width=“match_parent”  
  10.         android:layout_height=“wrap_content” />  
  11.   
  12.   
  13. RelativeLayout>  



    



头布局文件head.xml

[html] view plain copy
print ?
  1. xml version=“1.0” encoding=“utf-8”?>  
  2. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent” >  
  5.   
  6.     <ImageView  
  7.         android:id=“@+id/iv_head”  
  8.         android:layout_width=“80dp”  
  9.         android:layout_height=“80dp”  
  10.         android:layout_margin=“10dp”  
  11.         android:src=“@drawable/head” />  
  12.   
  13.     <TextView  
  14.         android:id=“@+id/tv_head”  
  15.         android:layout_width=“wrap_content”  
  16.         android:layout_height=“wrap_content”  
  17.         android:layout_alignTop=“@+id/iv_head”  
  18.         android:layout_toRightOf=“@+id/iv_head”  
  19.         android:text=“这是一张熊猫的图片” />  
  20.   
  21.     <ImageView  
  22.         android:id=“@+id/iv_head_bottom”  
  23.         android:layout_width=“wrap_content”  
  24.         android:layout_height=“wrap_content”  
  25.         android:layout_alignBottom=“@+id/iv_head”  
  26.         android:layout_toRightOf=“@+id/iv_head”  
  27.         android:src=“@drawable/type” />  
  28.   
  29. RelativeLayout>  



    

    

    



适配器MyRecycleAdapter.java

[java] view plain copy
print ?
  1. import android.content.Context;  
  2. import android.support.v7.widget.RecyclerView;  
  3. import android.support.v7.widget.RecyclerView.ViewHolder;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.ViewGroup;  
  8. import android.widget.Toast;  
  9.   
  10. public class MyRecycleAdapter extends  
  11.         RecyclerView.Adapter {  
  12.   
  13.     private View headView;  
  14.     private Context mContext;  
  15.     private int count;  
  16.   
  17.     MyRecycleAdapter(Context mContext, int count) {  
  18.         this.count = count;  
  19.         this.mContext = mContext;  
  20.     }  
  21.   
  22.     /** 
  23.      * 设置数据源总的条目 
  24.      */  
  25.     @Override  
  26.     public int getItemCount() {  
  27.         //返回条目数加头布局个数  
  28.         return count + 1;  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onBindViewHolder(MyRecycleAdapterHolder holder,  
  33.             final int position) {  
  34.         int itemViewType = getItemViewType(position);  
  35.         // 头部  
  36.         if (itemViewType == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {  
  37.             return;  
  38.         } else if (itemViewType == RecycleViewWithHead.NORMAL_RECYCLER_VIEW_ITEM) {//普通条目  
  39.             holder.iv_item_icon.setOnClickListener(new OnClickListener() {  
  40.                 @Override  
  41.                 public void onClick(View v) {  
  42.                     Toast.makeText(mContext, (position - 1) + “”0).show();  
  43.                 }  
  44.             });  
  45.         }  
  46.   
  47.     }  
  48.   
  49.     @Override  
  50.     public MyRecycleAdapterHolder onCreateViewHolder(ViewGroup parent,  
  51.             int viewType) {  
  52.         View root = null;  
  53.         if (viewType == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {  
  54.             root = headView;  
  55.         } else {  
  56.             root = LayoutInflater.from(mContext).inflate(R.layout.item, parent,  
  57.                     false);  
  58.         }  
  59.         return new MyRecycleAdapterHolder(root, viewType);  
  60.     }  
  61.   
  62.     /** 
  63.      * 添加自定义头部 
  64.      */  
  65.     public void addHeadView(View view) {  
  66.         this.headView = view;  
  67.     }  
  68.   
  69.     @Override  
  70.     public int getItemViewType(int position) {  
  71.   
  72.         if (position == 0) {  
  73.             return RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM;  
  74.         } else {  
  75.             return RecycleViewWithHead.NORMAL_RECYCLER_VIEW_ITEM;  
  76.         }  
  77.     }  
  78. }  
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

public class MyRecycleAdapter extends
        RecyclerView.Adapter {

    private View headView;
    private Context mContext;
    private int count;

    MyRecycleAdapter(Context mContext, int count) {
        this.count = count;
        this.mContext = mContext;
    }

    /**
     * 设置数据源总的条目
     */
    @Override
    public int getItemCount() {
        //返回条目数加头布局个数
        return count + 1;
    }

    @Override
    public void onBindViewHolder(MyRecycleAdapterHolder holder,
            final int position) {
        int itemViewType = getItemViewType(position);
        // 头部
        if (itemViewType == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {
            return;
        } else if (itemViewType == RecycleViewWithHead.NORMAL_RECYCLER_VIEW_ITEM) {//普通条目
            holder.iv_item_icon.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, (position - 1) + "", 0).show();
                }
            });
        }

    }

    @Override
    public MyRecycleAdapterHolder onCreateViewHolder(ViewGroup parent,
            int viewType) {
        View root = null;
        if (viewType == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {
            root = headView;
        } else {
            root = LayoutInflater.from(mContext).inflate(R.layout.item, parent,
                    false);
        }
        return new MyRecycleAdapterHolder(root, viewType);
    }

    /**
     * 添加自定义头部
     */
    public void addHeadView(View view) {
        this.headView = view;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM;
        } else {
            return RecycleViewWithHead.NORMAL_RECYCLER_VIEW_ITEM;
        }
    }
}


普通条目的布局文件item.xml

[html] view plain copy
print ?
  1. xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:id=“@+id/ll_list”  
  4.     android:layout_width=“wrap_content”  
  5.     android:layout_height=“wrap_content”  
  6.     android:layout_marginLeft=“2dp”  
  7.     android:layout_marginRight=“2dp”  
  8.     android:layout_marginTop=“5dp”  
  9.     android:orientation=“vertical” >  
  10.   
  11.     <ImageView  
  12.         android:id=“@+id/iv_item_icon”  
  13.         android:layout_width=“wrap_content”  
  14.         android:layout_height=“wrap_content”  
  15.         android:src=“@drawable/item” />  
  16.   
  17.     <TextView  
  18.         android:id=“@+id/tv_item”  
  19.         android:layout_width=“wrap_content”  
  20.         android:layout_height=“wrap_content”  
  21.         android:layout_gravity=“center_horizontal”  
  22.         android:layout_marginTop=“5dp”  
  23.         android:text=“这是一只熊猫” />  
  24.   
  25. LinearLayout>  



    

    




普通条目的ViewHolder

[java] view plain copy
print ?
  1. import android.support.v7.widget.RecyclerView;  
  2. import android.view.View;  
  3. import android.widget.ImageView;  
  4. import android.widget.TextView;  
  5.   
  6. public class MyRecycleAdapterHolder extends RecyclerView.ViewHolder{  
  7.       
  8.     public ImageView iv_item_icon;  
  9.     public TextView tv_item;  
  10.     public MyRecycleAdapterHolder(View itemView) {  
  11.         super(itemView);  
  12.     }  
  13.     public MyRecycleAdapterHolder(View itemView,int viewType) {  
  14.         super(itemView);  
  15.         initView(itemView,viewType);  
  16.     }  
  17.     private void initView(View itemView, int viewType) {  
  18.         iv_item_icon = (ImageView) itemView.findViewById(R.id.iv_item_icon);  
  19.         tv_item = (TextView) itemView.findViewById(R.id.tv_item);  
  20.     }  
  21.   
  22. }  
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MyRecycleAdapterHolder extends RecyclerView.ViewHolder{

    public ImageView iv_item_icon;
    public TextView tv_item;
    public MyRecycleAdapterHolder(View itemView) {
        super(itemView);
    }
    public MyRecycleAdapterHolder(View itemView,int viewType) {
        super(itemView);
        initView(itemView,viewType);
    }
    private void initView(View itemView, int viewType) {
        iv_item_icon = (ImageView) itemView.findViewById(R.id.iv_item_icon);
        tv_item = (TextView) itemView.findViewById(R.id.tv_item);
    }

}

效果图:

RecyclerView的多条目和单列双列的实现_第1张图片




还有一种情况类似于淘宝的商品展示我们可以切换每行显示的数量,其实也很简单

先看下要实现的效果:

RecyclerView的多条目和单列双列的实现_第2张图片

我先说下大致的实现思路:

1.给adapter设置一个当前显示多行还是单行的标记。

2.每次切换视图时重置标记,并重置RecycleView的LayoutManager。

3.调用adapter.notifyItemRangeChanged(2, adapter.getItemCount());(第一个参数是动画开始的位置索引)

好了再来看下RecycleViewWithHead.java

[java] view plain copy
print ?
  1. import com.example.myrecycleviewdemo.adapter.MyRecycleAdapter;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.support.v7.widget.GridLayoutManager;  
  5. import android.support.v7.widget.GridLayoutManager.SpanSizeLookup;  
  6. import android.support.v7.widget.RecyclerView;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.ImageView;  
  10.   
  11. public class RecycleViewWithHead extends Activity implements OnClickListener {  
  12.   
  13.     private RecyclerView rcv;  
  14.     // 当前的条目是recyclerView的头布局  
  15.     public static final int HEADER_RECYCLER_VIEW_ITEM = 0;  
  16.     // 当前的条目是普通recyclerView的条目  
  17.     public static final int NORMAL_RECYCLER_VIEW_ITEM = 1;  
  18.     // 一行显示一个  
  19.     public static final int RECYCLER_VIEW_ITEM_SINGLE = 3;  
  20.     // 一行显示两个  
  21.     public static final int RECYCLER_VIEW_ITEM_DOUBLE = 4;  
  22.     private ImageView iv_switch;// 视图转换  
  23.     private MyRecycleAdapter adapter;  
  24.     private GridLayoutManager manager;  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         // TODO Auto-generated method stub  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_recycle);  
  31.         rcv = (RecyclerView) findViewById(R.id.rcv);  
  32.         iv_switch = (ImageView) findViewById(R.id.iv_switch);  
  33.         iv_switch.setOnClickListener(this);  
  34.         manager = new GridLayoutManager(this2);  
  35.         // 设置布局管理一条数据占用几行,如果是头布局则头布局自己占用一行  
  36.         manager.setSpanSizeLookup(new SpanSizeLookup() {  
  37.             @Override  
  38.             public int getSpanSize(int postion) {  
  39.                 if (postion == 0) {  
  40.                     return 2;  
  41.                 } else {  
  42.                     return 1;  
  43.                 }  
  44.             }  
  45.         });  
  46.         rcv.setLayoutManager(manager);  
  47.         adapter = new MyRecycleAdapter(RecycleViewWithHead.this20);  
  48.         View view = View.inflate(this, R.layout.head, null);  
  49.         // 设置当前ViewType  
  50.         adapter.setSpanSize(RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE);  
  51.         adapter.addHeadView(view);  
  52.         rcv.setAdapter(adapter);  
  53.     }  
  54.   
  55.     @Override  
  56.     public void onClick(View v) {  
  57.         switch (v.getId()) {  
  58.         case R.id.iv_switch:  
  59.             changeRecycleViewList();  
  60.             break;  
  61.   
  62.         default:  
  63.             break;  
  64.         }  
  65.   
  66.     }  
  67.   
  68.     /** 
  69.      * 改变RecycleView的显示列数 
  70.      */  
  71.     private void changeRecycleViewList() {  
  72.         if (adapter != null) {  
  73.             int spanSize = adapter.getSpanSize();  
  74.             // 当前一行显示一列  
  75.             if (spanSize == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE) {  
  76.                 manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {  
  77.                     @Override  
  78.                     public int getSpanSize(int position) {  
  79.                         if (adapter.getItemViewType(position) == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {  
  80.                             return 2;  
  81.                         } else {  
  82.                             return 1;  
  83.   
  84.                         }  
  85.                     }  
  86.                 });  
  87.                 adapter.setSpanSize(RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE);  
  88.             }  
  89.             // 当前一行显示两列  
  90.             else if (spanSize == RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE) {  
  91.                 manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {  
  92.                     @Override  
  93.                     public int getSpanSize(int position) {  
  94.                         if (adapter.getItemViewType(position) == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {  
  95.                             return 2;  
  96.                         } else {  
  97.                             return 2;  
  98.                         }  
  99.                     }  
  100.                 });  
  101.                 adapter.setSpanSize(RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE);  
  102.             }  
  103.             // 第一个参数是动画开始的位置索引  
  104.             adapter.notifyItemRangeChanged(2, adapter.getItemCount());  
  105.         }  
  106.     }  
  107. }  
import com.example.myrecycleviewdemo.adapter.MyRecycleAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.GridLayoutManager.SpanSizeLookup;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class RecycleViewWithHead extends Activity implements OnClickListener {

    private RecyclerView rcv;
    // 当前的条目是recyclerView的头布局
    public static final int HEADER_RECYCLER_VIEW_ITEM = 0;
    // 当前的条目是普通recyclerView的条目
    public static final int NORMAL_RECYCLER_VIEW_ITEM = 1;
    // 一行显示一个
    public static final int RECYCLER_VIEW_ITEM_SINGLE = 3;
    // 一行显示两个
    public static final int RECYCLER_VIEW_ITEM_DOUBLE = 4;
    private ImageView iv_switch;// 视图转换
    private MyRecycleAdapter adapter;
    private GridLayoutManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle);
        rcv = (RecyclerView) findViewById(R.id.rcv);
        iv_switch = (ImageView) findViewById(R.id.iv_switch);
        iv_switch.setOnClickListener(this);
        manager = new GridLayoutManager(this, 2);
        // 设置布局管理一条数据占用几行,如果是头布局则头布局自己占用一行
        manager.setSpanSizeLookup(new SpanSizeLookup() {
            @Override
            public int getSpanSize(int postion) {
                if (postion == 0) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
        rcv.setLayoutManager(manager);
        adapter = new MyRecycleAdapter(RecycleViewWithHead.this, 20);
        View view = View.inflate(this, R.layout.head, null);
        // 设置当前ViewType
        adapter.setSpanSize(RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE);
        adapter.addHeadView(view);
        rcv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.iv_switch:
            changeRecycleViewList();
            break;

        default:
            break;
        }

    }

    /**
     * 改变RecycleView的显示列数
     */
    private void changeRecycleViewList() {
        if (adapter != null) {
            int spanSize = adapter.getSpanSize();
            // 当前一行显示一列
            if (spanSize == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE) {
                manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        if (adapter.getItemViewType(position) == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {
                            return 2;
                        } else {
                            return 1;

                        }
                    }
                });
                adapter.setSpanSize(RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE);
            }
            // 当前一行显示两列
            else if (spanSize == RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE) {
                manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        if (adapter.getItemViewType(position) == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {
                            return 2;
                        } else {
                            return 2;
                        }
                    }
                });
                adapter.setSpanSize(RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE);
            }
            // 第一个参数是动画开始的位置索引
            adapter.notifyItemRangeChanged(2, adapter.getItemCount());
        }
    }
}


布局文件activity_recycle.xml

[html] view plain copy
print ?
  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=“http://schemas.android.com/tools”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”>  
  5.   
  6.     <android.support.v7.widget.RecyclerView  
  7.         android:id=“@+id/rcv”  
  8.         android:layout_width=“match_parent”  
  9.         android:layout_height=“wrap_content” />  
  10.       
  11.     <ImageView   
  12.         android:id=“@+id/iv_switch”  
  13.         android:layout_width=“wrap_content”  
  14.         android:layout_height=“wrap_content”  
  15.         android:layout_alignParentTop=“true”  
  16.         android:layout_alignParentRight=“true”  
  17.         android:layout_margin=“20dp”  
  18.         android:src=“@drawable/more”  
  19.         />  
  20.   
  21. RelativeLayout>  


    

    


适配器MyRecycleAdapter.java

[java] view plain copy
print ?
  1. import com.example.myrecycleviewdemo.R;  
  2. import com.example.myrecycleviewdemo.RecycleViewWithHead;  
  3. import com.example.myrecycleviewdemo.R.layout;  
  4. import android.content.Context;  
  5. import android.support.v7.widget.RecyclerView;  
  6. import android.support.v7.widget.RecyclerView.ViewHolder;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup;  
  11. import android.widget.Toast;  
  12.   
  13. public class MyRecycleAdapter extends  
  14.         RecyclerView.Adapter {  
  15.   
  16.     public View headView;  
  17.     public Context mContext;  
  18.     public int count;  
  19.     private int spanSize;// 当前每行显示几列  
  20.   
  21.     public MyRecycleAdapter(Context mContext, int count) {  
  22.         this.count = count;  
  23.         this.mContext = mContext;  
  24.     }  
  25.   
  26.     /** 
  27.      * 设置数据源总的条目 
  28.      */  
  29.     @Override  
  30.     public int getItemCount() {  
  31.         // 返回条目数加头布局个数  
  32.         return count + 1;  
  33.     }  
  34.   
  35.     @Override  
  36.     public void onBindViewHolder(MyRecycleAdapterHolder holder,  
  37.             final int position) {  
  38.         int itemViewType = getItemViewType(position);  
  39.         // 头部  
  40.         if (itemViewType == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {  
  41.             return;  
  42.         } else {// 普通条目  
  43.             if (itemViewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE) {// 一行两列视图  
  44.                 holder.iv_item_icon.setOnClickListener(new OnClickListener() {  
  45.                     @Override  
  46.                     public void onClick(View v) {  
  47.                         Toast.makeText(mContext, ”2列,” + (position - 1) + “”0)  
  48.                                 .show();  
  49.                     }  
  50.                 });  
  51.             } else if (itemViewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE) {// 一行一列视图  
  52.                 holder.iv_item_icon_single.setOnClickListener(new OnClickListener() {  
  53.                     @Override  
  54.                     public void onClick(View v) {  
  55.                         Toast.makeText(mContext,”单列,” + (position - 1) + “”0).show();  
  56.                     }  
  57.                 });  
  58.             }  
  59.         }  
  60.     }  
  61.   
  62.     @Override  
  63.     public MyRecycleAdapterHolder onCreateViewHolder(ViewGroup parent,  
  64.             int viewType) {  
  65.         View root = null;  
  66.         // 头部  
  67.         if (viewType == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {  
  68.             root = headView;  
  69.         } else {// 普通条目  
  70.             /** 一行显示一条 */  
  71.             if (viewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE) {  
  72.                 root = LayoutInflater.from(mContext).inflate(R.layout.item_single, parent, false);  
  73.             }  
  74.             /** 一行显示两条 */  
  75.             else {  
  76.                 root = LayoutInflater.from(mContext).inflate(R.layout.item_double, parent, false);  
  77.             }  
  78.         }  
  79.         return new MyRecycleAdapterHolder(root, viewType);  
  80.     }  
  81.   
  82.     /** 
  83.      * 添加自定义头部 
  84.      */  
  85.     public void addHeadView(View view) {  
  86.         this.headView = view;  
  87.     }  
  88.   
  89.     @Override  
  90.     public int getItemViewType(int position) {  
  91.   
  92.         if (position == 0) {  
  93.             return RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM;  
  94.         } else {  
  95.             /** 一行显示一条 */  
  96.             if (spanSize == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE) {  
  97.                 return RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE;  
  98.                 /** 一行显示两条 */  
  99.             } else {  
  100.                 return RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE;  
  101.             }  
  102.         }  
  103.     }  
  104.   
  105.     public int getSpanSize() {  
  106.         return spanSize;  
  107.     }  
  108.   
  109.     public void setSpanSize(int spanSize) {  
  110.         this.spanSize = spanSize;  
  111.     }  
  112. }  
import com.example.myrecycleviewdemo.R;
import com.example.myrecycleviewdemo.RecycleViewWithHead;
import com.example.myrecycleviewdemo.R.layout;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

public class MyRecycleAdapter extends
        RecyclerView.Adapter {

    public View headView;
    public Context mContext;
    public int count;
    private int spanSize;// 当前每行显示几列

    public MyRecycleAdapter(Context mContext, int count) {
        this.count = count;
        this.mContext = mContext;
    }

    /**
     * 设置数据源总的条目
     */
    @Override
    public int getItemCount() {
        // 返回条目数加头布局个数
        return count + 1;
    }

    @Override
    public void onBindViewHolder(MyRecycleAdapterHolder holder,
            final int position) {
        int itemViewType = getItemViewType(position);
        // 头部
        if (itemViewType == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {
            return;
        } else {// 普通条目
            if (itemViewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE) {// 一行两列视图
                holder.iv_item_icon.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "2列," + (position - 1) + "", 0)
                                .show();
                    }
                });
            } else if (itemViewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE) {// 一行一列视图
                holder.iv_item_icon_single.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext,"单列," + (position - 1) + "", 0).show();
                    }
                });
            }
        }
    }

    @Override
    public MyRecycleAdapterHolder onCreateViewHolder(ViewGroup parent,
            int viewType) {
        View root = null;
        // 头部
        if (viewType == RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM) {
            root = headView;
        } else {// 普通条目
            /** 一行显示一条 */
            if (viewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE) {
                root = LayoutInflater.from(mContext).inflate(R.layout.item_single, parent, false);
            }
            /** 一行显示两条 */
            else {
                root = LayoutInflater.from(mContext).inflate(R.layout.item_double, parent, false);
            }
        }
        return new MyRecycleAdapterHolder(root, viewType);
    }

    /**
     * 添加自定义头部
     */
    public void addHeadView(View view) {
        this.headView = view;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return RecycleViewWithHead.HEADER_RECYCLER_VIEW_ITEM;
        } else {
            /** 一行显示一条 */
            if (spanSize == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE) {
                return RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE;
                /** 一行显示两条 */
            } else {
                return RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE;
            }
        }
    }

    public int getSpanSize() {
        return spanSize;
    }

    public void setSpanSize(int spanSize) {
        this.spanSize = spanSize;
    }
}


普通条目的ViewHolder

[java] view plain copy
print ?
  1. import com.example.myrecycleviewdemo.R;  
  2. import com.example.myrecycleviewdemo.R.id;  
  3. import com.example.myrecycleviewdemo.RecycleViewWithHead;  
  4. import android.support.v7.widget.RecyclerView;  
  5. import android.view.View;  
  6. import android.widget.ImageView;  
  7. import android.widget.TextView;  
  8.   
  9. public class MyRecycleAdapterHolder extends RecyclerView.ViewHolder{  
  10.     //一行两列视图  
  11.     public ImageView iv_item_icon;  
  12.     public TextView tv_item;  
  13.       
  14.     //一行一列视图  
  15.     public ImageView iv_item_icon_single;  
  16.     public TextView tv_item_single;  
  17.       
  18.     public MyRecycleAdapterHolder(View itemView) {  
  19.         super(itemView);  
  20.     }  
  21.     public MyRecycleAdapterHolder(View itemView,int viewType) {  
  22.         super(itemView);  
  23.         initView(itemView,viewType);  
  24.     }  
  25.     private void initView(View itemView, int viewType) {  
  26.         if(viewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE){  
  27.             iv_item_icon = (ImageView) itemView.findViewById(R.id.iv_item_icon);  
  28.             tv_item = (TextView) itemView.findViewById(R.id.tv_item);  
  29.         }else if(viewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE){  
  30.             iv_item_icon_single = (ImageView) itemView.findViewById(R.id.iv_item_icon_single);  
  31.             tv_item_single = (TextView) itemView.findViewById(R.id.tv_item_single);  
  32.         }  
  33.     }  
  34. }  
import com.example.myrecycleviewdemo.R;
import com.example.myrecycleviewdemo.R.id;
import com.example.myrecycleviewdemo.RecycleViewWithHead;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MyRecycleAdapterHolder extends RecyclerView.ViewHolder{
    //一行两列视图
    public ImageView iv_item_icon;
    public TextView tv_item;

    //一行一列视图
    public ImageView iv_item_icon_single;
    public TextView tv_item_single;

    public MyRecycleAdapterHolder(View itemView) {
        super(itemView);
    }
    public MyRecycleAdapterHolder(View itemView,int viewType) {
        super(itemView);
        initView(itemView,viewType);
    }
    private void initView(View itemView, int viewType) {
        if(viewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_DOUBLE){
            iv_item_icon = (ImageView) itemView.findViewById(R.id.iv_item_icon);
            tv_item = (TextView) itemView.findViewById(R.id.tv_item);
        }else if(viewType == RecycleViewWithHead.RECYCLER_VIEW_ITEM_SINGLE){
            iv_item_icon_single = (ImageView) itemView.findViewById(R.id.iv_item_icon_single);
            tv_item_single = (TextView) itemView.findViewById(R.id.tv_item_single);
        }
    }
}

头布局文件head.xm没有任何变化

条目的布局拆分为2个item_double.xml和item_single.xml

item_double.xml和之前的item.xml一样。


item_single.xml

[html] view plain copy
print ?
  1. xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:id=“@+id/ll_list”  
  4.     android:layout_width=“wrap_content”  
  5.     android:layout_height=“wrap_content”  
  6.     android:layout_marginLeft=“2dp”  
  7.     android:layout_marginRight=“2dp”  
  8.     android:layout_marginTop=“5dp”  
  9.     android:orientation=“horizontal” >  
  10.   
  11.     <ImageView  
  12.         android:id=“@+id/iv_item_icon_single”  
  13.         android:layout_width=“wrap_content”  
  14.         android:layout_height=“wrap_content”  
  15.         android:src=“@drawable/item” />  
  16.   
  17.     <TextView  
  18.         android:id=“@+id/tv_item_single”  
  19.         android:layout_width=“wrap_content”  
  20.         android:layout_height=“wrap_content”  
  21.         android:layout_gravity=“center_vertical”  
  22.         android:layout_marginTop=“5dp”  
  23.         android:text=“这是一只熊猫” />  
  24.   
  25. LinearLayout>  



    

    



源码地址:http://download.csdn.NET/detail/linder_qzy/9491370



你可能感兴趣的:(recyclerview使用,布局)