我查看了所有其他简单的问题,但找不到我正在寻找的答案。我有一个 ListView,每个列表项都包含一个切换按钮。我有两个问题:
当滚动列表时按下其中一个列表项上的切换按钮时,项目将被回收;假设我点击了第一个项目(一次只显示三个)每四个单元格打开切换按钮
由于这是一个切换按钮,我只希望打开按钮的一个实例,因此如果它打开单个列表项,则 ListView 中可能打开 ToggleButton 的所有其他项都应该关闭。
任何帮助,将不胜感激。我的适配器代码如下所示:
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);
}