1

XML:如何在Android 2.3.3上突出顯示ListView中的選定行。 (HoloeveryWhere + ABS)

<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> 

在仿真器(安卓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

我花在這個超過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)); 
    } 

也許有人知道如何解決它更「正常」的方式?

相關問題