接上一篇
由简到繁,循序渐进
实现了:
0,点击&长按事件;
1,header;
2,footer;
3,加载更多;
4,还想下拉刷新?“想的美,这本就不是RecyclerView该做的事情!”
import java.util.ArrayList;
import java.util.List;
import com.example.test.R;
import com.widget.view.HeaderFooterAdapter.ViewHolder;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* 为了用RecyclerView创建一个带header的grid:
1,定义一个具有两种view类型的adapter,一个为header一个为普通item。
2,inflate一个header,把它传递给adapter。
3,重写GridLayoutManager中的setSpanSizeLookup,在header所处的位置返回和span count(列数)相等的 span size。
* @author Administrator
*
*/
public class HeaderFooterAdapter extends RecyclerView.Adapter {
private static final int ITEM_VIEW_TYPE_HEADER = 0;
private static final int ITEM_VIEW_TYPE_ITEM = 1;
private static final int ITEM_VIEW_TYPE_FOOTER = 2;
private final View header;
private final View footer;
private int headerCount=0;
private int footerCount=0;
private final List beans;
public HeaderFooterAdapter(View header, View footer,List beans) {
headerCount=header == null?0:1;
footerCount=footer == null?0:1;
this.header = header;
this.footer = footer;
this.beans = beans;
}
public boolean isHeader(int position) {
if(headerCount==0){
return false;
}
return position == 0;
}
public boolean isFooter(int position) {
if(footerCount==0){
return false;
}
return position == beans.size()+headerCount;// add 1 for footer
// return position == getItemCount()-headerCount;// or Subtract 1 for footer
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_VIEW_TYPE_HEADER) {
return new ViewHolder(header);
}
if (viewType == ITEM_VIEW_TYPE_FOOTER) {
return new ViewHolder(footer);
}
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (isHeader(position)) {
return;
}
if (isFooter(position)) {
return;
}
final T bean = beans.get(position - headerCount); // Subtract 1 for header
holder.textView.setText(bean.toString());
}
@Override
public int getItemViewType(int position) {
if(isHeader(position)){
return ITEM_VIEW_TYPE_HEADER;
}
if(isFooter(position)){
return ITEM_VIEW_TYPE_FOOTER;
}
return ITEM_VIEW_TYPE_ITEM;
}
/**
* 获得实际View的数量,包括header和footer
*/
@Override
public int getItemCount() {
return beans.size() + headerCount+footerCount;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{
public TextView textView;
public ViewHolder(View itemView) {
super(itemView);
if (onItemClickLitener != null){
itemView.setOnClickListener(this);
}
if(onItemLongClickLitener!=null){
itemView.setOnLongClickListener(this);
}
textView = (TextView) itemView.findViewById(R.id.item_0);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onItemClickLitener.onItemClick(v, getLayoutPosition());//getPosition()被废弃了
}
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
onItemLongClickLitener.onItemLongClick(v, getLayoutPosition());
return true;
}
}
private static OnItemClickLitener onItemClickLitener;
public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
this.onItemClickLitener = onItemClickLitener;
}
public interface OnItemClickLitener {
/**
* 点击回调,注意position,指的是在Adapter中的位置,已经算上header
* @param view
* @param position
*/
void onItemClick(View view, int position);
}
private static OnItemLongClickLitener onItemLongClickLitener;
public void setOnItemLongClickLitener(OnItemLongClickLitener onItemLongClickLitener) {
this.onItemLongClickLitener = onItemLongClickLitener;
}
public interface OnItemLongClickLitener {
/**
* 长按回调,注意position,指的是在Adapter中的位置,已经算上header
* @param view
* @param position
*/
void onItemLongClick(View view, int position);
}
/**
* 在position位置插入item
* @param position:指的是item在beans中的位置,而不是在Adapter中的位置;
* 所以在notifyItemInserted的时候需要算上header
* @param item
*/
public void addItemAt(int position,T item) {
beans.add(position, item);
notifyItemInserted(position+headerCount);
}
/**
* 在底部添加Items
* @param items
*/
public void addItemsAtLast(ArrayList items) {
int size=beans.size();
beans.addAll(items);
notifyItemRangeInserted(size+headerCount, items.size());
}
/**
* 移除position位置的Item
* @param position :指的是item在beans中的位置,而不是在Adapter中的位置;
* 所以在notifyItemInserted的时候需要算上header
*/
public void removeItemAt(int position) {
beans.remove(position);
notifyItemRemoved(position+headerCount);
}
/**
* 获得中间Item的数量,不包括header和footer
* @return
*/
public int getItemSize(){
return beans.size();
}
}
ArrayList list=new ArrayList();
for (int i = 0; i adapter = new HeaderFooterAdapter(header,footer, list);
adapter.setOnItemClickLitener(new HeaderFooterAdapter.OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
// TODO Auto-generated method stub
if(adapter.isHeader(position)){
Toast.makeText(context, "header clicked", Toast.LENGTH_SHORT).show();
}else if(adapter.isFooter(position)){
Toast.makeText(context, "footer clicked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "header item "+position, Toast.LENGTH_SHORT).show();
}
}
});
final GridLayoutManager manager = new GridLayoutManager(this, 2);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if(adapter.isHeader(position)||adapter.isFooter(position)){
return manager.getSpanCount();
}
return 1;
}
});
//如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView rv, int dx, int dy) {
boolean isBottom =
manager.findLastCompletelyVisibleItemPosition()>= adapter.getItemCount()-1;
if (isBottom) {
ArrayList list=new ArrayList();
int size=adapter.getItemSize();
for (int i = size; i
这是一个如你熟悉ListView、GridView一样熟悉的RecyclerView类库,你可以用以前使用ListView / GridView的习惯来使用RecyclerView,这些可让你将项目原有的ListView / GridView / 瀑布流 迁移到RecyclerView时减少许多工作量.
你必须了解的RecyclerView的五大开源项目-解决上拉加载、下拉刷新和添加Header、Footer等问题
http://www.see-source.com/blog/300000035/1463.html
滑动到顶部/底部 可拉伸回弹的ListView与ScrollView
https://github.com/teisun/SunmiUI
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net