简体   繁体   English

setChoiceMode(ListView.CHOICE_MODE_SINGLE)如何取消选择?

[英]setChoiceMode(ListView.CHOICE_MODE_SINGLE) How to cancel selection?

I have a ListView which uses: 我有一个ListView使用:

setChoiceMode(ListView.CHOICE_MODE_SINGLE)

So when the user clicks on a list item it is displayed as selected. 因此,当用户单击列表项时,它将显示为已选中。 This works fine and automatically selects the item. 这很好,并自动选择项目。

Note: The standard action when the user clicks on a list item is that it updates some content in another fragment. 注意:用户单击列表项时的标准操作是它更新另一个片段中的某些内容。

But there are some list items in the list which I do not want selected. 但是列表中有一些我不想选择的列表项。 Instead of updating content I show a dialog box. 而不是更新内容我显示一个对话框。 The trouble is, when the user has got rid of the dialog box, the item is still marked as selected, which I don't want. 麻烦的是,当用户摆脱了对话框时,该项仍然被标记为已选中,这是我不想要的。 In fact I don't want it to be selected at all, and keep the previously selected list item selected. 实际上我根本不希望它被选中,并保持选择先前选择的列表项。

Is there any way to programmatically select a list item (so you can choose whether to set it selected, or not), and at the same time use the CHOICE_MODE_SINGLE because this ensures that only 1 item in the list is currently selected ? 有没有办法以编程方式选择列表项(因此您可以选择是否选择它),同时使用CHOICE_MODE_SINGLE,因为这样可以确保当前只选择列表中的1个项目?

Look at this code: 看看这段代码:

mListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
        if (position == 0) {
            mListView.setItemChecked(position, false);
        }
    }
});

It unselects list item, if chosen first item. 如果选择第一项,则取消选择列表项。 Also you can save last correct position 您也可以保存最后正确的位置

mListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
        if (position == 0) {
            mListView.setItemChecked(mLastCorrectPosition, true);
        }
        else {
            mLastCorrectPosition = position;
        }
    }
});

EDIT 编辑

Full activity 完整的活动

public class MainActivity extends Activity {

    private ListView mListView;
    private String[] mData = new String[] { "xxx", "yyy", "zzz", "aaa" };
    private BaseAdapter mAdapter;
    private int mLastCorrectPosition = -1;
    private int mButtonPosition = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        mAdapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_activated_1, mData);
        mListView.setAdapter(mAdapter);
        mListView.setSelector(new ColorDrawable(0));

        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                if (position == mButtonPosition) {
                    if (mLastCorrectPosition != -1) {
                        mListView.setItemChecked(mLastCorrectPosition, true);
                    }
                    else {
                        mListView.setItemChecked(mButtonPosition, false);
                    }
                    // here show dialog
                }
                else {
                    mLastCorrectPosition = position;
                    // here refresh fragment
                }
            }
        });
    }
}

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

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