我已經看過所有其他簡單的問題,但無法找到我正在尋找的答案。我有一個ListView,每個列表項都包含一個切換按鈕。我有兩個問題:ListView和ToggleButton
當滾動列表時按下其中一個列表項目上的切換按鈕時,項目被回收;說我點擊第一個項目(並且一次只有三個顯示)每第四個單元格打開切換按鈕
由於這是一個切換,我只希望打開一個按鈕的實例,所以如果它打開了一個列表項,那麼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);
}
您的代碼乍看上去好了,我看不出有任何問題出現。雖然我更喜歡使用getView()參數中的'position'變量,而通過button的標籤傳遞它。我會改變爲vh.playpause.setTag(Integer.valueOf(position)) - 只是我不確定int是否默認轉換爲Integer,或者是否存在帶int參數的setTag方法?我會建議在IDE中調試代碼 – Mixaz 2014-11-20 22:54:43
我認爲,檢查的togglebutton集是android的bug。嘗試將所有設置爲true。作爲解決方法,我會使用imageview併爲檢查的狀態設置不同的圖像。 – MD1948 2014-11-20 22:59:50
問題是它仍然不起作用。我已經嘗試了兩個。 – RnP 2014-11-20 23:01:23