所以,我有一个ListFragment
的自定义布局,其自定义CursorAdapter
将自定义视图传递给ListView
。 ListView
的选择模式设置为MultiChoiceModal
。但是,从不突出显示所选项目,无论我是在正常模式下选择一个项目还是在操作模式下选择多个项目。为了解决这个问题,我尝试了一个选择器。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/holo_blue_dark"
android:state_pressed="true" />
<item android:color="@android:color/holo_blue_light"
android:state_checked="true" />
<item android:color="@android:color/holo_blue_bright"
android:state_selected="true" />
</selector>
然而,将此应用于ListView
作为列表的List Selector
只会导致应用崩溃并在我尝试运行时出现此错误:
04-04 15:01:41.196: E/AndroidRuntime(849): FATAL EXCEPTION: main
04-04 15:01:41.196: E/AndroidRuntime(849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mowdowndevelopments.vault/com.mowdowndevelopments.vault.AccountListActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ListView
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.os.Looper.loop(Looper.java:137)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-04 15:01:41.196: E/AndroidRuntime(849): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 15:01:41.196: E/AndroidRuntime(849): at java.lang.reflect.Method.invoke(Method.java:511)
04-04 15:01:41.196: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 15:01:41.196: E/AndroidRuntime(849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 15:01:41.196: E/AndroidRuntime(849): at dalvik.system.NativeStart.main(Native Method)
04-04 15:01:41.196: E/AndroidRuntime(849): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ListView
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
04-04 15:01:41.196: E/AndroidRuntime(849): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-04 15:01:41.196: E/AndroidRuntime(849): at com.mowdowndevelopments.vault.AccountListFragment.onCreateView(AccountListFragment.java:121)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.Fragment.performCreateView(Fragment.java:1695)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.Activity.onCreateView(Activity.java:4717)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-04 15:01:41.196: E/AndroidRuntime(849): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.Activity.setContentView(Activity.java:1881)
04-04 15:01:41.196: E/AndroidRuntime(849): at com.mowdowndevelopments.vault.AccountListActivity.onCreate(AccountListActivity.java:45)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.Activity.performCreate(Activity.java:5104)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-04 15:01:41.196: E/AndroidRuntime(849): ... 11 more
04-04 15:01:41.196: E/AndroidRuntime(849): Caused by: java.lang.reflect.InvocationTargetException
04-04 15:01:41.196: E/AndroidRuntime(849): at java.lang.reflect.Constructor.constructNative(Native Method)
04-04 15:01:41.196: E/AndroidRuntime(849): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
04-04 15:01:41.196: E/AndroidRuntime(849): ... 33 more
04-04 15:01:41.196: E/AndroidRuntime(849): Caused by: android.content.res.Resources$NotFoundException: File res/color/view_selector_background.xml from drawable resource ID #0x7f0a0000
04-04 15:01:41.196: E/AndroidRuntime(849): at android.content.res.Resources.loadDrawable(Resources.java:1953)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.widget.AbsListView.<init>(AbsListView.java:772)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.widget.ListView.<init>(ListView.java:143)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.widget.ListView.<init>(ListView.java:139)
04-04 15:01:41.196: E/AndroidRuntime(849): ... 36 more
04-04 15:01:41.196: E/AndroidRuntime(849): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
04-04 15:01:41.196: E/AndroidRuntime(849): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
04-04 15:01:41.196: E/AndroidRuntime(849): at android.content.res.Resources.loadDrawable(Resources.java:1950)
04-04 15:01:41.196: E/AndroidRuntime(849): ... 40 more
它也会导致Eclipse抱怨:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:208)
at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:782)
at android.view.View.<init>(View.java:3330)
at android.view.View.<init>(View.java:3259)
at android.view.ViewGroup.<init>(ViewGroup.java:425)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:210)
at sun.reflect.GeneratedConstructorAccessor64.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.BridgeInflater.onCreateView(BridgeInflater.java:86)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:131)
at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:385)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:332)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:440)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderPreview.renderSync(RenderPreview.java:560)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderPreview.access$5(RenderPreview.java:506)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderPreview$RenderJob.runInUIThread(RenderPreview.java:1230)
at org.eclipse.ui.progress.UIJob$1.run(UIJob.java:95)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3529)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3182)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
当将选择器应用于自定义视图的根布局(特别是作为背景)时,我从Eclipse得到了同样的抱怨,因此逻辑指示我会得到类似的错误。有什么我做错了吗?
编辑:好吧,多亏了vArDo,再也没有崩溃了。但选择器实际上并没有做任何事情。这是列表项布局的xml,对于那些可能会看到如何实际开始工作的人来说:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/NameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/balanceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/NameView"
android:text="Balance"
android:textAppearance="?android:attr/textAppearanceSmall"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/favStar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="@string/accessFav"
android:src="@android:drawable/btn_star_big_on"
android:visibility="gone" />
</RelativeLayout>
答案 0 :(得分:3)
在ListView
中,android:listSelector
需要是 drawable (see documentation),因此XML {{1}中的每个item
标记都是selector
} file必须包含android:drawable
。只需将android:color
更改为android:drawable
(您可以使用相同的值),您当前的错误就应该消失。
替换后的文件应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_blue_dark"
android:state_pressed="true" />
<item android:drawable="@android:color/holo_blue_light"
android:state_checked="true" />
<item android:drawable="@android:color/holo_blue_bright"
android:state_selected="true" />
</selector>
答案 1 :(得分:1)
好吧,显然我应该更频繁地挖掘源代码。我最终做的是采用我的列表项的根布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
并添加了一个属性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/itemLayout"
android:background="?android:attr/activatedBackgroundIndicator"
android:layout_width="match_parent"
android:layout_height="match_parent" >
这增加了android.R.layout.simple_list_item_activated_1
等人
答案 2 :(得分:0)
另一种替代但更长的方法是在getView()
方法中手动设置listView项目的背景颜色。
class CustomAdapter extends BaseAdapter{
...
int selectedID;
public void setSelectedId(int n)
{
selectedID = n;
}
public View getView(int position, View convertView, ViewGroup parent) {
....
if(position == selectedID)
convertView.setBackgroundColor(someColor);
return convertView;
}
在listView的OnItemClickListener
中,添加:
customAdapter.setSelectedId(position)
尽管如此,方法效率不高,但它在我的项目中完美运行。 此外,请避免使用静态ViewHolder类或使用此方法回收视图。