本文介绍了当列表视图Android的滚动开关改变ON-OFF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带开关的一个ListView。每当我切换到ON和向下滚动,滚动它回到它变为OFF。

I have a ListView with switch. Whenever I switch to ON and scroll down and scroll it back it changes to OFF.

推荐答案

SparseBooleanArray 没有任何指定的方法的indexOf

SparseBooleanArray doesn't have any method named indexOf.

如果你只是想检查存储状态,我建议你使用的ListView 的建法 setItemChecked(INT位置,布尔值)

If you just want to store checked states, I'd recommend you to use ListView's built in method setItemChecked(int position, boolean value).

在你的 getView(),你可以看到,如果一个位置被选中(或你的情况,你的交换机的状态是ON或OFF)象下面这样:

In your getView(), you can see if a position is checked (or in your case, your switch's state being ON or OFF) like below:

ListView listView = (ListView)parent;    //get the listview
listView.setItemChecked(itemPosition, true);    //set position as checked
listView.isItemChecked(position);    //check if position is checked (switch is ON)

请确保你把这个在你的的ListView

Make sure you call this on your ListView:

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

更新1:

在你的code:

Update 1:

In your code:

  1. holder.switchItem =(开关)convertView.findViewById(R.id.switchItem); ,添加这个code:

  1. after holder.switchItem = (Switch) convertView.findViewById(R.id.switchItem);, add this code:

final ListView listView = (ListView)parent;
//add listener
holder.switchItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        listView.setItemChecked(position, isChecked);
    }
});

  • 替换 holder.switchItem.setOnCheckedChangeListener(switchItemOnCheckedChangeListener);

    //restore switch state
    holder.switchItem.setChecked(listView.isItemChecked(position));
    

  • 删除以下code:

  • remove the following code:

    SparseBooleanArray checkedItems = new SparseBooleanArray();
    if (checkedItems.indexOf(accountList.get(position).toString()) >= 0) {
    
        holder.switchItem.setChecked(true);
    }
    

  • 这我怎么我的适配器是这样的:

    Update 2:

    This my how my adapter looks like:

    @Override
    public int getCount() {
        return 50;
    }
    
    @Override
    public String getItem(int position) {
        return String.valueOf(position);
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        final ListView listView = (ListView)parent;
        if(convertView == null || convertView.getTag() == null) {
            convertView = mInflator.inflate(R.layout.list_item, parent, false);
            holder = new Holder();
            holder.switch1 = (Switch) convertView.findViewById(R.id.switch1);
            holder.switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    listView.setItemChecked(position, isChecked);
                }
            });
        }
        else {
            holder = (Holder) convertView.getTag();
        }
        holder.switch1.setText(getItem(position));
        //restore switch state
        holder.switch1.setChecked(listView.isItemChecked(position));
    
        return convertView;
    }
    

    这也是为什么我设置我的列表视图在我的 ListActivity

    setListAdapter(new MyAdapter(this));
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    

    这篇关于当列表视图Android的滚动开关改变ON-OFF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-14 08:50