package com.bwie.recyclerviewheader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rl;
private ArrayList<String> list;
private GridLayoutManager gr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl = (RecyclerView) findViewById(R.id.rl);
list = new ArrayList<>();
for (int i = 0; i <100 ; i++) {
list.add("数据"+i);
}
gr = new GridLayoutManager(this, 3);
rl.setLayoutManager(gr);
final MyAdapter adapter = new MyAdapter();
TextView tv = new TextView(this);
tv.setText("我是第一个头布局");
TextView tv1 = new TextView(this);
tv1.setText("我是第二个头布局");
adapter.addHeader(tv);
adapter.addHeader(tv1);
gr.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (adapter.isHeader(position)){
return gr.getSpanCount();
}
return 1;
}
});
rl.setAdapter(adapter);
TextView tv3 = new TextView(this);
tv3.setText("我是第一个尾布局");
adapter.addFooter(tv3);
}
class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == HEADER_TYPE){
View view = View.inflate(MainActivity.this,R.layout.header,null);
MyHeaderViewHolder headerHolder = new MyHeaderViewHolder(view);
return headerHolder;
}else if(viewType == ITEM_TYPE) {
View view = View.inflate(MainActivity.this,R.layout.item,null);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}else {
View view = View.inflate(MainActivity.this,R.layout.header,null);
MyFooterViewHolder footerViewHolder = new MyFooterViewHolder(view);
return footerViewHolder;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyHeaderViewHolder){
if (((MyHeaderViewHolder)holder).header.getChildCount() == 0){
((MyHeaderViewHolder)holder).header.addView(headerList.get(position));
}
}else if (holder instanceof MyFooterViewHolder){
if (((MyFooterViewHolder)holder).footer.getChildCount() ==0){
((MyFooterViewHolder)holder).footer.addView(footerList.get(position-list.size()-headerList.size()));
}
} else if(holder instanceof MyViewHolder) {
((MyViewHolder)holder).tv.setText(list.get(position - headerList.size()));
}
}
@Override
public int getItemCount() {
return list.size()+headerList.size()+footerList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
}
}
List<View> headerList = new ArrayList<>();
List<View> footerList = new ArrayList<>();
private final static int HEADER_TYPE = 0;
private final static int ITEM_TYPE = 1;
private final static int FOOTER_TYPE = 2;
class MyHeaderViewHolder extends RecyclerView.ViewHolder{
LinearLayout header;
public MyHeaderViewHolder(View itemView) {
super(itemView);
header = itemView.findViewById(R.id.header);
}
}
class MyFooterViewHolder extends RecyclerView.ViewHolder{
LinearLayout footer;
public MyFooterViewHolder(View itemView) {
super(itemView);
footer = itemView.findViewById(R.id.header);
}
}
@Override
public int getItemViewType(int position) {
if ( position < headerList.size()){
return HEADER_TYPE;
}else if( position < headerList.size()+list.size()) {
return ITEM_TYPE;
}else {
return FOOTER_TYPE;
}
}
public void addHeader(View v){
if(headerList != null){
headerList.add(v);
notifyDataSetChanged();
}
}
public void addFooter(View v){
footerList.add(v);
}
public boolean isHeader(int position){
if (position < headerList.size()){
return true;
}
return false;
}
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rl;
private ArrayList<String> list;
private GridLayoutManager gr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl = (RecyclerView) findViewById(R.id.rl);
list = new ArrayList<>();
for (int i = 0; i <100 ; i++) {
list.add("数据"+i);
}
gr = new GridLayoutManager(this, 3);
rl.setLayoutManager(gr);
final MyAdapter adapter = new MyAdapter();
TextView tv = new TextView(this);
tv.setText("我是第一个头布局");
TextView tv1 = new TextView(this);
tv1.setText("我是第二个头布局");
adapter.addHeader(tv);
adapter.addHeader(tv1);
gr.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (adapter.isHeader(position)){
return gr.getSpanCount();
}
return 1;
}
});
rl.setAdapter(adapter);
TextView tv3 = new TextView(this);
tv3.setText("我是第一个尾布局");
adapter.addFooter(tv3);
}
class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == HEADER_TYPE){
View view = View.inflate(MainActivity.this,R.layout.header,null);
MyHeaderViewHolder headerHolder = new MyHeaderViewHolder(view);
return headerHolder;
}else if(viewType == ITEM_TYPE) {
View view = View.inflate(MainActivity.this,R.layout.item,null);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}else {
View view = View.inflate(MainActivity.this,R.layout.header,null);
MyFooterViewHolder footerViewHolder = new MyFooterViewHolder(view);
return footerViewHolder;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyHeaderViewHolder){
if (((MyHeaderViewHolder)holder).header.getChildCount() == 0){
((MyHeaderViewHolder)holder).header.addView(headerList.get(position));
}
}else if (holder instanceof MyFooterViewHolder){
if (((MyFooterViewHolder)holder).footer.getChildCount() ==0){
((MyFooterViewHolder)holder).footer.addView(footerList.get(position-list.size()-headerList.size()));
}
} else if(holder instanceof MyViewHolder) {
((MyViewHolder)holder).tv.setText(list.get(position - headerList.size()));
}
}
@Override
public int getItemCount() {
return list.size()+headerList.size()+footerList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv;
public MyViewHolder(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
}
}
List<View> headerList = new ArrayList<>();
List<View> footerList = new ArrayList<>();
private final static int HEADER_TYPE = 0;
private final static int ITEM_TYPE = 1;
private final static int FOOTER_TYPE = 2;
class MyHeaderViewHolder extends RecyclerView.ViewHolder{
LinearLayout header;
public MyHeaderViewHolder(View itemView) {
super(itemView);
header = itemView.findViewById(R.id.header);
}
}
class MyFooterViewHolder extends RecyclerView.ViewHolder{
LinearLayout footer;
public MyFooterViewHolder(View itemView) {
super(itemView);
footer = itemView.findViewById(R.id.header);
}
}
@Override
public int getItemViewType(int position) {
if ( position < headerList.size()){
return HEADER_TYPE;
}else if( position < headerList.size()+list.size()) {
return ITEM_TYPE;
}else {
return FOOTER_TYPE;
}
}
public void addHeader(View v){
if(headerList != null){
headerList.add(v);
notifyDataSetChanged();
}
}
public void addFooter(View v){
footerList.add(v);
}
public boolean isHeader(int position){
if (position < headerList.size()){
return true;
}
return false;
}
}
}