package com.tyy.recyclerview;

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

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class SampleAdapter extends RecyclerView.Adapter<ViewHolder> {
 private List<Integer> list;

 private static final int TYPE_ITEM = 0;
 private static final int TYPE_FOOTER = 1;

 public List<Integer> getList() {
  return list;
 }

 public SampleAdapter() {
  list = new ArrayList<Integer>();
 }

 @Override
 public int getItemCount() {
  return list.size()+1;
 }

 @Override
 public int getItemViewType(int position) {
  if (position + 1 == getItemCount()) {
   return TYPE_FOOTER;
  } else {
   return TYPE_ITEM;
  }
 }

 @Override
 public void onBindViewHolder(ViewHolder holder, final int position) {
  if (holder instanceof ItemViewHolder) {
   ((ItemViewHolder) holder).textView.setText(String.valueOf(list.get(position)));
  }
 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  if (viewType == TYPE_ITEM) {
   View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_text, null);
   view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
   return new ItemViewHolder(view);
  } else if (viewType == TYPE_FOOTER) {
   View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footerview, null);
   view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
   return new FooterViewHolder(view);
  }
  return null;
 }

 class FooterViewHolder extends ViewHolder {

  public FooterViewHolder(View view) {
   super(view);
  }

 }

 class ItemViewHolder extends ViewHolder {
  TextView textView;

  public ItemViewHolder(View view) {
   super(view);
   textView = (TextView) view.findViewById(R.id.text);
  }

 }

}

03-09 05:46