如何在图像下方的开关按钮中打开/关闭时保持状态

时间:2015-01-19 04:31:24

标签: switch-statement

http://s107.photobucket.com/user/nguyenduydat274/media/onoff.png.html

enter image description here

开启关闭:保持状态(开/关)并禁用列表视图

关闭开启:保持状态(开/关)并启用lliseview。

1 个答案:

答案 0 :(得分:0)

我会从我理解的内容中添加小代码。这个想法是当您关闭/打开“所有已保存的电影”按钮时,列表视图应该启用和禁用,并保持特定电影的状态。

public class UserSettingsMoviesAdapter extends BaseAdapter {

    // Stores if the list is enabled or disabled. You need to update this 
    // value when the 'All saved movies' button is turned on or off. 
    private Boolean isListEnabled;

    // This list will store if the movie is checked.
    private List < Boolean > mArrChecked;

    @Override
    public boolean isEnabled(int position) {
        if (isListEnabled) {
            return true;
        } else {
            return false;
        }
    }

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

        ...

        boolean isChecked = mArrChecked.get(position);
        swOnOff.setChecked(isChecked);

        swOnOff.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mArrChecked.set(currentPos, isChecked);
                notifyDataSetChanged();
            }
        });

        ...
    }
}    

现在,要启用或禁用整个列表,您需要更改isListEnabled变量的值。如果返回false,则isEnabled()将为每个项返回false,列表视图将禁用所有项,但状态将保留。确保在更新notifyDataSetChanged()的值后致电isListEnabled