Android中設定ListView的選中的Item的背景顏色
阿新 • • 發佈:2018-12-25
ListView中沒有預設的選擇顏色,只有選擇Item後的焦點顏色,滑鼠點選時Item有顏色,放開滑鼠後顏色也就沒有了,要實現放開滑鼠後選擇項的背景還是有顏色的
1、配置main.xml
Java程式碼- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- <ListView android:id="@+id/listView" android:listSelector="#000000"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
設定Item的獲取焦點顏色為白色android:listSelector="#000000"(即不顯示背景顏色)
2、配置用於ListView顯示Item的button_layout.xml
Java程式碼- <?xml version=
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/LinearLayoutButton"
- android:layout_width="144px"
- android:layout_height="99px"
- android:gravity="center"
- android:orientation="vertical">
- <TextView
- android:id="@+id/TextViewButton"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:textSize="20px">
- </TextView>
- </LinearLayout>
3、實現Activity
Java程式碼- package com.listButtonTest.www;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- publicclass listButtonTest extends Activity {
- private ListView listView = null;
- private ListAdapter listAdapter = null;
- /** Called when the activity is first created. */
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- listView = (ListView) this.findViewById(R.id.listView);
- ArrayList<ButtonView> buttonListView = new ArrayList<ButtonView>();
- ButtonView a = new ButtonView(R.string.l1);
- buttonListView.add(a);
- ButtonView b = new ButtonView(R.string.l2);
- buttonListView.add(b);
- ButtonView c = new ButtonView(R.string.l3);
- buttonListView.add(c);
- ButtonView d = new ButtonView(R.string.l4);
- buttonListView.add(d);
- ButtonView e = new ButtonView(R.string.l5);
- buttonListView.add(e);
- listAdapter = new ListAdapter(buttonListView);
- listView.setAdapter(listAdapter);
- listView.setDividerHeight(0);
- listView.setOnItemClickListener(new ListView.OnItemClickListener() {
- @Override
- publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- // TODO Auto-generated method stub
- listAdapter.setSelectedPosition(arg2);
- listAdapter.notifyDataSetInvalidated();
- }
- });
- };
- publicclass ListAdapter extends BaseAdapter {
- ArrayList<ButtonView> arrayList = null;
- LayoutInflater inflater;
- View view;
- ButtonLayoutHolder buttonLayoutHolder;
- LinearLayout buttonLayout = null;
- TextView buttonText = null;
- privateint selectedPosition = -1;// 選中的位置
- public ListAdapter(ArrayList<ButtonView> buttonListView) {
- // TODO Auto-generated constructor stub
- arrayList = buttonListView;
- }
- @Override
- publicint getCount() {
- // TODO Auto-generated method stub
- return arrayList.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return arrayList.get(position);
- }
- @Override
- publiclong getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- publicvoid setSelectedPosition(int position) {
- selectedPosition = position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view = inflater.inflate(R.layout.button_layout, null, false);
- buttonLayoutHolder = (ButtonLayoutHolder) view.getTag();
- if (buttonLayoutHolder == null) {
- buttonLayoutHolder = new ButtonLayoutHolder();
- buttonLayoutHolder.buttonLayout = (LinearLayout) view
- .findViewById(R.id.LinearLayoutButton);
- buttonLayoutHolder.textView = (TextView) view
- .findViewById(R.id.TextViewButton);
- view.setTag(buttonLayoutHolder);
- }
- buttonLayout = buttonLayoutHolder.buttonLayout;
- buttonText = buttonLayoutHolder.textView;
- if (selectedPosition == position) {
- buttonText.setSelected(true);
- buttonText.setPressed(true);
- buttonLayout.setBackgroundColor(Color.RED);
- } else {
- buttonText.setSelected(false);
- buttonText.setPressed(false);
- buttonLayout.setBackgroundColor(Color.TRANSPARENT);
- }
- buttonText.setTextColor(Color.WHITE);
- buttonText.setText(arrayList.get(position).textViewId);
- return view;
- }
- };
- }
- class ButtonView {
- int textViewId;
- ButtonView(int tId) {
- textViewId = tId;
- }
- }
- class ButtonLayoutHolder {
- LinearLayout buttonLayout;
- TextView textView;
- }
在listView的setOnItemClickListener事件中標記這次選擇的Item的下標:listAdapter.setSelectedPosition(arg2);
然後呼叫listAdapter.notifyDataSetInvalidated()通知後臺重新重新整理介面。
在ListAdapter的getView()方法中,如果是選中的Item則顯示背景顏色,如果不是則不顯示背景顏色。