有多种实现效果,可以根据情况来设定,今天先写一种。
下面请先看一下效图。
实现方法是通过给布局文件item设置背景色来实现的。
代码如下
MainActivity.java
package com.example.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView listView;
String string[] = { "a", "b", "c", "d", "e", "f", "g", "h" };
private LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listViewId);
ListViewAdapter adapter = new ListViewAdapter();
listView.setAdapter(adapter);
listView.setItemsCanFocus(true);// 让ListView的item获得焦点
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// 单选模式
// 默认第一个item被选中
listView.setItemChecked(0, true);
}
private class ListViewAdapter extends BaseAdapter {
public ListViewAdapter() {
super();
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(MainActivity.this);
}
@Override
public int getCount() {
return string.length;
}
@Override
public Object getItem(int position) {
return string[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.content = (TextView) view
.findViewById(R.id.textViewId);
view.setTag(viewHolder);
}
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.content.setText(string[position]);
return view;
}
}
private class ViewHolder {
private TextView content;
}
}
主布局文件:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
条目布局文件:item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
<strong>android:background="@drawable/list_selector"</strong>
tools:context=".MainActivity" >
<TextView
android:id="@+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textSize="20sp" />
</RelativeLayout>
以上布局文件中设置了背景色,下面就要在drawable文件夹中写这个list_selector文件了。
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
list_item_bg_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:startColor="@color/item_click"
android:endColor="@color/item_click"
android:angle="90" />
</shape>
list_item_bg_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- shape用于设定形状 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 渐变 -->
<gradient
android:angle="90"
android:endColor="@color/white"
android:startColor="@color/white" />
</shape>
由于这里的颜色值引用的color标签,所有要在values目录下生成colors文件,里面写上对应标签的颜色值。
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="item_click">#FF82AB</color>
</resources>