我正在使用listview
我跟随xml
跟随listitem
one.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#D5F4FF"/>
<stroke android:width="1dp" android:color="#979797"/>
<corners
android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>
<padding android:left="5dp" android:top="5dp"
android:right="5dp" android:bottom="5dp" />
</shape>
two.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFA500"/>
<stroke android:width="1dp" android:color="#979797"/>
<corners
android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>
<padding android:left="5dp" android:top="5dp"
android:right="5dp" android:bottom="5dp" />
</shape>
现在,默认情况下,我想将 one.xml
设置为我的listitem,当用户点击listitem
时,应设置 two.xml
它仍然保持不变,但它不起作用
我已尝试使用listselector
的{{1}}属性。
CustomAdapter
listview
答案 0 :(得分:0)
在selector xml
下方使用 view_selector.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape android:shape="rectangle">
<corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" />
<solid android:color="@color/view_pressed" />
</shape></item>
<item android:state_focused="true"><shape android:shape="rectangle">
<corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" />
<solid android:color="@color/view_pressed" />
</shape></item>
<item><shape android:shape="rectangle">
<corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" />
<solid android:color="@color/view_normal" />
</shape></item>
</selector>
并在colors.xml
中添加颜色:
<color name="view_pressed">#FFA500</color>
<color name="view_normal">#D5F4FF</color>
假设您有一个自定义适配器的示例,该适配器具有布局,您可以在根标记中将其设置为background
。举个例子:
<强> custom_adapter_layout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/view_selector" >
<TextView
style="@style/textviewstyle_Medium"
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:lines="2"
android:text="@string/textview1"/>
<TextView
style="@style/textviewstyle_small"
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center"
android:padding="2dp"
android:text="@string/textview2"/>
</LinearLayout>
答案 1 :(得分:0)
要使列表选择器起作用,您需要设置ListeView
选择模式:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
并覆盖
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// Make the newly clicked item the currently selected one.
getListView().setItemChecked(position, true);
}
注意:确保项目的布局支持选择,或使用android布局文件:
android.R.layout.simple_list_item_activated_1