ListView 选择器颜色选择所有对象,而不是每个项目
ListView selector color selects all object, not each item
我做了一个 ListView 并用自定义适配器填充它,现在我想用不同的颜色显示选择了什么元素,所以我用这段代码做了一个 drawer_list_selection.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="@color/primaryDark" />
<item android:state_activated="true" android:drawable="@color/primary" />
<item android:drawable="@color/grey_soft" />
</selector>
然后,我将我的 ListView 背景设置为这个文件:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="match_parent" android:choiceMode="singleChoice"
android:divider="@android:color/transparent" android:dividerHeight="0dp"
tools:context=".NavigationDrawerFragment" android:id="@+id/section_list"
android:background="@drawable/drawer_list_selector" />
我的问题是,当我按下一个部分(一个元素)时,整个 ListView 将转为@color/primary,并且激活的方法都不起作用。任何解决方案?也许是我的适配器的问题?这是顺便说一句:
public class adaptadorLista extends ArrayAdapter<Secciones>{
public adaptadorLista(Context context, Secciones[] section){
super(context,0,section);
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(getContext());
View item = inflater.inflate(R.layout.fragment_main, null);
ImageView img= (ImageView) item.findViewById(R.id.imageView_section);
img.setImageResource(section[position].getIcon());
TextView txt_section = (TextView) item.findViewById(R.id.section_label);
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Jaapokki-Regular.otf");
txt_section.setTypeface(tf);
txt_section.setText(section[position].getSec());
return (item);
}
}
视图的自定义 xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/drawer_list_selector"
tools:context=".MainActivity$PlaceholderFragment"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp" android:background="@drawable/drawer_list_selector">
<TableRow android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginRight="5sp"
android:id="@+id/imageView_section" android:src="@drawable/ic_d" android:scaleType="fitCenter"
android:maxHeight="20dp" android:maxWidth="20dp" android:adjustViewBounds="true"/>
<TextView android:id="@+id/section_label" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="hi" android:textSize="20sp"
android:textColor="#000"
/>
</TableRow>
</TableLayout>
</LinearLayout>
希望您的适配器视图有一个父布局。
所以给 android:background="@drawable/drawer_list_selector"
布局或 ImageView 或 TextView。
如果布局中有 ImageView 和 TextView,则可以为该布局设置背景。
希望对你有帮助。
你需要删除这些行。
<item android:state_activated="true" android:drawable="@color/primary" />
android:background="@drawable/list_selector"
并设置列表选择器不是背景
android:listSelector="@drawable/yourlist_selector"
并在 xml 的 listView 中设置 属性 android:drawSelectorOnTop="true"
.
我做了一个 ListView 并用自定义适配器填充它,现在我想用不同的颜色显示选择了什么元素,所以我用这段代码做了一个 drawer_list_selection.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="@color/primaryDark" />
<item android:state_activated="true" android:drawable="@color/primary" />
<item android:drawable="@color/grey_soft" />
</selector>
然后,我将我的 ListView 背景设置为这个文件:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="match_parent" android:choiceMode="singleChoice"
android:divider="@android:color/transparent" android:dividerHeight="0dp"
tools:context=".NavigationDrawerFragment" android:id="@+id/section_list"
android:background="@drawable/drawer_list_selector" />
我的问题是,当我按下一个部分(一个元素)时,整个 ListView 将转为@color/primary,并且激活的方法都不起作用。任何解决方案?也许是我的适配器的问题?这是顺便说一句:
public class adaptadorLista extends ArrayAdapter<Secciones>{
public adaptadorLista(Context context, Secciones[] section){
super(context,0,section);
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(getContext());
View item = inflater.inflate(R.layout.fragment_main, null);
ImageView img= (ImageView) item.findViewById(R.id.imageView_section);
img.setImageResource(section[position].getIcon());
TextView txt_section = (TextView) item.findViewById(R.id.section_label);
Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Jaapokki-Regular.otf");
txt_section.setTypeface(tf);
txt_section.setText(section[position].getSec());
return (item);
}
}
视图的自定义 xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/drawer_list_selector"
tools:context=".MainActivity$PlaceholderFragment"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp" android:background="@drawable/drawer_list_selector">
<TableRow android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginRight="5sp"
android:id="@+id/imageView_section" android:src="@drawable/ic_d" android:scaleType="fitCenter"
android:maxHeight="20dp" android:maxWidth="20dp" android:adjustViewBounds="true"/>
<TextView android:id="@+id/section_label" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="hi" android:textSize="20sp"
android:textColor="#000"
/>
</TableRow>
</TableLayout>
</LinearLayout>
希望您的适配器视图有一个父布局。
所以给 android:background="@drawable/drawer_list_selector"
布局或 ImageView 或 TextView。
如果布局中有 ImageView 和 TextView,则可以为该布局设置背景。
希望对你有帮助。
你需要删除这些行。
<item android:state_activated="true" android:drawable="@color/primary" />
android:background="@drawable/list_selector"
并设置列表选择器不是背景
android:listSelector="@drawable/yourlist_selector"
并在 xml 的 listView 中设置 属性 android:drawSelectorOnTop="true"
.