How to change ExpandableListView group indicator position?

前端 未结 3 1737
执念已碎
执念已碎 2021-02-04 14:28

I want to change the ExpandableListView group indicator to right with padding.

I used custom adapter to load data to ExpandableListV

相关标签:
3条回答
  • 2021-02-04 14:39
    private void setGroupIndicatorToRight() {
        /* Get the screen width */
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int width = dm.widthPixels;
            expandableList.setIndicatorBounds(width - getDipsFromPixel(35), width - getDipsFromPixel(5));
        }
        // 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 + 250.5f);
        }
    
    0 讨论(0)
  • 2021-02-04 14:54

    If you want to move indicator to right Just create a dimen in res folder. It will be like that:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <dimen name="my_value">350dp</dimen>
    </resources>
    

    Then in the <ExpandableListView/> add like that:

    <ExpandableListView
            ...
            android:indicatorLeft="@dimen/my_value"
            .../>
    

    You can change the dp according to device settings

    0 讨论(0)
  • 2021-02-04 14:55

    setIndicatorBounds(int, int) does not work properly for Android 4.3. They introduced a new method setIndicatorBoundsRelative(int, int) which works ok for 4.3.

    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);
    }
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels; 
        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            explvList.setIndicatorBounds(width-GetPixelFromDips(35), width-GetPixelFromDips(5));
        } else { 
            explvList.setIndicatorBoundsRelative(width-GetPixelFromDips(35), width-GetPixelFromDips(5));
        }
    }
    
    0 讨论(0)
提交回复
热议问题