开发者

Viewflipper listener for when next view is actually displayed

开发者 https://www.devze.com 2023-03-02 18:19 出处:网络
I have 2 ExpandableListViews and I\'m using a ViewFlipper to switch between them using a fling action. My flipper layout looks something like this

I have 2 ExpandableListViews and I'm using a ViewFlipper to switch between them using a fling action. My flipper layout looks something like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ViewFlipper 
        android:id="@+id/flipper" 
        android:layout_width="fill_parent"      
        android:layout_height="fill_parent">
        <LinearLayout 
            android:id="@+id/view1"
            android:orientation="vertical">
            <ExpandableListView 
                android:id="@id/android:list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:drawSelectorOnTop="false"/>
            <ImageView
                android:id="@id/android:empty"
                android:src="@drawable/logo">
           </ImageView>
        </LinearLayout>
        <LinearLayout 
            android:id="@+id/view2"
            android:orientation="vertical">
            <ExpandableListView 
                android:id="@id/android:list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:drawSelectorOnTop="false"/>
            <ImageView
                android:id="@id/android:empty"
                android:src="@drawable/logo">
           </ImageView>
        </LinearLayout>
    </ViewFlipper>
</LinearLayout>

I want to automatically expand the first group in the开发者_如何学编程 list whenever the view is switched to so I switch to the next view and then I expand the first group if the view has any groups present. Something like this

public void switchNextView(){
    flipper.showNext();
    expandFirstGroup();       
}

public void expandFirstGroup(){
    LinearLayout layout = (LinearLayout)flipper.getCurrentView();
    ExpandableListView view = (ExpandableListView)layout.getChildAt(0);

    if(view.getChildCount() > 0){
        view.expandGroup(0);
    }
}

Now the problem is that getChildCount() always returns 0 the first time I'm switching to the next view which tells me that the the groups have still not been added to the ExpandableListView at this point

In order to decide when the view was actually 'ready' I created an AnimationListener and called expandFirstGroup() in the onAnimationEnd()

This seemed to be a workaround though since I might not always have/need an animation so my question is are there any better ways of doing this i.e. finding when the view is displayed or ready to be displayed?

0

精彩评论

暂无评论...
验证码 换一张
取 消