为了账号安全,请及时绑定邮箱和手机立即绑定

layoutId被Common类封装了,怎么在外部更改??

@Override
public View getView(int position, View view, ViewGroup viewGroup){
        // 布局文件放在这里,外部怎么更改?
   ViewHolder holder = ViewHolder.get(mContext, view, viewGroup, R.layout.item_listview, position);
   convert(holder, getItem(position));
   return holder.getConvertView();
}


正在回答

1 回答

把布局ID定义为Adapter全局,然后重构造方法里传进来。比如:

public abstract class WrapAdapter<T> extends BaseAdapter {
   protected LayoutInflater    mInflater;
   protected Context           mContext;
   protected List<T>           mDatas;
   protected final int  mItemLayoutId;//外面使用Adapter时传入的布局ID,在getView中使用此ID

   public WrapAdapter(Context context, List<T> mDatas, int itemLayoutId) {
       this.mContext = context;
       this.mInflater = LayoutInflater.from(mContext);
       this.mDatas = mDatas;
       this.mItemLayoutId = itemLayoutId;
   }


在getVIew 中使用

public View getView(int position, View view, ViewGroup viewGroup){

   ViewHolder holder = ViewHolder.get(mContext, view, viewGroup, mItemLayoutId, position);//此处的mItemLayouId就是从外面传入进来的。

   convert(holder, getItem(position));

   return holder.getConvertView();

}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android-打造万能适配器
  • 参与学习       34706    人
  • 解答问题       107    个

本课程通过对传统的ListView数据绑定写法,打造万能适配器

进入课程

layoutId被Common类封装了,怎么在外部更改??

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信