码迷,mamicode.com
首页 > 其他好文 > 详细

解决ScrollView中有listView,造成不屏幕不停留在顶端的问题

时间:2015-05-04 14:00:22      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:android   scrollview   fragment   tabhost   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>

本人才疏学浅,写的不好大家见谅。希望大家多提宝贵意见。以后改进。

解决ScrollView中有listView,造成不屏幕不停留在顶端的问题

标签:android   scrollview   fragment   tabhost   listview   

原文地址:http://blog.csdn.net/wujian543/article/details/45478449

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!