无法突出显示ListView中的所选项目

时间:2015-06-10 20:22:57

标签: android android-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

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

解决方案是使用ListActivity,并通过覆盖适配器中的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