切换ListViewes可见性会导致滚动问题

时间:2014-06-26 16:26:23

标签: android listview scroll visibility

我试图在一个地方设置两个ListViewes,一次只能看到其中一个。 (这是以编程方式设置的,具有setVisibility(View.GONE)) 第二个ListView可见后消失,第一个不滚动。我做了一些测试,并且发现,第二个ListView的onScrollListener总是捕获事件。 您是否知道如何设置它,总是可见的ListView的onScrollListener捕获事件? 我已经尝试过requestFocus(),但它没有用。

这里有一些来自代码的sinpets:

public void toggle_services(View v) {
    if (home_services_detail.isShown()) {
        AnimationHelper.slide_up(this, home_services_detail);
        home_services_detail.setVisibility(View.GONE);
    } else {
        home_services_detail.setVisibility(View.VISIBLE);
        AnimationHelper.slide_down(this, home_services_detail);
        home_services_detail.requestFocus();

    }
    if (mobile_services_detail.isShown()) {
        AnimationHelper.slide_up(this, mobile_services_detail);
        mobile_services_detail.setVisibility(View.GONE);
    } else {
        mobile_services_detail.setVisibility(View.VISIBLE);
        AnimationHelper.slide_down(this, mobile_services_detail);
        home_services_detail.requestFocus();
    }
}

布局:

 <LinearLayout
                android:orientation="vertical"
                android:layout_width="775dp"
                android:layout_height="match_parent">
            <!-- activity_info layout file -->
            <!-- Clickable title -->
            <TextView
                    android:id="@+id/home_services_title"

                    android:text="@string/home_services"
                    android:clickable="true"
                    android:onClick="toggle_services"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center|center_vertical|center_horizontal"
                    style="@style/THeader"/>
            <!--content to hide/show -->
            <LinearLayout
                    android:id="@+id/home_services_detail"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:layout_weight="1">

                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:weightSum="1">

                    <TextView
                            android:id="@+id/fees_header"
                            android:layout_width="0dp"
                            style="@style/fees_addons"
                            android:layout_height="match_parent"
                            android:text="@string/fees"
                            android:layout_weight="0.91"
                            android:gravity="center|center_vertical|center_horizontal"/>

                    <TextView
                            android:id="@+id/addons_header"
                            android:layout_width="360dp"
                            style="@style/fees_addons"
                            android:layout_height="match_parent"
                            android:gravity="center|center_vertical|center_horizontal"
                            android:text="@string/addons"/>
                </LinearLayout>

              <ListView
                      android:id="@+id/homeServicesList"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:listSelector="@android:color/transparent"
                      android:cacheColorHint="#00000000"
                      android:dividerHeight="0dp"
                      android:divider="@null"/>
            </LinearLayout>
            <TextView
                    android:id="@+id/mobile_services_title"

                    android:text="@string/mobile_services"
                    android:clickable="true"
                    android:onClick="toggle_services"
                    android:layout_width="match_parent"
                    android:layout_weight="5.6"
                    android:layout_gravity="bottom"
                    style="@style/THeader"
                    android:gravity="center|center_vertical|center_horizontal"
                    android:layout_height="fill_parent"
                    android:maxHeight="50dp"/>
            <!--content to hide/show -->
            <LinearLayout
                    android:id="@+id/mobile_services_detail"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="1">

                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:weightSum="1">

                    <TextView
                            android:id="@+id/mobile_fees_header"
                            android:layout_width="0dp"
                            style="@style/fees_addons"
                            android:layout_height="match_parent"
                            android:text="@string/fees"
                            android:layout_weight="0.91"
                            android:gravity="center|center_vertical|center_horizontal"/>

                    <TextView
                            android:id="@+id/mobile_addons_header"
                            android:layout_width="360dp"
                            style="@style/fees_addons"
                            android:layout_height="match_parent"
                            android:gravity="center|center_vertical|center_horizontal"
                            android:text="@string/addons"/>
                </LinearLayout>

                <ListView
                        android:id="@+id/mobileServicesList"
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:listSelector="@android:color/transparent"
                        android:cacheColorHint="#00000000"
                        android:dividerHeight="0dp"
                        android:divider="@null"/>
            </LinearLayout>

        </LinearLayout>

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。我不知道为什么可见性改变会有奇怪的行为,所以毕竟我做了一个解决方案。 在代码中,之前有可见性改变,现在我从不应该在屏幕上的父级中删除布局并添加应该可见的布局。现在两个ListViews都完美运行。我认为它比可见性设置慢一点,但它正在运行。 有必要始终在屏幕上显示两个标题(mobile_services_title和home_services_title),这就是以编程方式设置布局参数的原因。

切换功能:

 public void toggle_services(View v){
        LinearLayout.LayoutParams bigWieght=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 11f);
        LinearLayout.LayoutParams smallWieght=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
        if(isHomeVisible){
            AnimationHelper.slide_up(this, home_services_detail);
            homeServicesLinearLayout.removeView(home_services_detail);
homeServicesLinearLayout.setLayoutParams(bigWieght);
            mobileServicesLinearLayout.addView(mobile_services_detail);
            AnimationHelper.slide_down(this, mobile_services_detail);
            mobileServicesLinearLayout.setLayoutParams(smallWieght);
        } else {
            AnimationHelper.slide_up(this, mobile_services_detail);
            mobileServicesLinearLayout.removeView(mobile_services_detail);
            mobileServicesLinearLayout.setLayoutParams(bigWieght);
            homeServicesLinearLayout.addView(home_services_detail);
            AnimationHelper.slide_down(this, home_services_detail);
            homeServicesLinearLayout.setLayoutParams(smallWieght);
        }
        isHomeVisible=!isHomeVisible;
    }

布局:

 <LinearLayout
            android:orientation="vertical"
            android:layout_width="775dp"
            android:layout_height="match_parent">
        <!-- activity_info layout file -->
        <!-- Clickable title -->
        <LinearLayout
                android:id="@+id/homServicesLinearLayout"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1">

            <TextView
                    android:id="@+id/home_services_title"

                    android:text="@string/home_services"
                    android:clickable="true"
                    android:onClick="toggle_services"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center|center_vertical|center_horizontal"
                    style="@style/THeader"/>
            <!--content to hide/show -->
            <LinearLayout
                    android:id="@+id/home_services_detail"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:orientation="vertical"
                    android:layout_weight="1">

                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:weightSum="1">

                    <TextView
                            android:id="@+id/fees_header"
                            android:layout_width="0dp"
                            style="@style/fees_addons"
                            android:layout_height="match_parent"
                            android:text="@string/fees"
                            android:layout_weight="0.91"
                            android:gravity="center|center_vertical|center_horizontal"/>

                    <TextView
                            android:id="@+id/addons_header"
                            android:layout_width="360dp"
                            style="@style/fees_addons"
                            android:layout_height="match_parent"
                            android:gravity="center|center_vertical|center_horizontal"
                            android:text="@string/addons"/>
                </LinearLayout>

                <ListView
                        android:id="@+id/homeServicesList"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:listSelector="@android:color/transparent"
                        android:cacheColorHint="#00000000"
                        android:dividerHeight="0dp"
                        android:divider="@null"/>
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
                android:id="@+id/mobileServicesLinearLayout"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="9.5">

            <TextView
                    android:id="@+id/mobile_services_title"

                    android:text="@string/mobile_services"
                    android:clickable="true"
                    android:onClick="toggle_services"
                    android:layout_width="match_parent"
                    style="@style/THeader"
                    android:gravity="center|center_vertical|center_horizontal"
                    android:layout_height="wrap_content"
                    android:maxHeight="50dp"/>
            <!--content to hide/show -->
            <LinearLayout
                    android:id="@+id/mobile_services_detail"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:layout_weight="1">

                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="60dp"
                        android:weightSum="1">

                    <TextView
                            android:id="@+id/mobile_fees_header"
                            android:layout_width="0dp"
                            style="@style/fees_addons"
                            android:layout_height="match_parent"
                            android:text="@string/fees"
                            android:layout_weight="0.91"
                            android:gravity="center|center_vertical|center_horizontal"/>

                    <TextView
                            android:id="@+id/mobile_addons_header"
                            android:layout_width="360dp"
                            style="@style/fees_addons"
                            android:layout_height="match_parent"
                            android:gravity="center|center_vertical|center_horizontal"
                            android:text="@string/addons"/>
                </LinearLayout>

                <ListView
                        android:id="@+id/mobileServicesList"
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:listSelector="@android:color/transparent"
                        android:cacheColorHint="#00000000"
                        android:dividerHeight="0dp"
                        android:divider="@null"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>