Android的 - 是ViewHolder模式自动CursorAdapter的实施? [英] Android - Is ViewHolder pattern automatically implemented in CursorAdapter?

查看:113
本文介绍了Android的 - 是ViewHolder模式自动CursorAdapter的实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是用 ViewHolder 的模式在我的自定义 ArrayAdapter 类。然而,在的CursorAdapter getView()方法不是强制性的要求被否决,但有 bindView NewView的的方法。

我的问题是 - 做的CursorAdapter 再使用意见通过内部实施 ViewHolder 图案或它需要codeD,因为我们通常做的定制 ArrayAdapter ?如果它需要codeD,什么是做了正确的方法是什么?

更新

我用 android.support.v4.widget.CursorAdapter

解决方案
  

我的问题是 - 做的CursorAdapter重新使用意见通过内部   实施ViewHolder图案或它需要$ C $光盘,因为我们   通常做自定义ArrayAdapter?

我不知道在你指的是由 ViewHolder 模式是什么。如果你指的是有一个辅助类缓存每次寻找视图(并将其设置为该行标记查看),那么答案是否定的。如果要实现这个模式,你需要安装支架(寻找在该行视图中的意见 findViewById )的 NewView的方法,然后将其设置为该行视图中的标签。然后在 bindView 方法,你可以调用 getTag ,检索持有并使用它。举个例子:

  //自定义的CursorAdapter ...

    @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
        查看rowView =((LayoutInflater)上下文
                .getSystemService(layout_inflater))。膨胀(
                R.layout.row_layout,父母,假);
        ViewHolder持有人=新ViewHolder();
        holder.v1 = rowView.findViewById(R.id.v1);
        holder.v2 = rowView.findViewById(R.id.v2);
        rowView.setTag(保持器);
        返回rowView;
    }

    @覆盖
    公共无效bindView(查看视图,上下文的背景下,光标光标){
        ViewHolder支架=(ViewHolder)view.getTag();
        //使用充满意见的持有人
        // hlder.v1.setSomething
    }

    类ViewHolder {
        查看V1,V2;
    }
// ...
 

如果你指的是 convertView 被重复使用(如在非光标的适配器),那么答案是是的, getView 方法实现了这个模式,你只需要实现 NewView的 bindView 方法和你保证得到其中被回收(如果可能的话在那一刻)。

视图

I always use ViewHolder pattern in my custom ArrayAdapter classes. However, in CursorAdapter the getView() method is not mandatory required to be overridden , but has the bindView and newView methods.

My question is - does CursorAdapter re-uses views by internally implementing the ViewHolder pattern or it needs to be coded as we normally do in custom ArrayAdapter? If it needs to be coded, what is the correct way to do it?

Update

I'm using android.support.v4.widget.CursorAdapter

解决方案

My question is - does CursorAdapter re-uses views by internally implementing the ViewHolder pattern or it needs to be coded as we normally do in custom ArrayAdapter?

I'm not sure at what do you refer by the ViewHolder pattern. If you are referring to having a helper class to cache looking for view each time(and setting it as a tag for the row View) then the answer is no. If you want to implement this pattern you'll need to setup the holder(look for the views in the row view with findViewById) in the newView method and then set it as the tag for the row view. Then in the bindView method you can call getTag, retrieve the holder and use it. An example:

// custom CursorAdapter ...

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View rowView = ((LayoutInflater) context
                .getSystemService("layout_inflater")).inflate(
                R.layout.row_layout, parent, false);
        ViewHolder holder = new ViewHolder();
        holder.v1 = rowView.findViewById(R.id.v1);
        holder.v2 = rowView.findViewById(R.id.v2);
        rowView.setTag(holder);
        return rowView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        // use the holder filled with views
        // hlder.v1.setSomething
    }

    class ViewHolder {
        View v1, v2;
    }
// ...

If you are referring to the convertView being reused(like in non Cursor based adapters) then the answer is yes, the getView method implements this pattern, you just need to implement the newView and bindView methods and you're guaranteed to get a view which was recycled(if possible at that moment).

这篇关于Android的 - 是ViewHolder模式自动CursorAdapter的实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆