2015-06-19 56 views
0

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

代码从主要活动:从组布局

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

代码:

从activity_main
<?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> 

代码:

<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

在我看来,你正在获取屏幕的宽度并使用它来设置指标的界限。但是,你的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,要等到它的测量,然后设置指标。

+0

我刚刚更改为int width = mExpandableListView.getWidth();并修复了问题 – Vasilisfoo

+0

@Vasilisfoo干得不错。我强烈建议你不要使用像'onWindowFocusChanged'这样的方法:) – Simas

+0

我确实改变了onWindowFocusChanged,我用你的建议! – Vasilisfoo