Toggle button in a list view loose their state when scrolled of screen in Android

断了今生、忘了曾经 提交于 2019-12-11 02:38:07

问题


I have a list view in which each data is fetched from List adapter. Each item in list view contains a text view and toggle button. But there occurs a problem when i scroll the list. the checked state of toggle buttons change i.e if i selected a toggle button at index 4, then after scrolling i find that any random button except fourth is selected.


回答1:


if u still doesn't get any solution pls try this getview method.. // switchState[] is a boolean array .

public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder = new ViewHolder();
         // mSwitchButton = (ToggleButton) findViewById(R.id.switchButton);
        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.settings_item_cell, parent, false);
            holder.categoryName =
                    (TextView) convertView.findViewById(R.id.categoryType);
            convertView.setTag(holder);
            holder.switchButton  = (ToggleButton) convertView.findViewById(R.id.switchButton); 
        }
        else {
            holder = (ViewHolder) convertView.getTag();


        }

            holder.switchButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    int index =  (Integer) v.getTag();
                    Log.v("tag of switch============",""+index);
                    if(((ToggleButton) v).isChecked()) {
                        switchState[index] = true; 
                        ((ToggleButton) v).setButtonDrawable(R.drawable.item_selected);

                    }
                    else {
                        ((ToggleButton) v).setButtonDrawable(R.drawable.item_deselected);
                        switchState[index] =false; 
                    }
                    isToggleButtonClicked = true;

                }
            });  
            if(switchState[position])   
                holder.switchButton.setButtonDrawable(R.drawable.item_selected);
            else
                holder.switchButton.setButtonDrawable(R.drawable.item_deselected);

        holder.categoryName.setText(categories[position]);
        holder.switchButton.setTag(new Integer(position));

        return convertView;

    }



回答2:


Since rows get recycled, you need to maintain your own state, saving and restoring your ToggleButton status as it is modified. Here is a sample project demonstrating using a RatingBar in rows; the same basic process should hold for a ToggleButton.




回答3:


I know this is a really old thread , but the answers I found to the question were not really great. It took me some time to get a workable solution from the two answers. I just wanted to share the working code, if any other noob is trying to figure out how persist the state of toggle button while scrolling.

private class MyListAdapter extends ArrayAdapter<Product> {
     boolean [] switchState= new boolean[100] ;
    public MyListAdapter() {
        super(ProductSelectActivity.this, R.layout.item_view, myProducts);
    }

public View getView( int position, View convertView, ViewGroup parent) {
        // Make sure we have a view to work with (may have been given null)

        ViewHolder holder;

        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
            View row= itemView;
            holder= new ViewHolder(row);
            itemView.setTag(holder);
            holder.switchButton  = (ToggleButton) itemView.findViewById(R.id.favorite_button); 

        }
        else
        {
            holder = (ViewHolder) itemView.getTag();

        }


        holder.switchButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                int index =  (Integer) v.getTag();

                if(((ToggleButton) v).isChecked()) {
                    switchState[index] = true; 
                    ((ToggleButton) v).setBackgroundResource(android.R.drawable.btn_star_big_on);

                }
                else {
                    ((ToggleButton) v).setBackgroundResource(android.R.drawable.btn_star_big_off);
                    switchState[index] =false; 
                }

            }
        });
        if(switchState[position])   
            holder.switchButton.setBackgroundResource(android.R.drawable.btn_star_big_on);

        else
            holder.switchButton.setBackgroundResource(android.R.drawable.btn_star_big_off);

    //holder.categoryName.setText(categories[position]);
    holder.switchButton.setTag(Integer.valueOf(position));

ViewHolder.class

public class ViewHolder {
ToggleButton switchButton=null;

  ViewHolder(View base) {
    this.switchButton=(ToggleButton)base.findViewById(R.id.favorite_button);
  }

}

This code compiles.



来源:https://stackoverflow.com/questions/4712441/toggle-button-in-a-list-view-loose-their-state-when-scrolled-of-screen-in-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!