Android:在ListView打开时将项目设置为选中状态?

时间:2010-10-08 05:49:58

标签: android android-listview focus listviewitem

活动有一个Button和一个ListView。 最初,只有Button可见。按下按钮时,将显示ListView。 显示时,我是否可以将一个特定项目显示为选中/聚焦?

一个用例可能是假设它是一个语言设置列表,当列表打开时,当前选择的语言必须显示为高亮显示。

如果我知道该项目的索引,如何将其设置为专注于显示?

4 个答案:

答案 0 :(得分:40)

我发布了我的解决方案,因为谷歌仍然不知道答案。

getListView().setItemChecked(selectedGroupIndex, true);

答案 1 :(得分:15)

简而言之,ListView :: setSelection(int position)就是您所需要的。但是,根据设备是否处于触摸模式,它可能有也可能没有视觉效果(背景突出显示)。有关详细信息,请参阅Android ListView Selection Problem

答案 2 :(得分:11)

如果您使用Adapter ListView将此代码添加到适配器:

public class MyAdapter extends ArrayAdapter<MyClass> {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflator.inflate(R.layout.my_adapter, null);
        } else {
            rowView = (View) convertView;
        }

        //...

        // set selected item
        LinearLayout ActiveItem = (LinearLayout) rowView;
        if (position == selectedItem){
            ActiveItem.setBackgroundResource(R.drawable.background_dark_blue);

            // for focus on it
            int top = (ActiveItem == null) ? 0 : ActiveItem.getTop();
            ((ListView) parent).setSelectionFromTop(position, top);
        }
        else{
            ActiveItem.setBackgroundResource(R.drawable.border02);
        }    
    }

    private int selectedItem;

    public void setSelectedItem(int position) {
        selectedItem = position;
    }    
}

Activity

myAdapter.setSelectedItem(1);

答案 3 :(得分:0)

我正在使用适配器并且不想设置自定义背景颜色,但在drawable xml中使用android:state_selected。 SetSelection对我不起作用,但也许这也是因为我需要SetNotifyDataChanged,这表明Selected State不是持久的。

我还发现ListView中项目的Selected状态不是持久的,因为SetNotifyDataChanged会导致更新ListView布局,从而清除它们。在适配器的GetView中将项目设置为Selected也太快了。

最后,在更改列表视图的布局之后,我为所选项目的视图设置了Selected状态,这是在触发LayoutChange事件时(在Java中,它可能附加到ListView的OnLayoutChangeListener)。

为了让它变得非常简单,我将所选项目的视图存储为Adapter的SelectedItemView。 在ListView的LayoutChange事件处理程序中,我只是将适配器的SelectedItemView.Selected设置为true。

这是我的Activity中的代码,我为ListView设置了适配器,也订阅了LayoutChange(或者在Java中附加了一个OnLayoutChangeListener)

        ringTonesListView.Adapter = ringTonesListAdapter;
        ringTonesListView.LayoutChange += (s, layoutChangeArgs) => {
            //At this stage the layout has been updated and the Selected can be set to true for the view of the selected item. This will result in android:state_selected logic to be applied as desired and styling can be completely done per layout in Resources.
            ringTonesListAdapter.SelectedItemView.Selected = true;
        };

这是我的适配器代码:

public class RingTonesListAdapter : BaseAdapter<RingToneItem>
{
    List<RingTone> Items { get; set; }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;

        // re-use an existing view, if one is available
        // otherwise create a new one
        if (view == null)
        {
            view = Context.LayoutInflater.Inflate(Resource.Layout.AlertSoundItem, parent, false);
            view.Click += SelectRingTone;
        }

        RingTone ringTone = this[position];
        if (ringTone.Selected)
        {
            //==> Important
            //Store this view since it's the view for the Selected Item
            SelectedItemView = view;
            //Setting view.Selected to true here doesn't help either, since Selected will be cleared after.
        }

        return view;
    }

    private void SelectRingTone(object sender, EventArgs args)
    {
        View view = (View)sender;
        string title = view.FindViewById<TextView>(Resource.Id.ringToneTitle).Text;
        RingToneItem ringToneItem = Items.First(rt => rt.Title == title);
        if (!ringToneItem.Selected)
        {
            //The RingTone was not selected and is selected now
            //Deselect Old and Select new
            foreach (RingToneItem oldItem in Items.Where(rt => rt.Selected))
            {
                oldItem.Selected = false;
            }

            // Select New RingTone
            ringToneItem.Selected = true;
            //Update the ListView. 
            //This will result in removal of Selected state for all Items when the ListView updates it's layout
            NotifyDataSetChanged();
        }

        //Now play the test sound
        NotifierService.TestSound(Context, ringToneItem);
    }

    public View SelectedItemView { get; set; }
}