RecyclerView是一个高度可定制性的View
本文将使用RecyclerView实现带header的grid
为了用RecyclerView创建一个带header的grid:
1,定义一个具有两种view类型的adapter,一个为header,一个为普通item。
2,nflate一个header,把它传递给adapter。
3,重写GridLayoutManager中的setSpanSizeLookup,在header所处的位置返回和span count (列数)相等的 span size。
import java.util.ArrayList;
import java.util.List;
import com.example.test.R;
import com.widget.view.HeaderAdapter.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 HeaderAdapter extends RecyclerView.Adapter<ViewHolder> {
private static final int ITEM_VIEW_TYPE_HEADER = 0;
private static final int ITEM_VIEW_TYPE_ITEM = 1;
private final View header;
private final List<String> labels;
public HeaderAdapter(View header, int count) {
if (header == null) {
throw new IllegalArgumentException("header may not be null");
}
this.header = header;
this.labels = new ArrayList<String>(count);
for (int i = 0; i < count; ++i) {
labels.add(String.valueOf(i));
}
}
public boolean isHeader(int position) {
return position == 0;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_VIEW_TYPE_HEADER) {
return new ViewHolder(header);
}
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;
}
final String label = labels.get(position - 1); // Subtract 1 for header
holder.textView.setText(label);
}
@Override
public int getItemViewType(int position) {
return isHeader(position) ? ITEM_VIEW_TYPE_HEADER : ITEM_VIEW_TYPE_ITEM;
}
@Override
public int getItemCount() {
return labels.size() + 1;
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView textView;
public ViewHolder(View itemView) {
super(itemView);
if (onItemClickLitener != null){
itemView.setOnClickListener(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()被废弃了
}
}
private static OnItemClickLitener onItemClickLitener;
public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
this.onItemClickLitener = onItemClickLitener;
}
public interface OnItemClickLitener {
void onItemClick(View view, int position);
}
}
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
View header = LayoutInflater.from(this).inflate(R.layout.header_view, recyclerView, false);
final HeaderAdapter adapter = new HeaderAdapter(header, 30);
adapter.setOnItemClickLitener(new HeaderAdapter.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{
Toast.makeText(context, "header item "+(position-1), Toast.LENGTH_SHORT).show();
}
}
});
final GridLayoutManager manager = new GridLayoutManager(this, 3);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return adapter.isHeader(position) ? manager.getSpanCount() : 1;
}
});
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
具体描述看原帖,我只是稍稍改了一下
http://www.open-open.com/lib/view/open1437662138631.html 如果你觉得使用RecyclerView添加Header比较烦,那没办法,就使用这个 HeaderGridView类吧
RecyclerView 必知必会
http://mp.weixin.qq.com/s/CzrKotyupXbYY6EY2HP_dA
标签:int,header,grid,position,RecyclerView,public,view From: https://blog.51cto.com/u_5454003/6174212