2014-06-26 68 views
0

我試圖在一個地方設置兩個ListViewes,一次只能看到其中一個。 (這是以編程方式設置的,使用setVisibility(View.GONE)) 第二個ListView可見並消失後,第一個不滾動。我做了一些測試,並發現,總是第二個ListView的onScrollListener捕獲事件。 你有什麼想法我怎麼設置它,總是可見的ListView的onScrollListener捕獲事件? 我已經嘗試過requestFocus(),但它沒有奏效。切換ListViewes可見性導致滾動問題

這裏從代碼中的一些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> 

回答

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>