博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
给recycleview添加尾部
阅读量:6570 次
发布时间:2019-06-24

本文共 1967 字,大约阅读时间需要 6 分钟。

hot3.png

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);
  }

 }

}

转载于:https://my.oschina.net/u/2334470/blog/518265

你可能感兴趣的文章