在Android中为选定的ListView项设置背景颜色

14

我有一个显示多个项的列表视图。现在我想要滚动到一些特定的项(例如第33个项目)。我知道可以通过以下方式实现:

myList.setSelection(32);

但是在用户界面上该项没有接收到任何高亮显示(因为它处于触摸模式?!)。如何为此项应用特定的背景颜色?我尝试过

myList.getSelection().getSelectedView().setBackgroundColor(Color.Red);

但是我收到了一个空指针异常,因为getSelectedView()返回null。有没有办法实现这种高亮?我必须以某种方式通知用户哪个项目是“活动”的...

但是由于getSelectedView()返回null,我遇到了NullPointerException异常。是否有方法可以实现此突出显示?我必须以某种方式向用户通知哪个项目是“活动”的...

7个回答

3
  1. 为了突出显示所选项,您应该请求listView的焦点,并在UI线程中运行setSelection()。以下方法对我有效:
runOnUiThread(new Runnable() {
    public void run() {
        myList.requestFocus();
        myList.setSelection(position);
    }
});

2. 给一个项目应用特定的颜色。您可以使用自定义的listItem。

a. 为您的listView设置自定义的listItem:

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.customizedlistitem,arrListView);
myList.setAdapter(listAdapter);
b. 在您的布局文件夹中,创建customizedlistitem.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/customizedlistitem"   
    android:layout_width="match_parent"   
    android:layout_height="wrap_content"
    android:background="@drawable/customizedbackground"/>   

在你的drawable文件夹中创建customizedbackground.xml,内容如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/RED"></item>
    <item android:state_pressed="true" android:drawable="@color/GREEN"></item>      
    <item android:drawable="@color/BLACK"></item>   <!-- for other state -->        
</selector>

d. 确保在您的项目中定义了颜色RED、GREEN和BLACK(您可以在values文件夹下的color.xml中定义):

<?xml version="1.0" encoding="utf-8"?>

<resources>  
     <color name="RED">#FF0000</color>
     <color name="GREEN">#008000</color>
     <color name="BLACK">#000000</color>
</resources>

3

试试这个,它会有效-

myList.getChildAt(myList.getSelectedItemPosition()).setBackgroundColor(Color.RED);

1
在xml文件中的ListView:
<ListView android:id="@+id/android:list" android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@+id/Tablayoutdesign"
        android:cacheColorHint="#000000"
        android:dividerHeight="1dip"
        android:layout_marginTop="63dip"
        android:layout_marginBottom="40dip"

        />

创建一个名为“listselector”的新XML,保留HTML格式,不做解释。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_selected="false" 
    android:drawable="@drawable/focused"/> 

  <!-- Pressed -->
  <item 
    android:state_selected="true" 
    android:state_focused="false"
    android:drawable="@drawable/selected" /> 

</selector>

创建一个名为 colors.xml 的文件,内容如下:
<resources>
    <drawable name="focused">#ff5500</drawable>
    <drawable name="selected">#FF00FF</drawable>
</resources>

在你的Java代码中:

ListView lv= (ListView) findViewById(R.id.list);
lv.setSelector( R.drawable.listselector);

0

更改该行

myList.getSelection().getSelectedView().setBackgroundColor(Color.Red);

myList.getSelectedView().setBackgroundColor(getResources().getColor(Color.RED));

1
我仍然收到NullPointerException。您可以通过扩展listview-tutorial自行尝试:http://developer.android.com/resources/tutorials/views/hello-listview.html - codie4711

0

我尝试了以下方法并解决了问题。

创建了一个接收位置的JavaBean类,如下所示。

public class Global {

    public static int mListPosition = 0;

    public static int getListPosition() {
        return mListPosition;
    }

    public static void setListPosition(int mListPosition) {
        Global.mListPosition = mListPosition;
    }
}

然后在 OnListItemClickListener() 中,我设置所选项目的位置。就像下面这样

mList.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Global.setListPosition(arg2);                   
    }
});

然后在适配器类中执行以下操作

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) mCtx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.details_retailer_list_row, null);

        mHolder = new ViewHolder();
        v.setTag(mHolder);

        mHolder.mDetailsRetailerLayout = (LinearLayout) v
                .findViewById(R.id.details_cart_retailer_row_layout);
        mHolder.mDetailsRetailer = (TextView) v
                .findViewById(R.id.detailsRetailerRow);

    } else {
        mHolder = (ViewHolder) v.getTag();
    }

    final CartDetailsRetailerBean mDetailsRetailerBean = mItems
            .get(position);
    if (position == Global.getListPosition()) {
        mHolder.mDetailsRetailerLayout
                .setBackgroundResource(R.drawable.image1);
    } else {
        mHolder.mDetailsRetailerLayout
                .setBackgroundResource(R.drawable.image2);
    }

    if (mDetailsRetailerBean != null) {
        Log.i("Global Position", "" + Global.getListPosition());
        mHolder.mDetailsRetailer.setText(mDetailsRetailerBean
                .getRetailerName());
    }
    return v;
}    

尝试在Android列表视图中更改所选行的背景颜色。


0

NullPointerException 可能是因为您处于 touchmode 模式,当您处于 touch mode 时,setSelection 实际上并不会选择 listView 上的项目,它只是“查看”它,如果您知道我的意思。

您需要创建一个自定义适配器(通过创建一个扩展当前使用的适配器的类 :])

在您的自定义适配器类中,您必须重写 getView(int position, View convertView, ViewGroup parent) 方法,像这样:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
    String content = lista.get(position).toString();
    TextView text = (TextView) convertView.findViewById(android.R.id.text1);
    text.setText(content);
    convertView.setBackgroundColor(lista.get(position).getBackgroundColor());
    return convertView;
}

在代码中,变量"lista"是我在适配器上拥有的一些项目的列表。
因此,当你完成选择你的项目后,你必须为每个项目调用getView方法,或者你可以在设置选择之前从你的listView调用invalidateViews方法(如果你在选择之后调用invalidateViews,选择将被忽略(至少在触摸模式下:]))。

0

可能最简单的方法是使用State List Drawable作为您的背景。这使您能够根据所选控件是否被选择/聚焦/禁用等应用不同的样式。

如果想了解State List Drawables的介绍,请查看this article


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接