ScrollView和ListView嵌套

来源:互联网 发布:招商银行数据分析 编辑:程序博客网 时间:2024/05/01 03:29
转自:http://blog.csdn.net/fu222cs98/article/details/25250471

前 言

问题出现原因:布局中用到了ListView,但是无奈界面上又不是只有ListView 当内容变多时,需要滚动界面。大致布局如下

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/app_bg"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ScrollView  
  9.         android:id="@+id/scSpace"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content" >  
  12.   
  13.         <LinearLayout  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:orientation="vertical" >  
  17.   
  18.             <ImageView  
  19.                 android:layout_width="match_parent"  
  20.                 android:layout_height="wrap_content"  
  21.                 android:src="@drawable/shadow_title" />  
  22.   
  23.             <ListView  
  24.                 android:id="@+id/listview_personalhealthindex"  
  25.                 android:layout_width="match_parent"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:background="@color/app_bg" >  
  28.             </ListView>  
  29.         </LinearLayout>  
  30.     </ScrollView>  
  31.   
  32. </LinearLayout>  
这样的布局会导致ListView显示不全,问题相当棘手,好在万能的网友给出了答案

滚动的解决办法:在

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. listView.setAdapter(indexAdapter);  
后面加入如下方法:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static void setListViewHeightBasedOnChildren(ListView listView) {  
  2.     ListAdapter listAdapter = listView.getAdapter();   
  3.     if (listAdapter == null) {  
  4.         // pre-condition  
  5.         return;  
  6.     }  
  7.   
  8.     int totalHeight = 0;  
  9.     for (int i = 0; i < listAdapter.getCount(); i++) {  
  10.         View listItem = listAdapter.getView(i, null, listView);  
  11.         listItem.measure(00);  
  12.         totalHeight += listItem.getMeasuredHeight();  
  13.     }  
  14.   
  15.     ViewGroup.LayoutParams params = listView.getLayoutParams();  
  16.     params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
  17.     listView.setLayoutParams(params);  
  18. }  
即可解决,但是这样会造成一个小的问题打开该界面时,会显示到底部而不是从顶部显示

现实顶部的解决办法1:初始化的时候就让该界面的顶部的某一个控件获得焦点,滚动条自然就显示到顶部了,代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. txtBaseMsg.setFocusable(true);  
  2. txtBaseMsg.setFocusableInTouchMode(true);  
  3. txtBaseMsg.requestFocus();  
现实顶部的解决办法2:在加入setListViewHeightBasedOnChildren(listView);的后面我们加入如下代码:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. listview.post(new Runnable() {  
  2.                   
  3.                 @Override  
  4.                 public void run() {  
  5.                     ((ScrollView)findViewById(R.id.scSpace)).scrollTo(00);  
  6.                       
  7.                 }  
  8.             });  
同样也可完美解决,注:scSpace是ScorollView的id




参考链接:http://blog.csdn.net/hitlion2008/article/details/6737459

http://bbs.csdn.net/topics/370109727

0 0
原创粉丝点击