1. 程式人生 > >設定ListView單擊和選中背景顏色

設定ListView單擊和選中背景顏色

需要自定義ListView單擊和選擇時候的背景顏色,寫一個Selector的資原始檔,設定為這個ListView的背景就可以了

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <!-- 正常狀態下的背景 -->
    <item android:state_window_focused="false" android:drawable="@drawable/bg_listview_item"></item>
    
    <!-- 單擊的背景 -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/bg_listiview_tem__selected"></item>
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/bg_listiview_tem__selected"></item>
    
    <!-- 長按選中的背景 -->
    <item android:state_selected="true" android:drawable="@drawable/bg_listiview_tem__selected"></item>
    <item android:state_focused="true" android:drawable="@drawable/bg_listiview_tem__selected"></item>
</selector>

有時候在Drawer中選單的選擇中,我們需要改變選擇一項對應的文字的顏色和這個文字的背景,以提示使用者選擇項是哪個。

關於文字的背景,寫一個Selector檔案來定義drawer_item_selector.xml

<item  android:state_selected="true" android:drawable="@drawable/bg_drawer_item_selected"></item>
關於文字的顏色,也是寫一個Selector檔案來定義drawer_text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item  android:state_selected="true" android:color="@android:color/white"></item>
    <item  android:color="#aaaaaa"></item>
</selector>

然後,只需要在Adapter中設定對應TextView的選擇狀態就好
package com.corer.bluudribbo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.corer.bluudribbo.MyApplication;
import com.corer.bluudribbo.bean.Category;
import com.corer.bluutestdribbo.R;

public class DrawerAdapter extends BaseAdapter {
	
	private ListView mListView;
	
	public DrawerAdapter(ListView listView){
		mListView=listView;
	}

	@Override
	public int getCount() {
		return Category.values().length;
	}

	@Override
	public Category getItem(int position) {
		return Category.values()[position];
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView==null) {
			convertView=LayoutInflater.from(MyApplication.getGloblaContext()).inflate(R.layout.listview_item_drawer, null);
		}
		TextView textView=(TextView) convertView.findViewById(R.id.txt);
		textView.setText(getItem(position).getName());
		textView.setSelected(mListView.isItemChecked(position));
		return convertView;
	}

}

listview_item_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <TextView 
        android:id="@+id/txt"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="15dip"
        android:background="@drawable/drawer_item_selector"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="3dip"
        android:paddingBottom="3dip"
        android:textColor="@drawable/drawer_text_selector"
        android:layout_gravity="center"/>

</FrameLayout>

在DrawerFragment中
package com.corer.bluudribbo.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.corer.bluudribbo.acticity.MainActivity;
import com.corer.bluudribbo.adapter.DrawerAdapter;
import com.corer.bluutestdribbo.R;

public class DrawerFragment extends Fragment {
	private ListView mListView;
	private MainActivity mActivity;
	private DrawerAdapter mAdapter;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActivity=(MainActivity) getActivity();
		View containView=inflater.inflate(R.layout.fragment_drawer, null);
		mListView=(ListView)containView.findViewById(R.id.listview);
		mAdapter=new DrawerAdapter(mListView);
		mListView.setAdapter(mAdapter);
		mListView.setItemChecked(0, true);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				//set item textcolor
				mListView.setItemChecked(position, true);
				//change fragment
				mActivity.swichFragment(position);
			}
		});
		
		return containView;
	}

}