如何在启动时将ListView的第一项选择为默认值?

时间:2012-03-06 03:35:12

标签: android android-layout android-listview

我的应用程序中有一个ListView,ListView有一个选择器。我想让这个ListView的第一项在启动App 时选择作为默认,如何?谁能提供一些提示? THX很多。

13 个答案:

答案 0 :(得分:10)

yourlist.setItemChecked(position,true)

答案 1 :(得分:6)

您可以通过

执行此操作
yourListView.setSelection(0);
yourListView.getSelectedView().setSelected(true);

我希望这会对你有所帮助

答案 2 :(得分:4)

这项工作对我而言。我希望这能帮到您。 :)

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    // do someting...
    if(position == 0){
        mListView.performItemClick(convertView, 0, 0);
    }

 }

现在我找到了另一个实现这个目标的好方法。

  • 首先,你应该设置ListView android:choiceMode = singleChoice
  • 其次,请按照以下步骤进行,

        int defaultPositon = 0;
        int justIgnoreId = 0;
        mListView.setItemChecked(defaultPositon, true);
        mListView.performItemClick(mListView.getSelectedView(), defaultPositon, justIgnoreId);
    

答案 3 :(得分:2)

//put the below code in get view function 
if(position==0)
{
    convertView.setSelected(true); 
}

答案 4 :(得分:2)

我认为这将起作用,因为我们给listView

提供了0个数组索引
ListView.setSelection(0);

答案 5 :(得分:1)

创建onItemClickListener然后使用此代码段,填写相应的值:

listView.performItemClick(View view, int position, long id);

See the docs了解更多详情。

答案 6 :(得分:1)

您可以致电listview.setSelection(0);。只需确保在填写List后填写此内容。如果您在填充List之前调用它,它将无效,因为它当时没有任何数据。

答案 7 :(得分:1)

以下解决方案对我有用:

设置背景/查看ID获取视图&使用列表视图的setOnItemClickListener

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)
                ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.listitems, null);
    }
    ListItem m = items.get(position);

    if (m != null) {

        TextView txt = (TextView)v.findViewById(R.id.textView);
        txt.setText(m.Item);

    }

    // set selected item
    LinearLayout ActiveItem = (LinearLayout) v;
    v.setId(position);
    if (position == GetDeviceDetails.selectedsize)
    {
        ActiveItem.setBackgroundColor(0xFF7F8184);
    }
    return v;
}

在创建活动中:

listView1.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
                    adapter.findViewById(GetDeviceDetails.selectedsize).setBackgroundColor(0xFFFFFFF);
                    GetDeviceDetails.selectedsize = position;
                    adapter.findViewById(position).setBackgroundColor(0xFF7F8184);
                    Log.d("Selected Id", "" + v.getId());
                    Log.d("find Selected Id", "" + adapter.findViewById(0));
                }
            });
    listView1.setSelection(0);
    listView1.setItemChecked(0, true);

答案 8 :(得分:0)

如果您想单击它以完成其他事件处理,则此方法有效:

listView.getAdapter().getView(position, null, null).performClick();

答案 9 :(得分:0)

您可以像这样修复它。

//set list adapter, then
lv.setItemChecked(1, true);//position 1
lv.setSelection(1);//position 1

单一选择更新XML布局。

<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"  
    android:background="@drawable/list_item_background"
    android:cacheColorHint="#FFFFFF"
    android:choiceMode="singleChoice"
    android:dividerHeight="1dip" >
</ListView>

并将listview后台XML设置为list_item_background.xml,

<?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@drawable/bg_list_default"   android:state_activated="false"/>
     <item android:drawable="@drawable/bg_list_pressed" android:state_pressed="true"/>
     <item android:drawable="@drawable/bg_list_pressed" android:state_activated="true"/>

答案 10 :(得分:0)

这适用于我。您必须制作自定义适配器。

     First Create Modal Class

        public class FilterBean {

         String filter_catagory;
                 boolean isSelected;

            public boolean isSelected() {
                return isSelected;
            }

            public void setSelected(boolean selected) {
                isSelected = selected;
            }

          public String getFilter_catagory() {
                return filter_catagory;
            }

            public void setFilter_catagory(String filter_catagory) {
                this.filter_catagory = filter_catagory;
            }

        }


    In Adapter Class

    @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            convertView = layoutInflater.inflate(R.layout.size_name_indicator, null);
            txt = (TextView) convertView.findViewById(R.id.key_textView);
            txt.setText(getItem(position).getFilter_catagory());

            if(getItem(position).isSelected()){
                txt.setBackgroundColor(context.getResources().getColor(R.color.transparent));
            }else {

                txt.setBackgroundColor(context.getResources().getColor(R.color.colorWhite));
            }


            return convertView;
        }

     public void updateNameBean(String key,boolean checked)
        {

            for(int i=0;i<getCount();i++)
            {
                FilterBean filterBean =  getItem(i);
                if(filterBean.getFilter_catagory().equals(key))
                {
                    filterBean.setSelected(checked);
                }else {
                    filterBean.setSelected(false);
                }
            }
            notifyDataSetChanged();
        }

In Filter Activity or Fragment

 filter_listView.setAdapter(filterNameAdapter);
               filterNameAdapter.updateNameBean(filterNameAdapter.getItem(0).getFilter_catagory(),true);
        filter_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String key = filterNameAdapter.getItem(position).getFilter_catagory();
                filterNameAdapter.updateNameBean(filterNameAdapter.getItem(position).getFilter_catagory(),true);

            }
        }); 

答案 11 :(得分:0)

我已经使用数据绑定来解决这个问题,

使用以下代码:

private List<Unit> _units;
private Unit _selectedUnit;
public Unit selectedUnit{
  get{
    if (_selectedUnit is null){
      return _units.Count > 0 ? _units[0] : null;
    }else{
      return _selectedUnit;
    }
  }
  set{
    if (_selectedUnit != value){
      _selectedUnit = value;
      OnPropertyChanged("selectedUnit");
    }
  }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName){
  var changed = PropertyChanged;
  if (changed != null){
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }
}

和XAML

<ListView ItemsSource="{Binding units}"
          SelectedItem="{Binding selectedUnit, Mode=TwoWay}">

答案 12 :(得分:-1)

if(position == 0){
        // This is the first item, you need to select this
        //rowview which u inflate under getview 
        rowView.performClick();

    }

我认为这会对你有所帮助。它对我有用