简体   繁体   English

ListView中的奇怪ToggleButton行为

[英]Strange ToggleButton behavior inside ListView

I'm having a simple ListView filled with some test items: 我有一个简单的ListView ,其中包含一些测试项目:

private ArrayList<NearbyRestaurantListItem> getItems() {
    ArrayList<NearbyRestaurantListItem> items = new ArrayList<>();
    // ..
    items.add( new NearbyRestaurantListItem(null, "Item number " , "seven") );
    items.add( new NearbyRestaurantListItem(null, "Item number " , "eight") );
    items.add( new NearbyRestaurantListItem(null, "Item number " , "nine") );
    items.add( new NearbyRestaurantListItem(null, "Item number " , "ten") );
    // ..
    return items;
}

The thing is that these list items have a toggleable "favorite" button. 问题是这些列表项具有可切换的“收藏夹”按钮。 The strange behavior I am talking about is that if I toggle a favorite button another button from another list item gets toggled too. 我正在谈论的奇怪行为是,如果我切换一个收藏夹按钮, 另一个列表项中的另一个按钮也会被切换。 It appears that the 6th toggle button from the pressed one gets toggled too and I have no idea why. 看来,按下的第六个切换按钮也被切换了,我不知道为什么。 Is it because the OnClickListener* classes are static ? 是因为OnClickListener*类是static吗? NearbyRestaurantListItem is inflated like this: NearbyRestaurantListItem会像这样膨胀:

public View getView(int position, View convertView, ViewGroup parent) {

    NearbyRestaurantListItemViewHolder holder;
    if(convertView == null){
        convertView = mInflater.inflate(R.layout.listview_nearbylist_item, parent, false);
        holder = new NearbyRestaurantListItemViewHolder();
        holder.restaurantThumbnail = (ImageView) convertView.findViewById(R.id.restaurant_thumbnail);
        holder.restaurantName      = (TextView) convertView.findViewById(R.id.restaurant_name);
        holder.restaurantGenre     = (TextView) convertView.findViewById(R.id.restaurant_genre);
        holder.openingHours        = (TextView) convertView.findViewById(R.id.opening_hours);

        convertView.setTag(holder);
    } else {
        holder = (NearbyRestaurantListItemViewHolder) convertView.getTag();
    }

    holder.restaurantThumbnail.setImageDrawable(null);
    holder.restaurantName.setText(items.get(position).getRestaurantName());
    holder.restaurantGenre.setText(items.get(position).getRestaurantGenre());
    holder.openingHours.setText("08:00 - 18:00 Uhr");

    if(holder.restaurantGenre.getText().length() == 0) {
        holder.restaurantGenre.setVisibility(View.GONE);
    }

    ToggleButton favorite = (ToggleButton)convertView.findViewById(R.id.favorite);
    favorite.setOnClickListener(new NearbyRestaurantListFragment.OnClickFavoriteButtonListener(activity));
    favorite.setTag(convertView);

    ImageView map = (ImageView)convertView.findViewById(R.id.map);
    map.setOnClickListener(new NearbyRestaurantListFragment.OnClickMapButtonListener(activity));
    map.setTag(convertView);

    return convertView;
}

I really feel stupid asking this but I can't figure out the problem here. 我真的很愚蠢地问这个问题,但是我无法在这里找出问题所在。

The issue here is that in Android ListView s, the individual item rows are being recycled, and you're not resetting the state of the favorite button. 这里的问题是,在Android ListView ,单个项目行被回收,并且您没有重置“收藏夹”按钮的状态。 You should add your favorite button to the holder and change its state appropriately (or reset it to untoggled). 您应该将自己喜欢的按钮添加到holder并适当地更改其状态(或将其重置为未切换)。

I have implemented something similar in this post . 我已经在这篇文章中实现了类似的功能 Check it out, it is what you need. 检查一下,这就是您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM