![](/img/trans.png)
[英]How can I manage onClick function from my custom listview adapter in main activity class
[英]How can I cache custom views in my ListView Adapter?
在我的ImageAdapter
(扩展BaseAdapter
),每个View
都是一个自定义视图,我称为ImageTapView
。 它是一个单独的类,该类使用自己的以<merge>
开头的布局文件扩展RelativeLayout
。 此类包含所有单击侦听器,每个ImageTapView
从Internet下载和显示的图像的加载器,以及单击时发生的动画。
这是我的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
}
在我的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;
}
但这意味着Android每次调用getView
都会创建一个新的ImageTapView
而不是仅仅替换已经膨胀的视图中的数据。 我认为进入和替换每个字段并重置getView
上的动画会更困难,更不用说了,我希望用户看到在与ImageTapView
的交互中它们ImageTapView
在了ImageTapView
位置(例如放大图像)或在动画中间)。
我尝试将每个视图保存到名为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;
}
但是我得到一些非常奇怪的行为。 ImageTapViews
,在3个交互步骤(开始状态,缩放状态,信息叠加显示状态)中的第2步中的ImageTapViews
确实很奇怪。 他们似乎没有从上次停止的地方接听,也没有以正确的方式响应点击,而没有使用ImageTapView
的正确方式。
那么,是否存在在ListView
中缓存对象或自定义对象的标准方法? 我应该扩展ArrayAdapter
,还是在getView
函数中getView
同样的问题?
提前致谢!
试试这个,你需要创建额外的方法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;
}
尝试这种方式,希望这将帮助您解决问题。
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>
在适配器中填充自定义行布局。
公共类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.