【问题标题】:Can't highlight selected item in a ListView无法突出显示 ListView 中的选定项目
【发布时间】:2015-06-10 20:22:57
【问题描述】:

我无法突出显示 ListView 中的选定条目。我已按照http://www.michenux.net/android-listview-highlight-selected-item-387.html 中的程序进行操作。我也尝试了Android: setselector color for listview not working 中的建议,但无济于事。我尝试在 DialogFragment 和 Activity 中使用 ListView。

我注意到 (1) 我可以设置选择,但我无法查询 SelectedItemPosition,(2) onItemSelected 永远不会被调用,以及 (3) 我无法获取子视图。

这里有一些代码:

public class ChooseDirActivity extends Activity
{
  private ListView listView = null;

  @Override
  protected void onCreate( Bundle savedInstanceState )
  {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.choose_dir_layout );
    listView = (ListView)findViewById( R.id.directory_list );
    StateListDrawable selector = new StateListDrawable();
    selector.addState(new int[]{android.R.attr.state_selected}, new ColorDrawable( 0x808080 ));
    selector.addState( new int[]{ -android.R.attr.state_selected }, new ColorDrawable( 0xFFFFFF ) );
    listView.setSelector( selector );

    listView.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener()
    {
      @Override
      public void onItemSelected( AdapterView<?> adapterView, View view, int i, long l )
      {
        Log.i( "myApp",  "onItemSelected" );  // Never called
        view.setBackgroundColor( 0x808080 );
      }

      @Override public void onNothingSelected( AdapterView<?> adapterView ) { } } ); listView.setChoiceMode( ListView.CHOICE_MODE_SINGLE ); }

  @Override
  protected void onResume()
  {
    super.onResume();
    currentDirectory = Intonia.getStorageDirectory();
    ArrayList<String> dirList = getDirectoryList();
    int selected = 11;
    listView.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, dirList ) );
    Log.i( "myApp",  "setting selection to " + selected );
    listView.setSelection( selected );
    listView.setItemChecked( selected, true );
    Log.i( "myApp",  "selected item position is " + listView.getSelectedItemPosition() );
    View childView = listView.getChildAt( selected );
    if ( childView == null )
    {
      Log.i( "myApp",  "view at " + selected + " is null" );
    }
    else
      childView.setBackgroundColor( 0x808080  );
    Object so = listView.getItemAtPosition( selected );
    Log.i( "myApp",  "item at " + selected + " is " + ( so == null ? "null" : so.getClass().toString() ) );
  }

}

设置选择确实将所选项目移动到屏幕顶部,但似乎没有任何方法可以跟进。

这会产生以下日志输出

myApp: setting selection to 11
myApp: selected item position is -1
myApp: view at 11 is null
myApp: item at 11 is class java.lang.String

谁能帮帮我?

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    解决方案是使用ListActivity,并通过覆盖Adapter中的getView来设置背景颜色。设置选择会将选定的项目移动到用户屏幕的顶部,但在提供视图方面没有用。

    public class DCListActivity extends ListActivity
    {
        private int selected = 0;
    
        @Override
        protected void onCreate( Bundle savedInstanceState )
        {
            super.onCreate( savedInstanceState );
            ArrayList<String> dirList = getDirectoryList();
            selected = 11;
            setListAdapter( new MyAdapter( this, android.R.layout.simple_list_item_1, dirList ) );
            getListView().setSelection( selected );
        }
    
        private class MyAdapter extends ArrayAdapter<String>
        {
            public MyAdapter( Context context, int resource, List<String> objects )
            {
                super( context, resource, objects );
            }
    
            @Override
            public View getView( int position, View convertView, ViewGroup parent )
            {
                View v = super.getView( position, convertView, parent );
                if ( position == selected )
                    v.setBackgroundColor( getResources().getColor( R.color.background_highlight ) );
                else
                    v.setBackgroundColor( getResources().getColor( R.color.background ) );
                return v;
            }
        }
    

    C

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 2015-09-08
      • 1970-01-01
      • 2016-07-31
      • 2013-02-04
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 2011-08-16
      相关资源
      最近更新 更多