【问题标题】:How to keep the selected item highlighted in ListActivity如何在 ListActivity 中保持所选项目突出显示
【发布时间】:2013-04-07 05:43:34
【问题描述】:

我创建了一个从 ListActivity 类派生的类 RecordActivity,并为 .xml 中定义的 ListView 设置选择模式和选择器。默认行为是仅当我按下所选项目时才会突出显示。我想保持选定的项目突出显示。我试图覆盖 ArrayAdapter 的 getView 方法,但是它不起作用。任何帮助将不胜感激。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

   <ListView  android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>     
</LinearLayout>

public class RecordsActivity extends ListActivity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.records);

      List<Record> values = getAllRecords();
      // Use the SimpleCursorAdapter to show the
      // elements in a ListView
      adapter = new ArrayAdapter<Record>(this, android.R.layout.simple_list_item_1, 
                                                            values);
      setListAdapter(adapter);    
      getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      getListView().setSelector(android.R.color.holo_red_dark);
   }

   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {
      selectedItem = position;
      v.setSelected(true);      
   }
}

【问题讨论】:

标签: android highlight listactivity


【解决方案1】:

尝试使用simple_list_item_activated_1

adapter = new ArrayAdapter<Record>(this, android.R.layout.simple_list_item_activated_1, 
                                                        values);

【讨论】:

    【解决方案2】:

    试试这个,在蜂窝或更高版本上,所选项目将保持突出显示。由于 android.R.layout.simple_list_item_activated_1 仅适用于蜂窝或更高版本,您应该添加这样的布局以支持旧平台。

    int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
                    : android.R.layout.simple_list_item_1;
    
    
    adapter = new ArrayAdapter<Record>(this, layout, values);
    

    【讨论】:

      【解决方案3】:
          private int selectedValue;
          private View row;
      
        ListView.setOnItemClickListener(new OnItemClickListener() {
      
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                      long arg3) {
                  // TODO Auto-generated method stub
                  selectedValue=arg2;
                  if(row!=null)
                  {
                                      row.setBackgroundResource(R.drawable.group_item_normal);
                  }
                  row=arg1;
              arg1.setBackgroundResource(R.drawable.group_item_pressed);
      
              }
          });
      

      在getView()中做:- v是getView()返回的对象

        if(selectedValue==position)
              {
                  v.setBackgroundResource(R.drawable.group_item_pressed);
      
              }
              else{
                  v.setBackgroundResource(R.drawable.group_item_normal);
      
              }
      

      【讨论】:

      • 是的,它用于列表视图中突出显示的一行。如果您想要多个然后使用arrayList。
      猜你喜欢
      • 1970-01-01
      • 2013-04-17
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2019-07-07
      相关资源
      最近更新 更多