当前位置:   article > 正文

android中adapter公共方法,所有adapter可以继承该方法_android适配器中的使用点击调用另一个公共方法

android适配器中的使用点击调用另一个公共方法

今天忽然想到adapter也是可以写公共方法的,就上网找了一下,发现真的会省很多力气,真为自己以前自己写那么多代码感到着急啊,感叹自己的技术还差很远啊。

直接上代码:

1、先写一个ViewHoldr,这个是必须的,是获取页面数据的方法和给页面赋值的方法

package com.huanyunsoft.portal.base;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewHolder {


   private SparseArray<View> mViews;
   private int mPosition;
   private View mConvertView;
   public ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
      this.mPosition = position;
      this.mViews = new SparseArray<View>();
      mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,false);
      mConvertView.setTag(this);
   }
   public static ViewHolder get(Context context,View convertView,ViewGroup parent,int layoutId,int position){
      if(convertView == null)
      {
         return new ViewHolder(context,parent,layoutId,position);
      }else
      {
         ViewHolder holder = (ViewHolder) convertView.getTag();
         holder.mPosition = position;
         return holder;
      }
   }
   
   public View getConvertView()
   {
      return mConvertView;
   }
   /*
   *
   * */
   public <T extends View>T getView(int viewId)
   {
      View view = mViews.get(viewId);
      
      if(view ==null)
      {
         view = mConvertView.findViewById(viewId);
         mViews.put(viewId, view);
      }
      return (T)view;
      
   }
   /*
   * 基础方法设置字体
   * */
   public ViewHolder setText(int viewId,String text)
   {
      TextView tv = getView(viewId);
      tv.setText(text);
      return this;
   }
   
   public ViewHolder setImageResource(int viewId,int resId)
   {
      ImageView view = getView(viewId);
      view.setImageResource(resId);
      return this;
   }

}

2、下面就是真正的公共方法

package com.huanyunsoft.portal.base;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class CommonAdapter<T> extends BaseAdapter {
   protected Context mContext;
   protected List<T> mDatas;
   protected LayoutInflater mInflater;
   private int layoutId;
   public CommonAdapter(Context context,List<T> datas,int layoutId)
   {
      this.mContext = context;
      this.mDatas = datas;
      this.layoutId = layoutId;
      mInflater = LayoutInflater.from(context);
   }
   @Override
   public int getCount() {
      // TODO Auto-generated method stub
      return mDatas.size();
   }

   @Override
   public T getItem(int position) {
      // TODO Auto-generated method stub
      return mDatas.get(position);
   }

   @Override
   public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
   }

   @Override
   public  View getView(int position, View convertView, ViewGroup parent)
   {
      ViewHolder holder = ViewHolder.get(mContext, convertView, parent, layoutId, position);

      convert(holder,getItem(position));

      return holder.getConvertView();
   }

   public abstract void convert(ViewHolder holder,T t);

}
3、当业务中Activity需要使用listview时,调用方法如下

adapter=new CustomInfoListAdapter(CustomInfoActivity.this,cusBeanList,R.layout.custom_info_list_adapter);
custom_info_list.setAdapter(adapter);
4、自己写的adapter,需要继承上面写的公共方法

public class CustomInfoListAdapter extends CommonAdapter<CustomBuildBean> {

    public CustomInfoListAdapter(Context context, List<CustomBuildBean> datas, int layoutId) {
        super(context, datas, layoutId);
    }

    @Override
    public void convert(ViewHolder holder, CustomBuildBean stringStringMap) {
        holder.setText(R.id.building_name,stringStringMap.getBuildName());
        holder.setText(R.id.source_area,stringStringMap.getHouseArea());

    }

}

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号