Vel*_*lja 8 xml android listview listadapter android-listview
我一直在制作一个与Android中的ListViews一起使用的应用程序,我不能这样做,以便所选(chacked)项具有不同的背景.我正在使用CHOICE_MODE_SINGLE.这是我的代码到目前为止的样子:
我使用的ListView :(
在layout.xml内)
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:listSelector="@drawable/selector_test" >
</ListView>
Run Code Online (Sandbox Code Playgroud)
我在适配器中使用的TextView布局:
(listItem.xml)
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textStyle="bold" />
Run Code Online (Sandbox Code Playgroud)
这是我添加适配器的地方:
mListAdapter = new ArrayAdapter<String>(this, R.layout.listItem, mList);
mListView = (ListView) findViewById(R.id.listView);
mListView.setAdapter(mAuthorAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
String selected = mList.get(position);
// ...
mListView.setItemChecked(position, true);
}
});
Run Code Online (Sandbox Code Playgroud)
我确定在点击时检查了正确的项目,因为当我调用getCheckedItemPosition()时,它会返回正确的值.
现在,我试图突出检查项目的两件事:
选择器drawable:
(selector_test.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="@android:integer/config_longAnimTime">
<item android:state_checked="true"><shape>
<solid android:color="@color/red" />
</shape></item>
<item android:state_selected="true"><shape>
<solid android:color="@color/blue" />
</shape></item>
<item android:state_pressed="true"><shape>
<solid android:color="@color/green" />
</shape></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
我将它添加到.xml中:
android:listSelector="@drawable/selector_test"
Run Code Online (Sandbox Code Playgroud)
背景drawable:
(background_test.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/red" android:state_checked="true"/>
<item android:drawable="@color/blue" android:state_selected="true"/>
<item android:drawable="@color/green"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
我将它添加到.xml中:
android:background="@drawable/background_test"
Run Code Online (Sandbox Code Playgroud)
我已经尝试将选择器和背景添加到listView.xml和listItem.xml中,但唯一改变的是默认背景颜色,以及按下(或保持)项目时选择器的颜色.
android:state_checked ="true"和android:state_selected ="true"似乎什么都不做.
我可以通过覆盖ArrayAdapter中的getView()方法并在其中调用setBackgroundColor()来更改背景(如果选择了视图),它确实更改了背景,但也完全摆脱了选择器.另外,我真的不想仅仅为了改变一行代码来覆盖类,特别是如果可以以不同的方式实现同样的事情.
所以我要问的是,有没有办法通过添加选择器或背景可绘制来突出显示ListView中的选中项目,我只是做错了,或者我必须让它以其他方式工作.
提前致谢!:)
小智 8
在活动的onStart中添加此行
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Run Code Online (Sandbox Code Playgroud)
其中lv是listView的实例
然后重写此方法并向其添加以下行.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Set the item as checked to be highlighted
lv.setItemChecked(position, true);
v.setBackgroundColor(Color.BLUE);
conversationAdapter.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)
然后在自定义适配器的getView方法中将先前所选项目背景的颜色更改为正常
试试这个:
listViewDay.setItemChecked(position, true);
listViewDay.setSelection(position);
Run Code Online (Sandbox Code Playgroud)
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/list_day_selector" >
<TextView
android:id="@+id/txtItemDay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="22"
android:textColor="@android:color/white"
android:textSize="22sp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/bg_with_left_arrow" android:state_pressed="true"/>
<item android:drawable="@drawable/bg_with_left_arrow" android:state_activated="true"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46348 次 |
| 最近记录: |