简体   繁体   English

Android:如何在单选模式ListView中选择更改时收到通知?

[英]Android: How to be notified when selection changes in single choice mode ListView?

I want my ListView to highlight the selected item until selection changes. 我希望我的ListView突出显示所选项目,直到选择更改为止。 Also, I want to be notified when selection changes. 此外,我希望在选择更改时收到通知。

In order to accomplish the first task, I call list.setChoiceMode(ListView.CHOICE_MODE_SINGLE) . 为了完成第一个任务,我调用list.setChoiceMode(ListView.CHOICE_MODE_SINGLE)

I'm not sure how to accomplish the second. 我不确定如何完成第二步。 When a list item is clicked, my OnItemSelectedListener is never notified, and the OnItemClickListener is insufficient because by the time the OnItemClick event occurs, my ListView's checkedItem has been modified, so I cannot distinguish if the click event resulted in a selection change (it is possible/likely that the clicked item was already selected). 单击列表项时,我的OnItemSelectedListener永远不会被通知,并且OnItemClickListener不足,因为在OnItemClick事件发生时,我的ListView的checkedItem已被修改,所以我无法区分click事件是否导致选择更改(它是已点击的项目已被选中的可能性/可能性。

One option would be to store the currently checked index in a member variable, which I could use to determine if the click event corresponds to a selection change. 一种选择是将当前检查的索引存储在成员变量中,我可以使用它来确定click事件是否对应于选择更改。 But this ugly and So, for this choice mode, certainly there is a way to do this with the provided listeners? 但这很难看,所以,对于这种选择模式,肯定有一种方法可以用提供的监听器来做到这一点?

public class Temp extends Activity implements OnItemClickListener, OnItemSelectedListener
{
    ListView mView;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        List<String> strings = new ArrayList<String>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        strings.add("4");
        strings.add("5");


        mView = new ListView(this);
        mView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, strings));
        mView.setItemChecked(0, true);

        mView.setOnItemClickListener(this);
        mView.setOnItemSelectedListener(this);

        setContentView(mView);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        //this never gets called
        Log.e("item selected", ""+position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent)
    {
        Log.e("nothing seleted", "");
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
                //by the time this occurs, position == mView.getCheckedItemPosition()
        Log.e("item clicked", ""+position);
    }
}

When a list item is clicked, my OnItemSelectedListener is never notified. 单击列表项时,永远不会通知我的OnItemSelectedListener。

The OnItemSelectedListener is only called by a ListView when you use a trackball, keyboard navigation, or use another type of non-touch navigation. 当您使用轨迹球,键盘导航或使用其他类型的非触摸导航时,OnItemSelectedListener仅由ListView调用。 It does not correlate to the choice mode. 它与选择模式不相关。

One option would be to store the currently checked index in a member variable, which I could use to determine if the click event corresponds to a selection change. 一种选择是将当前检查的索引存储在成员变量中,我可以使用它来确定click事件是否对应于选择更改。

This is the best option. 这是最好的选择。 It only involves one extra int and an if-statement. 它只涉及一个额外的int和一个if语句。

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

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