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

时间:2015-06-19 18:37:07

标签: android expandablelistview

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

主要活动代码:

@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>

谢谢!

1 个答案:

答案 0 :(得分:1)

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

int width = mExpandableListView.getWidth();

同样所有这些都不应该在onWindowFocusChanged中调用,因为它可以被多次调用。

相反,您应将此代码放入onCreatesetContentView之后):

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添加一个监听器,等待它被测量,然后设置指示器。