【问题标题】:Switching ListViewes visibility causes scrolling issues切换 ListViewes 可见性会导致滚动问题
【发布时间】:2014-08-17 14:39:56
【问题描述】:

我尝试在一个地方设置两个 ListView,一次只能看到其中一个。 (这是以编程方式设置的,使用 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>

【问题讨论】:

    标签: android listview scroll visibility


    【解决方案1】:

    我设法解决了这个问题。我不知道为什么能见度改变会出现奇怪的行为,所以毕竟我做了一个工作。 在代码中,以前可见性发生了变化,现在我从父级中删除不应该在屏幕上的布局,并添加应该可见的布局。现在两个 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>
    

    【讨论】:

      猜你喜欢
      • 2015-09-13
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 2015-04-20
      • 2023-03-11
      • 2012-09-06
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多