我看过所有其他简单的问题,但找不到我想要的答案。我有一个ListView,每个列表项都包含一个切换按钮。我有两个问题:
任何帮助都将不胜感激。我的适配器代码如下所示:
public class RSSFeedItemListAdapter extends ArrayAdapter<RSSItem> {
private Context context;
private List<RSSItem> items;
private int resource;
private Podcast podcast;
private boolean[] playpauseState;
public RSSFeedItemListAdapter(Context context, int resource, List<RSSItem> items, Podcast podcast) {
super(context, resource, items);
this.items = items;
this.context = context;
this.resource = resource;
this.podcast = podcast;
playpauseState = new boolean[this.items.size()];
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Viewholder vh;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resource, parent, false);
Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/RobotoCondensed-Bold.ttf");
vh = new Viewholder();
vh.title = (TextView) convertView.findViewById(R.id.podcast_feeditem_title);
vh.title.setTypeface(custom_font);
vh.desc = (TextView) convertView.findViewById(R.id.podcast_feeditem_description);
vh.image = (ImageView) convertView.findViewById(R.id.podcast_feeditem_image);
vh.pubDate = (TextView) convertView.findViewById(R.id.podcast_feeditem_pubdate);
vh.collectionName = (TextView) convertView.findViewById(R.id.podcast_feeditem_collection_name);
vh.playpause = (ToggleButton) convertView.findViewById(R.id.podcast_feeditem_play_pause_cta);
vh.playpause.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = (Integer) buttonView.getTag();
playpauseState[position] = isChecked;
}
});
convertView.setTag(vh);
} else {
vh = (Viewholder) convertView.getTag();
}
vh.playpause.setChecked(playpauseState[position]);
vh.playpause.setTag(position);
RSSItem rssItem = items.get(position);
vh.title.setText(rssItem.getTitle());
vh.desc.setText(Jsoup.parse(rssItem.getDescription()).text());
Picasso.with(context).load(podcast.getArtworkExtraLarge()).into(vh.image);
// TODO detect the local and show the right format
// TODO the date format should match the date format from the rssfeedhandler code
if (rssItem.getPubDate() != null) {
vh.pubDate.setText(vh.simpleDateFormat.format(rssItem.getPubDate()));
}
// vh.playpause.setChecked(playpauseState[position]);
vh.collectionName.setText(podcast.getCollectionName());
return convertView;
}
/**
*
*/
static class Viewholder {
ImageView image;
TextView title;
TextView desc;
TextView pubDate;
TextView collectionName;
ToggleButton playpause;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d MMM", Locale.UK);
}发布于 2015-09-09 12:54:29
为了解决这个问题,我挣扎了大约10小时。如果你还没有找到解决办法,我会写一封迟来的回信。
列表视图基本上是基于一种非常灵活(相当聪明)的方式来显示项目,它非常慎重地重新绘制基于滚动的项目,并且可以在屏幕上查看。这意味着它根据当前视图必须容纳的内容动态创建项。把这个分开,想告诉我解决办法!对吗?来到那里
在适配器getView()方法中,只需将checked设置为该项的适当值即可。(或)如果您正在使用ImageView,只需根据on或OFF设置适当的图像资源即可。
public View getView(int position, View convertView, ViewGroup parent) {
ImageView toggle = (ImageView) convertView.findViewById(R.id.toggle);
if(toggle_array(postion) == 1)
toggle.setImageResource(R.drawable.on);
} else {
toggle.setImageResource(R.drawable.off);
}
}https://stackoverflow.com/questions/27050668
复制相似问题