您的位置:首页 > 其它

ScrollView 内部嵌套 ListView +复杂布局 设置页面全屏滑动

2015-03-26 19:44 525 查看
MainActivity

mScrollView = (MyScrollView) view.findViewById(R.id.index_scroll);
//将位置移动到顶端
mScrollView.smoothScrollTo(0, 0);
mScrollView.setBottomListener(this);

mListView = (MyListView) view.findViewById(R.id.listview);

owncircle.setOnClickListener(this);

自定义ListView ,   

package cn.weimx.beauty.view;

import android.content.Context;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import android.widget.ListView;

public class MyListView extends ListView{

/**
* pull state,pull up or pull down;PULL_UP_STATE or PULL_DOWN_STATE
*/
int mLastMotionY ;

boolean bottomFlag;

public MyListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
//阻止父类拦截事件
if(bottomFlag){
getParent().requestDisallowInterceptTouchEvent(true);
}

return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
int y = (int) ev.getRawY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// 首先拦截down事件,记录y坐标
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
// deltaY > 0 是向下运动,< 0是向上运动
int deltaY = y - mLastMotionY;

if(deltaY>0){
View child = getChildAt(0);
if(child!=null){

if (getFirstVisiblePosition() == 0
&& child.getTop() == 0) {
bottomFlag = false;
getParent().requestDisallowInterceptTouchEvent(false); 
}

int top = child.getTop();
int padding = getPaddingTop();
if (getFirstVisiblePosition() == 0
&& Math.abs(top - padding) <= 8) {//这里之前用3可以判断,但现在不行,还没找到原因
bottomFlag = false;
getParent().requestDisallowInterceptTouchEvent(false); 

}
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}

return super.onTouchEvent(ev);
}

public void setBottomFlag(boolean flag){
bottomFlag = flag;
}

}

自定义scrollView 

package cn.weimx.beauty.view;

import android.content.Context;

import android.util.AttributeSet;

import android.widget.ScrollView;

public class MyScrollView extends ScrollView{

// private ScrollViewListener scrollViewListener = null; 

public interface OnGetBottomListener {

        public void onBottom();

    }

private OnGetBottomListener onGetBottomListener;

public MyScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}

//  public void setScrollViewListener(ScrollViewListener scrollViewListener) {  

//        this.scrollViewListener = scrollViewListener;  

//    }  
 
   @Override  
   protected void onScrollChanged(int x, int y, int oldx, int oldy) {  
   
4000
super.onScrollChanged(x, y, oldx, oldy);  
   

//        if(scrollViewListener != null) {  

//            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);  

//        }  
       //到底部,给listview放松消息,让其获取触摸事件
       if( getChildCount()>=1&&getHeight() + getScrollY() == getChildAt(getChildCount()-1).getBottom()){
 onGetBottomListener.onBottom();
}
   }  
   
   public interface ScrollViewListener {    
       void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy);   
   }  

 
   public void setBottomListener(OnGetBottomListener listener){
    onGetBottomListener = listener;
   }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  scrollView
相关文章推荐