15

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

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

如果我知道项目的索引,如何将其设置为集中显示?

4

4 回答 4

41

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

getListView().setItemChecked(selectedGroupIndex, true);
于 2013-06-18T14:42:19.327 回答
15

简而言之, ListView::setSelection(int position) 就是您所需要的。但是,根据设备是否处于触摸模式,它可能有也可能没有视觉效果(背景突出显示)。更多详情请参考Android ListView 选择问题

于 2010-10-08T07:12:54.187 回答
13

如果您使用 a将此代码添加到AdapterListView的适配器:

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);
于 2012-04-23T08:45:07.557 回答
0

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

我还发现 ListView 中某个项目的 Selected 状态不是持久的,因为 SetNotifyDataChanged 会导致更新 ListView 布局,从而将它们全部清除。在 Adapter 的 GetView 中将项目设置为 Selected 也为时过早。

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

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

这是我的 Activity 中的代码,我在其中为 ListView 设置了 Adapter 并订阅了 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; }
}
于 2016-04-02T21:38:58.243 回答