我有一个listview,我使用inflater填充。当我用一些颜色实现选择器时它实际上是listview项目的顶部,这很好,但它也影响listview的文本。 textfont颜色也会根据选择器颜色进行更改。
如何在不影响listview文本字体颜色的情况下实现选择器
答案 0 :(得分:0)
包含ListView的layout.xml:
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/listitem_selector"
/>
listitem_selector.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/listitem_pressed" />
<item android:state_focused="true" android:drawable="@drawable/listitem_selected" />
</selector
或者,您可以通过覆盖ListAdapter的getView()方法来设置View的选择器。查看更多here
它会帮助你!!
答案 1 :(得分:0)
Android:以编程方式将ListView的listSelector设置为当前主题的默认值
private static void setDefaultListSelector(ListView listView, Context context) {
TypedArray typedArray = context.obtainStyledAttributes(null, new int[]{android.R.attr.listSelector}, android.R.attr.listViewStyle, 0);
Drawable drawable = typedArray.getDrawable(0);
listView.setSelector(drawable);
}
答案 2 :(得分:0)
您可以检测当前所选项目的位置
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};
覆盖适配器的getView方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == mSelectedItem) {
// set your color
}
return view;
}