StickyHeaderGridview 简单实用

效果图:

StickyHeaderGridview 简单实用_第1张图片

这里我只提供实用代码,封装好的StickyHeaderGridview可以网上自行下载


MainActivity代码:

packagecn.stickyheadergridviewtest;

importandroid.os.Bundle;

importandroid.support.v7.app.AppCompatActivity;

importandroid.widget.GridView;

importjava.util.ArrayList;

importjava.util.List;

public classMainActivityextendsAppCompatActivity {

privateGridViewmGridView;

privateListmGirdList=newArrayList();

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mGridView= (GridView) findViewById(R.id.asset_grid);

init();

mGridView.setAdapter(newStickyGridAdapter(MainActivity.this,mGirdList));

}

private voidinit() {

GridItem item1=newGridItem("1","1",1);

GridItem item2=newGridItem("2","2",1);

GridItem item3=newGridItem("3","3",1);

GridItem item4=newGridItem("4","4",1);

GridItem item5=newGridItem("5","5",1);

GridItem item6=newGridItem("6","6",2);

GridItem item7=newGridItem("7","7",2);

GridItem item8=newGridItem("8","8",2);

GridItem item9=newGridItem("9","9",2);

GridItem item10=newGridItem("10","10",2);

GridItem item11=newGridItem("11","11",3);

GridItem item12=newGridItem("12","12",3);

GridItem item13=newGridItem("13","13",3);

GridItem item14=newGridItem("14","14",3);

GridItem item15=newGridItem("15","15",3);

GridItem item16=newGridItem("16","16",3);

GridItem item17=newGridItem("17","17",4);

GridItem item18=newGridItem("18","18",4);

GridItem item19=newGridItem("19","19",4);

GridItem item20=newGridItem("20","20",4);

GridItem item21=newGridItem("21","20",4);

GridItem item22=newGridItem("22","20",5);

GridItem item23=newGridItem("23","20",5);

GridItem item24=newGridItem("24","20",5);

GridItem item25=newGridItem("25","20",5);

GridItem item26=newGridItem("26","20",5);

GridItem item27=newGridItem("27","20",6);

GridItem item28=newGridItem("28","20",6);

GridItem item29=newGridItem("29","20",6);

GridItem item30=newGridItem("29","20",6);

GridItem item31=newGridItem("20","20",6);

GridItem item32=newGridItem("20","20",7);

GridItem item33=newGridItem("20","20",7);

GridItem item34=newGridItem("20","20",7);

GridItem item35=newGridItem("20","20",7);

GridItem item36=newGridItem("20","20",7);

GridItem item37=newGridItem("20","20",8);

GridItem item38=newGridItem("20","20",8);

GridItem item39=newGridItem("20","20",8);

GridItem item40=newGridItem("20","20",8);

GridItem item41=newGridItem("20","20",8);

mGirdList.add(item1);

mGirdList.add(item2);

mGirdList.add(item3);

mGirdList.add(item4);

mGirdList.add(item5);

mGirdList.add(item6);

mGirdList.add(item7);

mGirdList.add(item8);

mGirdList.add(item9);

mGirdList.add(item10);

mGirdList.add(item11);

mGirdList.add(item12);

mGirdList.add(item13);

mGirdList.add(item14);

mGirdList.add(item15);

mGirdList.add(item16);

mGirdList.add(item17);

mGirdList.add(item18);

mGirdList.add(item19);

mGirdList.add(item20);

mGirdList.add(item21);

mGirdList.add(item22);

mGirdList.add(item23);

mGirdList.add(item24);

mGirdList.add(item25);

mGirdList.add(item26);

mGirdList.add(item27);

mGirdList.add(item28);

mGirdList.add(item29);

mGirdList.add(item30);

mGirdList.add(item31);

mGirdList.add(item32);

mGirdList.add(item33);

mGirdList.add(item34);

mGirdList.add(item35);

mGirdList.add(item36);

mGirdList.add(item37);

mGirdList.add(item38);

mGirdList.add(item39);

mGirdList.add(item40);

mGirdList.add(item41);

}

}

activity_main.xml


StickyHeaderGridview 简单实用_第2张图片

grid_item.xml


StickyHeaderGridview 简单实用_第3张图片

header.xml


StickyHeaderGridview 简单实用_第4张图片

StickyGridAdapter 适配器代码

packagecn.stickyheadergridviewtest;

importandroid.content.Context;

importandroid.graphics.Point;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.BaseAdapter;

importandroid.widget.GridView;

importandroid.widget.TextView;

importandroid.widget.Toast;

importjava.util.List;

importstickygridheaders.StickyGridHeadersSimpleAdapter;

public classStickyGridAdapterextendsBaseAdapterimplements

StickyGridHeadersSimpleAdapter {

privateListlist;

privateLayoutInflatermInflater;

privateContextmContext;

publicStickyGridAdapter(Context context,List list) {

this.mContext=context;

this.list= list;

mInflater= LayoutInflater.from(context);

}

@Override

public intgetCount() {

returnlist.size();

}

@Override

publicObjectgetItem(intposition) {

returnlist.get(position);

}

@Override

public longgetItemId(intposition) {

returnposition;

}

@Override

publicViewgetView(final intposition,View convertView,ViewGroup parent) {

ViewHolder mViewHolder;

if(convertView ==null) {

mViewHolder =newViewHolder();

convertView =mInflater.inflate(R.layout.grid_item,parent, false);

mViewHolder.tv_griditem= (TextView) convertView.findViewById(R.id.tv_griditem);

convertView.setTag(mViewHolder);

}else{

mViewHolder = (ViewHolder) convertView.getTag();

}

String path =list.get(position).getPath();

mViewHolder.tv_griditem.setText(path);

mViewHolder.tv_griditem.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

Toast.makeText(mContext,list.get(position).getTime(),Toast.LENGTH_SHORT).show();

}

});

returnconvertView;

}

@Override

publicViewgetHeaderView(final intposition,View convertView,ViewGroup parent) {

HeaderViewHolder mHeaderHolder;

if(convertView ==null) {

mHeaderHolder =newHeaderViewHolder();

convertView =mInflater.inflate(R.layout.header,parent, false);

mHeaderHolder.mTextView= (TextView) convertView.findViewById(R.id.header);

convertView.setTag(mHeaderHolder);

}else{

mHeaderHolder = (HeaderViewHolder) convertView.getTag();

}

mHeaderHolder.mTextView.setText(list.get(position).getSection()+"");

mHeaderHolder.mTextView.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

Toast.makeText(mContext,list.get(position).getSection()+"",Toast.LENGTH_SHORT).show();

}

});

returnconvertView;

}

public static classViewHolder {

publicTextViewtv_griditem;

}

public static classHeaderViewHolder {

publicTextViewmTextView;

}

@Override

public longgetHeaderId(intposition) {

returnlist.get(position).getSection();

}

}

你可能感兴趣的:(StickyHeaderGridview 简单实用)