gpt4 book ai didi

android - ListView 选择器颜色选择所有对象,而不是每个项目

转载 作者:行者123 更新时间:2023-11-30 01:56:27 27 4
gpt4 key购买 nike

我做了一个 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);
}
}

View 的自定义 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>

最佳答案

希望您的适配器 View 有一个父布局。

因此将 android:background="@drawable/drawer_list_selector" 给布局或 ImageView 或 TextView。

如果布局中有 ImageView 和 TextView,则可以为该布局设置背景。

希望对你有帮助。

关于android - ListView 选择器颜色选择所有对象,而不是每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32135877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com