[英]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
: 我尝试将每个视图保存到名为views
的HashMap
:
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. 尝试这种方式,希望这将帮助您解决问题。
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>
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.