Expandable list view move group icon indicator to right

前端 未结 16 2387
栀梦
栀梦 2020-12-04 08:56

As regards to expandable list view, the group icon indicator is positioned to the left side, can I move the indicator and positioned it to the right? Thanks.

相关标签:
16条回答
  •      DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels; 
    
     mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
     mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));  
    
       public int GetPixelFromDips(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);
    }
    

    this one worked for me

    0 讨论(0)
  • 2020-12-04 09:44

    Try this... this code for adjusting the ExpandableList group indicator into right side of the view.

    private ExpandableListView expandableListView;
    DisplayMetrics metrics;
    int width;
    metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    width = metrics.widthPixels;
    expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
    expandableListView.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
    

    And call this method,

    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);
    }
    

    The result is.. ExpandableList right side group indicator

    0 讨论(0)
  • 2020-12-04 09:45

    First set android:groupIndicator="@null" in you Expandable listview as below:

    then in your header layout as below:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/screen_margin"
        android:paddingRight="@dimen/screen_margin"
        android:paddingTop="@dimen/dot_margin"
        android:paddingBottom="@dimen/dot_margin"
        android:orientation="vertical">
    
    
    
    
        <TextView
            android:id="@+id/tvQuestion"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="what is jodi?"
            android:textColor="@color/textColor"
            android:textSize="@dimen/font_large"
            app:customFont="@string/font_toolbar"
            android:layout_toLeftOf="@+id/imgDropDown"
            android:layout_marginRight="@dimen/margin"
           />
    
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imgDropDown"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/right_arrow"/>
    
    </RelativeLayout>
    

    Lastly add this in you adapter in getGroupView method add below :

    TextView lblListHeader = (TextView) convertView.findViewById(R.id.tvQuestion);
    
    lblListHeader.setText(headerTitle);
    
    ImageView img=convertView.findViewById(R.id.imgDropDown);
    
    if (isExpanded) {
      img.setImageResource(R.drawable.down_arrow);
        lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorPrimary));
    } else {
     img.setImageResource(R.drawable.right_arrow);
        lblListHeader.setTextColor(_context.getResources().getColor(R.color.textColor));
    }
    
    0 讨论(0)
  • 2020-12-04 09:46
    FIX: 
    

    They introduced a new method in the API 18 and onwards, a method called setIndicatorBoundsRelative(int, int). You should check for Android version and use the respective methods with the API's:

    expListView = (ExpandableListView) v.findViewById(R.id.laptop_list);
    
    metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    width = metrics.widthPixels;
    
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        expListView.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
    
    } else {
        expListView.setIndicatorBoundsRelative(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
    
    }
    
    0 讨论(0)
  • 2020-12-04 09:46

    i know it's too late but here is a solution to put the indicator on the right programmatically, simple and easy to use :

    expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            Resources r = getResources();
            int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    50, r.getDisplayMetrics());
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
                expandableListView.setIndicatorBounds(width - px, width);
            } else {
                expandableListView.setIndicatorBoundsRelative(width - px, width);
            }
    

    where expandableListView is your ExpandableListview

    0 讨论(0)
  • 2020-12-04 09:50

    The setIndicatorBounds(int left, int right) is used to set the indicator bounds for the group view of an ExpandableListView.

    explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5));
    

    Here width means device width.

    Convert pixel to dip :

    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);
    } 
    
    0 讨论(0)
提交回复
热议问题