在列表视图切换按钮,在Android的滚动屏幕时,失去其状态 [英] Toggle button in a list view loose their state when scrolled of screen in Android

查看:123
本文介绍了在列表视图切换按钮,在Android的滚动屏幕时,失去其状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,其中每个数据来自列表获取适配器列表视图。
在列表视图中的每一项都包含一个文本视图,切换按钮。
但是,当我滚动列表出现问题。切换按钮的选中状态改变即如果我在指数4选择切换按钮,然后滚动后,我发现,除了第四任何随机按钮被选中。

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.

推荐答案

如果你仍然没有得到任何解决方案,请尝试此方法getview ..
// switchState []是一个布尔数组。

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;

    }

这篇关于在列表视图切换按钮,在Android的滚动屏幕时,失去其状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆