通常在简单的ArrayAdapter中创建ListView
时。无需额外设置即可自动获得此突出显示功能。但是,当我使用自定义ListView
创建此CursorAdapter
时,此功能似乎缺失,我找不到解决方法。
这是我的自定义CursorAdapter public class RecentCallAdapter扩展了CursorAdapter {
private final String tag = this.getClass().getSimpleName();
public RecentCallAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
// TODO Auto-generated constructor stub
}
private static class ViewHolder {
TextView name;
TextView date;
int nameCol;
int dateCol;
int numberCol;
Calendar cal;
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
// TODO Auto-generated method stub
ViewHolder holder = (ViewHolder) v.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.name = (TextView) v.findViewById(R.id.recentcall_item_name);
holder.date = (TextView) v.findViewById(R.id.recentcall_item_date);
holder.nameCol = cursor.getColumnIndex(Calls.CACHED_NAME);
holder.dateCol = cursor.getColumnIndex(Calls.DATE);
holder.numberCol = cursor.getColumnIndex(Calls.NUMBER);
holder.cal = Calendar.getInstance();
v.setTag(holder);
}
String name = cursor.getString(holder.nameCol);
if(name == null){
name = cursor.getString(holder.numberCol);
}
holder.name.setText(name);
holder.cal.setTimeInMillis(Long.valueOf(cursor.getString(holder.dateCol)));
holder.date.setText(Utility.calculateTimePass(holder.cal.getTime()));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.recentcall_item, parent, false);
bindView(v, context, cursor);
return v;
}
有什么方法可以解决这个问题? 感谢。
答案 0 :(得分:1)
使用选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@android:drawable/list_selector_background">
</item>
<item android:state_focused="true" android:state_selected="true" android:drawable="@android:drawable/list_selector_background">
</item>
</selector>
并在listview标签中使用此代码设置选择器
android:drawSelectorOnTop="true"
android:listSelector="@drawable/selector"
答案 1 :(得分:0)
我会举一个适合我的例子。
要在按下列表视图项时保持其颜色,请在
中包含以下行listview布局:
android:background="@drawable/bg_key"
然后在drawable文件夹中定义bg_key.xml,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
最后,将其包含在listview onClickListener:
中listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
...
}
}
这样,任何时候都只会选择一个项目进行颜色选择。您可以使用以下内容在res / values / colors.xml中定义颜色值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed_color">#4d90fe</color>
<color name="default_color">#ffffff</color>
</resources>
答案 2 :(得分:0)
在自定义布局中,将背景设置为android:background="@drawable/bkg"
bkg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/pressed" />
<item android:state_focused="false"
android:drawable="@drawable/normal" />
</selector>
normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#10EB0A"/>
<stroke android:width="3dp"
android:color="#0FECFF" />
</shape>
pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF1A47"/>
<stroke android:width="3dp"
android:color="#0FECFF"/>
</shape>