ScrollView嵌套ListView,GridView,ViewPager,以及这些控件自动滚动到底部问题的解决

Google是不推荐在ScrollView 中放入一个可滚动的菜单的,比如放置一个ListView、GridView、ViewPager这些控件的,尽量不要让两者嵌套,但有时候还是有这个需求,先不管它合不合理。如果直接在ScrollView中嵌套只会出现一行,然后在其中滚动,这样不是很好,下面是我的整理,希望对大家有帮助,我也是从网上摘抄的,当然加入了一些我自己的东西。

一、在ScrollView中嵌套ListView,有两张方法

第一种是自定义View,继承ListView代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.widget.ListView;  
  4.   
  5. public class MyListView extends ListView {  
  6.   
  7.     public MyListView(Context context) {  
  8.         super(context);  
  9.     }  
  10.     public MyListView(Context context, AttributeSet attrs, int defStyle) {  
  11.         super(context, attrs, defStyle);  
  12.     }  
  13.     public MyListView(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.     }  
  16.     @Override  
  17.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  18.          int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,    
  19.                  MeasureSpec.AT_MOST);      
  20.         super.onMeasure(widthMeasureSpec, expandSpec);  
  21.     }  
  22.   
  23. }  
第二种

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import android.view.View;  
  2. import android.view.ViewGroup;  
  3. import android.widget.ListAdapter;  
  4. import android.widget.ListView;  
  5.   
  6. public class Utility {  
  7.     public static void setListViewHeightBasedOnChildren(ListView listView) {  
  8.         ListAdapter listAdapter = listView.getAdapter();  
  9.         if (listAdapter == null) {  
  10.             // pre-condition  
  11.             return;  
  12.         }  
  13.   
  14.         int totalHeight = 0;  
  15.         for (int i = 0; i < listAdapter.getCount(); i++) {  
  16.             View listItem = listAdapter.getView(i, null, listView);  
  17.             listItem.measure(00);  
  18.             totalHeight += listItem.getMeasuredHeight();  
  19.         }  
  20.   
  21.         ViewGroup.LayoutParams params = listView.getLayoutParams();  
  22.         params.height = totalHeight  
  23.                 + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
  24.         listView.setLayoutParams(params);  
  25.     }  
  26. }  

第二种在setAdapter之后调用Utility.setListViewHeightBasedOnChildren(listview)这个方法,有些资料说只能item的根布局要LinearLayout,但是我的跟布局为RelativeLayout也可以的。

这两种都可以,但是在有些情况下中能用第一种,比如你显示ListView是一个ListFragment就只能用第二种方法了。

二、在ScrollView中嵌套GridView

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.widget.GridView;  
  4.   
  5. public class NoScrollGridView extends GridView {    
  6.         
  7.     public NoScrollGridView(Context context) {    
  8.         super(context);    
  9.             
  10.     }    
  11.     
  12.     public NoScrollGridView(Context context, AttributeSet attrs) {    
  13.         super(context, attrs);    
  14.     }    
  15.         
  16.     @Override    
  17.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    
  18.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);    
  19.         super.onMeasure(widthMeasureSpec, expandSpec);    
  20.     }    
  21.     
  22. }    
二、在ScrollView中嵌套 ViewPager,也有两张方法
第一种自定义ScrollView

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import android.content.Context;  
  2. import android.util.AttributeSet;  
  3. import android.view.MotionEvent;  
  4. import android.widget.ScrollView;  
  5.   
  6. /** 
  7.  * 能够兼容ViewPager的ScrollView 
  8.  *  
  9.  * @Description: 解决了ViewPager在ScrollView中的滑动反弹问题 
  10.  */  
  11. public class ScrollViewExtend extends ScrollView {  
  12.     // 滑动距离及坐标  
  13.     private float xDistance, yDistance, xLast, yLast;  
  14.   
  15.     public ScrollViewExtend(Context context, AttributeSet attrs) {  
  16.         super(context, attrs);  
  17.     }  
  18.   
  19.     @Override  
  20.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  21.         switch (ev.getAction()) {  
  22.         case MotionEvent.ACTION_DOWN:  
  23.             xDistance = yDistance = 0f;  
  24.             xLast = ev.getX();  
  25.             yLast = ev.getY();  
  26.             break;  
  27.         case MotionEvent.ACTION_MOVE:  
  28.             final float curX = ev.getX();  
  29.             final float curY = ev.getY();  
  30.   
  31.             xDistance += Math.abs(curX - xLast);  
  32.             yDistance += Math.abs(curY - yLast);  
  33.             xLast = curX;  
  34.             yLast = curY;  
  35.   
  36.             if (xDistance > yDistance) {  
  37.                 return false;  
  38.             }  
  39.         }  
  40.         return super.onInterceptTouchEvent(ev);  
  41.     }  
  42. }  
第二种是自定义ViewPager

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import android.content.Context;  
  2. import android.graphics.PointF;  
  3. import android.support.v4.view.ViewPager;  
  4. import android.util.AttributeSet;  
  5. import android.view.MotionEvent;  
  6.   
  7. /** 
  8.  * 嵌套在ScrollView中的ViewPager,解决冲突 
  9.  * @author m3 
  10.  * 
  11.  */  
  12. public class ChildViewPager extends ViewPager {  
  13.     /** 触摸时按下的点 **/  
  14.     PointF downP = new PointF();  
  15.     /** 触摸时当前的点 **/  
  16.     PointF curP = new PointF();  
  17.     OnSingleTouchListener onSingleTouchListener;  
  18.   
  19.     public ChildViewPager(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.         // TODO Auto-generated constructor stub  
  22.     }  
  23.   
  24.     public ChildViewPager(Context context) {  
  25.         super(context);  
  26.         // TODO Auto-generated constructor stub  
  27.     }  
  28.   
  29.     @Override  
  30.     public boolean onInterceptTouchEvent(MotionEvent arg0) {  
  31.         // TODO Auto-generated method stub  
  32.         // 当拦截触摸事件到达此位置的时候,返回true,  
  33.         // 说明将onTouch拦截在此控件,进而执行此控件的onTouchEvent  
  34.         return true;  
  35.     }  
  36.   
  37.     @Override  
  38.     public boolean onTouchEvent(MotionEvent arg0) {  
  39.         // TODO Auto-generated method stub  
  40.         // 每次进行onTouch事件都记录当前的按下的坐标  
  41.         curP.x = arg0.getX();  
  42.         curP.y = arg0.getY();  
  43.   
  44.         if (arg0.getAction() == MotionEvent.ACTION_DOWN) {  
  45.             // 记录按下时候的坐标  
  46.             // 切记不可用 downP = curP ,这样在改变curP的时候,downP也会改变  
  47.             downP.x = arg0.getX();  
  48.             downP.y = arg0.getY();  
  49.             // 此句代码是为了通知他的父ViewPager现在进行的是本控件的操作,不要对我的操作进行干扰  
  50.             getParent().requestDisallowInterceptTouchEvent(true);  
  51.         }  
  52.   
  53.         if (arg0.getAction() == MotionEvent.ACTION_MOVE) {  
  54.             // 此句代码是为了通知他的父ViewPager现在进行的是本控件的操作,不要对我的操作进行干扰  
  55.             getParent().requestDisallowInterceptTouchEvent(true);  
  56.         }  
  57.   
  58.         if (arg0.getAction() == MotionEvent.ACTION_UP) {  
  59.             // 在up时判断是否按下和松手的坐标为一个点  
  60.             // 如果是一个点,将执行点击事件,这是我自己写的点击事件,而不是onclick  
  61.             if (downP.x == curP.x && downP.y == curP.y) {  
  62.                 onSingleTouch();  
  63.                 return true;  
  64.             }  
  65.         }  
  66.   
  67.         return super.onTouchEvent(arg0);  
  68.     }  
  69.   
  70.     /** 
  71.      * 单击 
  72.      */  
  73.     public void onSingleTouch() {  
  74.         if (onSingleTouchListener != null) {  
  75.   
  76.             onSingleTouchListener.onSingleTouch();  
  77.         }  
  78.     }  
  79.   
  80.     /** 
  81.      * 创建点击事件接口 
  82.      *  
  83.      * @author wanpg 
  84.      *  
  85.      */  
  86.     public interface OnSingleTouchListener {  
  87.         public void onSingleTouch();  
  88.     }  
  89.   
  90.     public void setOnSingleTouchListener(  
  91.             OnSingleTouchListener onSingleTouchListener) {  
  92.         this.onSingleTouchListener = onSingleTouchListener;  
  93.     }  
  94.   
  95. }  
第二个问题在ScrollView嵌套ListView,GridView,如果这些子控件很长超出了屏幕的高度,那么ScrollView会自动滚到底部,但是我们需要默认在顶部,我们要在ListView和GridView上面的view中加入,一下代码即可解决:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. view.setFocusable(true);  
  2. view.setFocusableInTouchMode(true);  
  3. view.requestFocus();  
这段代码在初始化的时候就让该界面的顶部的某一个控件获得焦点,滚动条自然就显示到顶部了,顺便附一下我找到答案的地方http://blog.csdn.net/studyalllife/article/details/42970975这上面写的很混乱,大家可以参考一下,大功告成。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值