解決ScrollView中有listView,造成不屏幕不停留在頂端的問題

這個問題造成的原因 其實是加載ListView之後  ScrollView 的焦點異常   然後看上去被頂上去的


其實有兩個方法可以解決 這個問題 

一個是

scrollView.smoothScrollTo(0,20);

顧名思義  這個是讓屏幕滾動到頂端的意思。  可有的時候 必須用TabHost  等頁面切換Fragment的時候 

這個方法找不到合適的地方加    寫在生命週期裏  也可能不調用  所有就有了第二種方法

在XML文件裏   讓listView失去焦點   

只需要在父容器中加入這兩條屬性
android:focusable="true"
android:focusableInTouchMode="true"


借鑑EditText的方法就可以解決了

以下是完整的XML代碼 供大家參考 

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:background="@color/white"
   
android:clickable="true"
   
android:orientation="vertical" >

   
<ScrollView
       
android:id="@+id/scrollView"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:scrollbars="none" >

       
<LinearLayout
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:focusable="true"
           
android:focusableInTouchMode="true"
           
android:orientation="vertical" >

           
<cn.dt.app.view.SlideShowView
               
android:id="@+id/slideshowView"
               
android:layout_width="match_parent"
               
android:layout_height="150dp"
               
android:layout_centerHorizontal="true" />

           
<RelativeLayout
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:gravity="center"
               
android:padding="12dp" >

               
<ImageButton
                   
android:id="@+id/link_coupon"
                   
android:layout_width="46dp"
                   
android:layout_height="58dp"
                   
android:layout_centerInParent="true"
                   
android:layout_marginLeft="@dimen/main_link_margin"
                   
android:layout_marginRight="@dimen/main_link_margin"
                   
android:background="@drawable/link_coupon" />

               
<ImageButton
                   
android:id="@+id/link_notake"
                   
android:layout_width="46dp"
                   
android:layout_height="58dp"
                   
android:layout_toLeftOf="@id/link_coupon"
                   
android:background="@drawable/link_notake" />

               
<ImageButton
                   
android:id="@+id/link_machine"
                   
android:layout_width="46dp"
                   
android:layout_height="58dp"
                   
android:layout_centerInParent="true"
                   
android:layout_toRightOf="@id/link_coupon"
                   
android:background="@drawable/link_machine" />
           
</RelativeLayout>

           
<ListView
               
android:id="@+id/tab_listview"
               
android:layout_width="match_parent"
               
android:layout_height="wrap_content"
               
android:divider="@null"
               
android:dividerHeight="0dp"
               
android:listSelector="@color/transparent"
               
android:scrollbars="none" >
           
</ListView>
       
</LinearLayout>
   
</ScrollView>

   
<Button
       
android:id="@+id/city_default"
       
android:layout_width="70dp"
       
android:layout_height="35dp"
       
android:layout_marginLeft="3dp"
       
android:layout_marginTop="10dp"
       
android:background="@drawable/city_uncheck"
       
android:drawableLeft="@drawable/icon_location"
       
android:drawablePadding="7dp"
       
android:gravity="center_vertical"
       
android:paddingLeft="8dp"
       
android:text="北京"
       
android:textColor="@color/white"
       
android:textSize="16dp"
       
android:visibility="gone" />

   
<LinearLayout
       
android:id="@+id/city_layout"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@id/city_default"
       
android:visibility="gone" >

       
<include layout="@layout/city_selector" />
   
</LinearLayout>

   
<include layout="@layout/nodata_layout" />

</LinearLayout>

本人才疏學淺,寫的不好大家見諒。希望大家多提寶貴意見。以後改進。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章