首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ListView和ToggleButton

ListView和ToggleButton
EN

Stack Overflow用户
提问于 2014-11-20 22:17:52
回答 1查看 471关注 0票数 0

我看过所有其他简单的问题,但找不到我想要的答案。我有一个ListView,每个列表项都包含一个切换按钮。我有两个问题:

  • 当滚动列表时按下列表项上的切换按钮时,项目就会被回收;例如,我单击了第一个项目(一次只有三个显示),每四个单元格就打开切换按钮。
  • 因为这是一个切换,所以我只想打开按钮的一个实例,所以如果它打开了一个列表项,那么ListView中可能打开ToggleButton的所有其他项都应该关闭。

任何帮助都将不胜感激。我的适配器代码如下所示:

代码语言:javascript
运行
复制
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);

    }
EN

回答 1

Stack Overflow用户

发布于 2015-09-09 12:54:29

为了解决这个问题,我挣扎了大约10小时。如果你还没有找到解决办法,我会写一封迟来的回信。

列表视图基本上是基于一种非常灵活(相当聪明)的方式来显示项目,它非常慎重地重新绘制基于滚动的项目,并且可以在屏幕上查看。这意味着它根据当前视图必须容纳的内容动态创建项。把这个分开,想告诉我解决办法!对吗?来到那里

在适配器getView()方法中,只需将checked设置为该项的适当值即可。(或)如果您正在使用ImageView,只需根据on或OFF设置适当的图像资源即可。

代码语言:javascript
运行
复制
  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);
           }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27050668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档