是否可以通过列表选择器将自定义背景应用于每个Listview项?
默认选择器指定@android:color/transparent了state_focused="false"大小写,但将其更改为某个自定义drawable不会影响未选中的项目.Romain Guy似乎在这个答案中建议这是可能的.
我目前正在通过在每个视图上使用自定义背景并在选择/聚焦项目时隐藏它来实现相同的效果,以便显示选择器,但是在一个地方将所有这些都定义为更优雅.
作为参考,这是我用来试图让它工作的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:drawable="@drawable/list_item_gradient" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false"
android:state_pressed="true"
android:drawable="@drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="@drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />
<item android:state_focused="true"
android:drawable="@drawable/list_selector_background_focus" />
</selector>
Run Code Online (Sandbox Code Playgroud)
这就是我设置选择器的方式:
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@drawable/list_selector_background" />
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助!
已经在SO上阅读了很多相关问题,并通过Android文档和源代码查看了这一点,但是我很难过,尽管listSelector似乎只对所选项目应用样式,但是我没有震惊......
我在main.xml中定义了Listview:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdgeLength="5dp"
android:divider="#000000"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:singleLine="false"
android:text="@string/message_list_empty" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
引用的listSelector在这里(主要借用SDK中的默认Android状态列表:/android/platforms/android-8/data/res/drawable/list_selector_background.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- the window has lost focus, disable the items -->
<item
android:state_window_focused="false"
android:drawable="@drawable/shape_row_disabled" />
<!-- the list items are disabled -->
<item
android:state_enabled="false"
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/shape_row_disabled" />
<item
android:state_enabled="false"
android:state_focused="true"
android:drawable="@drawable/shape_row_disabled" />
<item
android:state_enabled="false"
android:drawable="@drawable/shape_row_disabled" />
<!-- the list items are enabled and being pressed -->
<item …Run Code Online (Sandbox Code Playgroud) 我知道这听起来很简单,对此有疑问.但它都不能解决我的问题.所以我们走了:
我希望ListActivity在用户点击它时更改列表项的背景颜色,并在用户再次单击时将其更改回原始颜色(即选择/取消选择项目排序)
我尝试使用getChildAt,如果我在一个屏幕上看到所有项目而不必滚动,它就能很好地工作.
码:
getListView().getChildAt(position).setBackgroundColor(Color.CYAN);
Run Code Online (Sandbox Code Playgroud)
当我在列表中有更多项目并且用户必须滚动它们时,问题就开始了.一旦项目的背景发生变化,当我滚动时,背景颜色会显示在新显示的项目上.此外,再次单击该项目时getChildAt(position)返回null(以及因此a NullPointerException).
任何人都可以帮我一个简单的代码,帮助我改变列表项的背景颜色?
提前致谢!
我想创建一个非常简单的游标自定义游标适配器,以方便在点击时更改行项目的颜色.使用以下代码
private static int save = -1;
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.getChildAt(position).setBackgroundColor(Color.BLUE);
if (save != -1 && save != position){
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
}
save = position;
}
Run Code Online (Sandbox Code Playgroud)
我从这个线程获得了代码/sf/answers/535491631/
我会使用一个简单的游标适配器并将代码放在onClick中,但由于ListFragment中的默认列表重用了视图,因此滚动多个视图时会突出显示.谈到IRC,有人建议我创建一个自定义游标适配器.但是,我似乎无法找到如何执行此操作的最佳实践,以及上述代码段所适用的位置.可以非常感谢帮助.
public class AreaCursorAdapter extends CursorAdapter {
private Context context;
public AreaCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView list_item = (TextView)view.findViewById(android.R.id.text1);
list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME)));
}
@Override
public View newView(Context context, …Run Code Online (Sandbox Code Playgroud) android android-listview simplecursoradapter android-listfragment android-cursoradapter
我正在为listview构建一个自定义适配器 - 我希望这个适配器为listview提供交替的背景颜色.
boolean alternate = false;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (alternate)
{
// set background colour of this item?
}
alternate = !alternate;
// lots of other stuff here
return convertView; }
Run Code Online (Sandbox Code Playgroud)
如何在上下文中设置listview项的背景?
我只需要一个想法或路径继续搜索,我有一个项目列表,我添加到listview适配器.我试图在列表中创建一些项目,以便在活动加载时具有特定的背景颜色(作为示例).像"预先选择"这些行一样的东西.这种方法public ListView getListView()对我帮助不大.谢谢你.
我制作了一个包含图像和textview的自定义列表.在设置样式时,选择器不可见.使用setTheme(R.style.rose)调用该样式; 在创建Listactivity的方法作为第一个调用.但是,如果没有使用颜色作为背景(或注释样式中的背景线),则橙色选择器可见.但不是在背景的时候
listSelector
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/list_selector_background_transition" />
<item android:state_pressed="true"
android:drawable="@drawable/list_selector_background_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/list_selector_background_focus" />
Run Code Online (Sandbox Code Playgroud)
样式
<style name="rose">
<item name="android:textColor">@color/pink</item>
<item name="android:background">@color/rose</item>
<item name="android:cacheColorHint">@color/rose</item>
<item name="android:listSelector">@drawable/listitem_selector</item>
</style>
Run Code Online (Sandbox Code Playgroud)