如何在Android 2.3.3上的ListView中突出显示所选行。 (HoloeveryWhere + ABS)

时间:2013-07-10 17:05:20

标签: android listview actionbarsherlock highlighting android-holo-everywhere

XML:

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

JAVA:

import org.holoeverywhere.app.ListFragment;
import org.holoeverywhere.widget.ListView;
    ....
    ....
private void listViewInit() {
    getListView().setOnItemClickListener(this);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    getListView().setMultiChoiceModeListener(getMultiChoiceModeListener());
    getListView().setSelector(R.drawable.activated_background); // Error there
    setListAdapter(adapter);
}

Activated_background是

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/row_activated"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

在模拟器(android 2.3.3)中,当ListView附近出现在屏幕上时,我有下一个异常:

 E/AndroidRuntime: FATAL EXCEPTION: main
        android.content.res.Resources$NotFoundException: File res/drawable/activated_background.xml from drawable resource ID #0x7f020131
        at org.holoeverywhere.drawable.DrawableCompat.loadDrawable(DrawableCompat.java:138)
        at org.holoeverywhere.drawable.DrawableCompat.getDrawable(DrawableCompat.java:107)
        at org.holoeverywhere.widget.ListView.setSelector(ListView.java:954)
        at org.kidinov.unixadmin.FileManagerFragment.init(FileManagerFragment.java:211)
        at org.kidinov.unixadmin.FileManagerFragment.go(FileManagerFragment.java:183)
        at org.kidinov.unixadmin.FileManagerFragment.onResume(FileManagerFragment.java:97)
        at android.support.v4.app.Fragment.performResume(Fragment.java:1503)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:948)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1089)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1445)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1c/d=0x0 a=2 r=0x7f090011}
        at org.holoeverywhere.drawable.DrawableCompat.loadDrawable(DrawableCompat.java:128)
        at org.holoeverywhere.drawable.DrawableCompat.getDrawable(DrawableCompat.java:107)
        at org.holoeverywhere.drawable.StateListDrawable.inflate(StateListDrawable.java:194)
        at org.holoeverywhere.drawable.DrawableCompat.createFromXmlInner(DrawableCompat.java:87)
        at org.holoeverywhere.drawable.DrawableCompat.createFromXml(DrawableCompat.java:63)
        at org.holoeverywhere.drawable.DrawableCompat.loadDrawable(DrawableCompat.java:135)

如果我从代码中删除getListView().setSelector(R.drawable.activated_background);,并将其作为我列表中自定义行的背景以XML格式移动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/activated_background">
....
</RelativeLayour>

它适用于4+,但2.3选定的行不会突出显示,没有任何例外。

欢迎任何帮助。

1 个答案:

答案 0 :(得分:1)

我花了超过1天的时间,最后我开始使用这个愚蠢的决定:

@Override
public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        if (b)
            listView.getChildAt(i).setBackgroundColor(activity.getResources().getColor(R.color.row_activated));
        else
            listView.getChildAt(i).setBackgroundColor(activity.getResources().getColor(R.color.abs__background_holo_dark));
    }

也许有人知道如何以更“正常”的方式解决它?