0

我查看了所有其他简单的问题,但找不到我正在寻找的答案。我有一个 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);

    }
4

1 回答 1

0

我挣扎了大约 10 个小时才弄清楚这一点。如果您仍然没有找到解决方案,我正在写一个很晚的回复

列表视图基本上以一种非常奇怪(相当聪明)的方式来显示项目,它非常周到地显示项目,它基于滚动重绘项目并且可以在屏幕上查看。这意味着它会根据当前视图必须容纳的内容动态创建项目。抛开这个,想告诉我解决方案!正确的?来到那里

在您的适配器 getView() 方法中,只需将选中状态设置为该项目的适当值。(或)如果您使用的是 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);
           }
        }
于 2015-09-09T12:54:29.107 回答