简体   繁体   English

如何在ListView适配器中缓存自定义视图?

[英]How can I cache custom views in my ListView Adapter?

In my ImageAdapter (extends BaseAdapter ), each View is a custom view I called ImageTapView . 在我的ImageAdapter (扩展BaseAdapter ),每个View都是一个自定义视图,我称为ImageTapView It's a separate class that extends RelativeLayout with its own layout file that starts with a <merge> . 它是一个单独的类,该类使用自己的以<merge>开头的布局文件扩展RelativeLayout This class contains all the click listeners, loaders for the images that each ImageTapView downloads and displays from the internet, as well as animations that happen on click. 此类包含所有单击侦听器,每个ImageTapView从Internet下载和显示的图像的加载器,以及单击时发生的动画。

Here's my ImageAdapter constructor: 这是我的ImageAdapter构造函数:

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private JSONArray posts;
    private String baseUrl;
    private HashMap views = new HashMap();

    public ImageAdapter(Context _context, JSONArray _posts, String _baseUrl){
        context = _context;
        posts = _posts;
        baseUrl = _baseUrl;            
    }

    ...etc
}

In my ImageAdapter.getView everything works fine if I do something like: 在我的ImageAdapter.getView如果执行以下操作,则一切正常:

public View getView(int position, View convertView, ViewGroup parent){
    // create new imageTapView
    JSONObject thisPost = posts.getJSONObject(position);
    convertView = new ImageTapView(context, thisPost);
    return convertView;
}

But this means Android is creating a new ImageTapView every time it calls getView instead of just replacing the data in the already inflated view. 但这意味着Android每次调用getView都会创建一个新的ImageTapView而不是仅仅替换已经膨胀的视图中的数据。 I think it'd be way harder to go in and replace each field and reset the animations on getView , not to mention, I'd like the user to see where in their interaction with ImageTapView they left off (eg zoomed in on an image or in the middle of an animation). 我认为进入和替换每个字段并重置getView上的动画会更困难,更不用说了,我希望用户看到在与ImageTapView的交互中它们ImageTapView在了ImageTapView位置(例如放大图像)或在动画中间)。

I tried saving each view to a HashMap called views : 我尝试将每个视图保存到名为viewsHashMap

public View getView(int position, View convertView, ViewGroup parent){
    if(views.containsKey(position)) return (ImageTapView) views.get(position);
    // create new imageTapView
    JSONObject thisPost = posts.getJSONObject(position);
    convertView = new ImageTapView(context, thisPost);
    views.put(position, convertView);
    return convertView;
}

But I get some very strange behavior. 但是我得到一些非常奇怪的行为。 ImageTapViews that are on, say, step 2 of 3 steps of interaction (start state, zoomed state, information overlay shown state), things get really strange. ImageTapViews ,在3个交互步骤(开始状态,缩放状态,信息叠加显示状态)中的第2步中的ImageTapViews确实很奇怪。 They don't seem to pick up where they left off and they don't respond to clicks the proper way a fresh ImageTapView would. 他们似乎没有从上次停止的地方接听,也没有以正确的方式响应点击,而没有使用ImageTapView的正确方式。

So is there a standard way of caching objects or custom objects in a ListView ? 那么,是否存在在ListView中缓存对象或自定义对象的标准方法? Should I be extending ArrayAdapter instead, or would my problem be the same in the getView function? 我应该扩展ArrayAdapter ,还是在getView函数中getView同样的问题?

Thanks in advance! 提前致谢!

try this ,you need create extra method setPost() to set post 试试这个,你需要创建额外的方法setPost()来设置发布

 public View getView(int position, View convertView, ViewGroup parent){
      // create new imageTapView
      JSONObject thisPost = posts.getJSONObject(position);
      if(convertView==null)
            convertView = new ImageTapView(context, thisPost);
      else{
      ((ImageTapView)convertView).setPost(thisPost); 
      }
     return convertView;
 }

Try this way,hope this will help you to solve your problem. 尝试这种方式,希望这将帮助您解决问题。

  1. Custom item layout for your adapter. 适配器的自定义项目布局。

row.xml row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <packageName.ImageTapView>
        android:id="@+id/imageTapView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    </packageName.ImageTapView>
</LinearLayout>
  1. inflate custom row layout in adapter. 在适配器中填充自定义行布局。

    public class ImageAdapter extends BaseAdapter { 公共类ImageAdapter扩展BaseAdapter {

     private Context context; private JSONArray posts; private String baseUrl; private HashMap views = new HashMap(); public ImageAdapter(Context _context, JSONArray _posts, String _baseUrl){ context = _context; posts = _posts; baseUrl = _baseUrl; } static class ViewHolder { ImageTapView imageTapView; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(_context).inflate(R.layout.row, null); viewHolder.imageTapView = (ImageTapView) convertView.findViewById(R.id.imageTapView); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.imageTapView.setPost(posts.getJSONObject(position)); return convertView; } ...etc 

    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从主要活动类中的自定义列表视图适配器管理onClick函数 - How can I manage onClick function from my custom listview adapter in main activity class 如何按字母顺序对 listView 的文件进行排序并集成列表适配器?我正在使用自定义适配器 - How do i sort alphabetically the files of my listView and integrate a list adapter?I'm using a custom adapter 如何从我的自定义适配器更新ListView - How update ListView from my Custom Adapter 在自定义Listview适配器中使用多个视图 - Using Multiple Views in a Custom Listview Adapter 调用自定义列表视图适配器时我的应用程序失败 - My app is failing when i call to my custom listview adapter 如何将ListView适配器转换为RecyclerView适配器? - How do I convert my ListView adapter into RecyclerView adapter? 将当前的ListView与带有ListView的自定义适配器集成在一起以允许多个视图 - Integrating current ListView with a Custom Adapter with ListView to allow multiple views 我可以从自定义适配器的getView填充一个listview吗? - Can I populate a listview from getView of a custom adapter? 如何使用我的自定义 ListView 访问 EditText - How can I access an EditText withing my custom ListView 如何为绑定到自定义适配器的 ListView 设置 onClickListener? - How do I set up onClickListener for ListView binded to custom adapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM