设置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;
	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值