我正在使用ArrayAdapter来显示Listview中的项目,但是当我滚动项目的顺序更改时,我想pb是在getView方法中,我试图添加一个viewHolder来解决这个问题,但没有改变,我的Listview仍然混淆了
这是我的适配器:
public class AdpaterFeed extends ArrayAdapter<String> {
String[] data;
int layoutResourceId;
Helper helper;
public AdpaterFeed(Context context, int layoutResourceId, String[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.data = data;
this.helper = Helper.getInstance();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(layoutResourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.txt =(TextView) convertView.findViewById(R.id.text3);
viewHolder.webview = (WebView) convertView.findViewById(R.id.webview);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (getItem(position) != null) {
if (getItem(position).contains("<img")) {
viewHolder.webview.loadData(helper.rss[position], "text/html", "UTF-8");
} else {
viewHolder.txt.setText(Html.fromHtml(helper.title[position]));
viewHolder.webview.loadData(helper.rss[position], "text/html", "UTF-8");
}
}
return convertView;
}
public static class ViewHolder {
TextView txt;
WebView webview;
}
}
有人可以帮助我吗?
答案 0 :(得分:1)
更新:在评论的OP精确度中,我认为问题来自于加载webview的方式。也许你应该找到一种方法来控制AsyncTasks的加载,并确保你将正确的视图与正确的内容相匹配。
也许你的问题在于,如果你不删除第一部分中的文字:
if (getItem(position).contains("<img")) {
viewHolder.text.setVisibility(View.GONE); // maybe you want this here?
viewHolder.webview.loadData(getItem(position), "text/html", "UTF-8");
} else {
viewHolder.text.setVisibility(View.VISIBLE); // and compensate here
viewHolder.text.setText(Html.fromHtml(helper.title[position]));
viewHolder.webview.loadData(getItem(position), "text/html", "UTF-8");
}
如果您有不同类型的项目(包含文本或图片),您可能应该查看适配器中的视图类型。这是一个非常重要的功能,允许为不同类型的视图使用不同的布局。
UNRELATED: 关于这段代码:
TextView text = (TextView) convertView.findViewById(R.id.text3);
WebView wv = (WebView) convertView.findViewById(R.id.webview);
viewHolder = new ViewHolder();
viewHolder.txt = text;
viewHolder.webview = wv;
我发现跳过局部变量更清晰(并且更短):
viewHolder = new ViewHolder();
viewHolder.txt = (TextView) convertView.findViewById(R.id.text3);
viewHolder.webview = (WebView) convertView.findViewById(R.id.webview);
答案 1 :(得分:0)
使用视图持有者概念,如下面的示例所示...
@覆盖 public View getView(int position,View convertView,ViewGroup parent){
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, null);
TextView programName = (TextView)convertView.findViewById(R.id.programName);
TextView programTime = (TextView)convertView.findViewById(R.id.programTime);
TextView programState = (TextView)convertView.findViewById(R.id.programState);
ImageView programImage =(ImageView)convertView.findViewById(R.id.programImage);
viewHolder = new ViewHolder();
viewHolder.p_Name = programName;
viewHolder.p_Time = programTime;
viewHolder.p_State = programState;
viewHolder.p_Image = programImage;
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.p_Name.setText(values.programNames.get(position));
viewHolder.p_Time.setText(values.programTimes.get(position));
new imageDownload(programImage).execute(values.programImageUrls.get(position));
return convertView;
}
View holder类看起来像这样
public static class ViewHolder {
TextView p_Name;
TextView p_Time;
TextView p_State;
ImageView p_Image;
}
希望这会对你有所帮助。