2014-12-11 87 views
0

我正在嘗試構建一個可擴展列表視圖,其中組指示符應放置在右側。 我使用的代碼是Expandable listview Group indicator to right

faq_ExpandableListView = (ExpandableListView)v. findViewById(R.id.lvExp); 
setGroupIndicatorToRight(); 



private void prepareListdata(){ 

    listDataChild = new HashMap<String, List<String>>(); 
    HashMap<String, String> single_item; 

    for (int i = 0; i < listDataHeader.size(); i++) { 

     single_item = arraylist.get(i); 

     List<String> comingSoon = new ArrayList<String>(); 
     comingSoon.add(single_item.get(AppConstants.faq_answers)); 

     listDataChild.put(listDataHeader.get(i), comingSoon); 

    } 

    populateListview(); 

} 

private void populateListview(){ 
    listAdapter = new com.mawaqaa.babtain.adapter.ExpandableListAdapter(getActivity(), listDataHeader, listDataChild); 

    // setting list adapter 
    faq_ExpandableListView.setAdapter(listAdapter); 
} 

    private void setGroupIndicatorToRight() { 
     /* Get the screen width */ 
     DisplayMetrics dm = new DisplayMetrics(); 
     getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); 
     int width = dm.widthPixels; 

     faq_ExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width 
       - getDipsFromPixel(5)); 

    } 

    public int getDipsFromPixel(float pixels) { 
     // Get the screen's density scale 
     final float scale = getResources().getDisplayMetrics().density; 
     // Convert the dps to pixels, based on density scale 
     return (int) (pixels * scale + 0.5f); 
    } 

佈局

<android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     card_view:cardBackgroundColor="#fff" 
     card_view:cardCornerRadius="8dp" 
     android:id="@+id/cardview" 
     android:layout_margin="15dp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

      <ExpandableListView 
      android:id="@+id/lvExp" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="3dp" 
      android:layout_marginRight="3dp" 
      android:groupIndicator="@drawable/settings_selector" 
      android:divider="@android:color/transparent" 
      android:cacheColorHint="#00000000"/> 

</android.support.v7.widget.CardView> 

settings_selector爲分組指示處理是

 <?xml version="1.0" encoding="utf-8"?> 
     <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 

     <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_expanded="true" android:drawable="@drawable/up_arrow" /> 
      <item android:drawable="@drawable/down_arrow" /> 
    </selector> 

    </animation-list> 
+0

嘗試在設置適配器後調用'setGroupIndicatorToRight'。和你的Android版本是什麼? – 2014-12-11 05:01:49

+0

http://stackoverflow.com/a/18013962/1318946 – 2014-12-11 05:02:39

+0

[Expandable list view move group icon indicator to right]的可能重複(http://stackoverflow.com/questions/5800426/expandable-list-view-move- group-icon-indicator-to-right) – 2014-12-11 05:03:03

回答

0

您需要使用setIndicatorBoundsRelative的API 4.3版本

private void setGroupIndicatorToRight() { 
     DisplayMetrics dm = new DisplayMetrics(); 
     getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); 
     int width = dm.widthPixels; 

     if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { 
      list_all_subscriptions.setIndicatorBounds(width - getDipsFromPixel(50), width - getDipsFromPixel(10)); 
     } else { 
      list_all_subscriptions.setIndicatorBoundsRelative(width - getDipsFromPixel(50), width - getDipsFromPixel(10)); 
     } 
} 

希望它有幫助Ð