Android ListView 分组基础adapter

来源:互联网 发布:手机动态屏幕软件 编辑:程序博客网 时间:2024/04/29 17:22
</pre><pre name="code" class="java">import android.content.Context;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public abstract class ListGroupAdapter extends BaseAdapter { public Context mContext;public int getSection(){//获取切片return 1;}/** * 根据切片section 获取对应的 row item * @param section * @return */public abstract int getRow(int section);@Overridefinal public int getCount() {int section = getSection();int count = 0;for (int i = 0; i < section; i++) {int header = isGroupHeaderForSection(i)?1:0;int footer = isGroupFooterForSection(i)?1:0;count = count + getRow(i) + header + footer;}return count;}@Overridefinal public int getItemViewType(int position) { IndexPath indexPath = findIndexPathForPosition(position); if (isGroupHeaderForSection(indexPath.section) && indexPath.row == 0 ) {//有头 return getItemHeaderViewType(indexPath); }  if (isGroupFooterForSection(indexPath.section)) {//有尾 int row = getRow(indexPath.section); int header = isGroupHeaderForSection(indexPath.section)?1:0; if (row == indexPath.row - header) {//到尾部了return getItemFooterViewType(indexPath);} } return getItemViewType(indexPath);}/** * 获取 view type的总数 */@Overridepublic int getViewTypeCount() {return 3;}/** *  * @param indexPath 改位置的 view type * @return */public int getItemViewType(IndexPath indexPath){return 0;}/** *  * @param indexPath section Footer 位置的 view type * @return */public int getItemFooterViewType(IndexPath indexPath){return 1;}/** *  * @param indexPath  section Header position view type * @return */public int getItemHeaderViewType(IndexPath indexPath){return 2;}/** * 根据 postion 计算 indexPath * @param position * @return */final public IndexPath findIndexPathForPosition(int position){position -= getListHeadCount();int section = getSection();int count = 0;int sec = 0;for (int i = 0; i < section; i++) {int header = isGroupHeaderForSection(i)?1:0;int footer = isGroupFooterForSection(i)?1:0;count = count + getRow(i) + header + footer;if (count > position) {sec = i;count = count - getRow(i) - header - footer;break;}}IndexPath indexPath = new IndexPath();indexPath.section = sec;indexPath.row = position-count;Log.d("demo", "" +indexPath +" po: "+ position +" getRow:"+getRow(indexPath.section));return indexPath;}/** * listview  header 的数量决定 position 的位置 * @return */public int getListHeadCount(){return 0;}/** * 次getview 不能被重写  */@Overridefinal public View getView(int position, View convertView, ViewGroup parent) { IndexPath indexPath = findIndexPathForPosition(position);  int header = isGroupHeaderForSection(indexPath.section)?1:0;  if (isGroupHeaderForSection(indexPath.section) && indexPath.row == 0) {//有头 return getHeaderView(position, indexPath.section, convertView, parent); }  if (isGroupFooterForSection(indexPath.section)) {//有尾 int row = getRow(indexPath.section); if (row == indexPath.row - header) {//到尾部了return getFooterView(position, indexPath.section, convertView, parent);} }  if (isGroupHeaderForSection(indexPath.section)) {//包含header row位置 -1 和 list index 保持一致 indexPath.row -= 1; }  //内容item return getView(position, indexPath, convertView, parent);}/** * 该组是否包含HeadView * @param section * @return */public boolean isGroupHeaderForSection(int section){return false;}/** * 该组是否包含FooterVew * @param section * @return */public boolean isGroupFooterForSection(int section){return false;}/** * body item的 view  * @param position * @param indexPath * @param convertView * @param parent * @return */public abstract View getView(int position,IndexPath indexPath,View convertView,ViewGroup parent);public View getHeaderView(int position,int section,View convertView,ViewGroup parent){if (isGroupHeaderForSection(section)) {TextView header = null;if (convertView!=null) {header =(TextView) convertView;}else {header = new TextView(mContext);}header.setText("第:"+section+" header");return header;}return null;}public View getFooterView(int position,int section,View convertView,ViewGroup parent){if (isGroupFooterForSection(section)) {TextView footer =null;if (convertView != null) {footer = (TextView) convertView;}else {footer = new TextView(mContext);}footer.setText("第:"+section +" footer");return footer;}return null;}public static class IndexPath{public int row;public int section;@Overridepublic String toString() {return "IndexPath [row=" + row + ", section=" + section + "]";}}}
</pre><pre name="code" class="java">使用:
<pre name="code" class="java">public class DemoAdapter  extends ListGroupAdapter implements OnClickListener{public List<OrderBean> orderBeans;LayoutInflater mInflater;public DemoAdapter( List<OrderBean> orderBeans, Context context ){mContext = context;mInflater = LayoutInflater.from(context);this.orderBeans = orderBeans;}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic int getSection() {return orderBeans.size();}@Overridepublic int getRow(int section) {OrderBean bean = orderBeans.get(section);if (bean.isShowAll) {return bean.goodsBeans.size();}else {return bean.goodsBeans.size()>2?2:bean.goodsBeans.size();}}@Overridepublic View getView(int position, IndexPath indexPath, View convertView,ViewGroup parent) {View view = null;if (convertView!=null) {view = convertView;}else{view = mInflater.inflate(R.layout.goods_item_layout, null);}TextView goodsView = (TextView) view.findViewById(R.id.goods_name);goodsView.setText(orderBeans.get(indexPath.section).goodsBeans.get(indexPath.row).goodsName);return view;}@Overridepublic boolean isGroupHeaderForSection(int section) {//if (section%2==0) {//return false;//}return true;}@Overridepublic boolean isGroupFooterForSection(int section) {if (orderBeans.get(section).goodsBeans.size()<=2) {return false ;}return true;}@Overridepublic View getFooterView(int position, int section, View conView,ViewGroup parent) {View view = null;if (conView!=null) {view = conView ;}else{view = mInflater.inflate(R.layout.footer_item_layout, null);}Button button = (Button) view.findViewById(R.id.button1);button.setTag(section);OrderBean bean = orderBeans.get(section);if (bean.isShowAll) {button.setText("关闭 - " + bean.goodsBeans.size());}else {button.setText("打开 - "+bean.goodsBeans.size());}button.setOnClickListener(this);return view;}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button1:int section = (Integer) v.getTag();OrderBean bean = orderBeans.get(section);bean.isShowAll = !bean.isShowAll;notifyDataSetChanged();break;default:break;}}}


                                             
0 0