可扩展列表视图的组指示器消失

The Group Indicator of the expandable List view disappears

我想更改可扩展列表视图的组指示器的位置,从左到右,但组指示器消失了。

主要代码 Activity:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        mExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width
                - getDipsFromPixel(5));
    } else {
        mExpandableListView.setIndicatorBoundsRelative(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 + 0.5f);
}

组布局中的代码:

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/tv_menu_background_color"
        android:paddingLeft="32dp"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/list_item_title"
        android:gravity="center_vertical"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
</RelativeLayout>

来自 activity_main 的代码:

    <android.support.v4.widget.DrawerLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity"
                android:id="@+id/drawer_layout">
                <FrameLayout
                    android:id="@+id/frame_container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
                <!-- Expandable list view -->
                <ExpandableListView
                    android:id="@+id/list_slideMenu"
                    android:layout_width="320dp"
                    android:layout_height="match_parent"
                    android:choiceMode="singleChoice"
                    android:divider="@color/list_divider"
                    android:dividerHeight="1dp"
                    android:listSelector="@drawable/list_selector"
                    android:background="@color/list_background"
                    android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>

谢谢!

在我看来,您正在获取屏幕的宽度并使用它来设置指示器的边界。但是,您的 ExpandableListView 的宽度是 320dp。我相信你应该这样计算宽度:

int width = mExpandableListView.getWidth();

此外,所有这些绝对不应该在 onWindowFocusChanged 中调用,因为它可以被调用多次。

相反,您应该将此代码放入 onCreate(在 setContentView 之后):

mExpandableListView = findViewById(R.id.elv);

mExpandableListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom,
                               int oldLeft, int oldTop, int oldRight, int oldBottom) {
        mExpandableListView.removeOnLayoutChangeListener(this);

        int width = mExpandableListView.getWidth();

        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            mExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width
                    - getDipsFromPixel(5));
        } else {
            mExpandableListView.setIndicatorBoundsRelative(width - getDipsFromPixel(35), width
                    - getDipsFromPixel(5));
        }
    }
});

这为 ELV 添加了一个侦听器,等待它被测量,然后设置指示器。