需要自定义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;
}
}