可扩展listview组指示器到右边

时间:2014-12-11 03:55:55

标签: android expandablelistview

我正在尝试构建一个可扩展的列表视图,其中组指示符应放在右侧。 我正在使用的代码是

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>

1 个答案:

答案 0 :(得分: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));
        }
 }

希望它有助于ツ