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;
}
}
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;
}
}
相关文章推荐
- Recycleview实现复杂页面 三种以上布局 瀑布流 多布局 scrollview嵌套recyclerView 显示不全 滑动冲突 之进阶终极篇
- Recycleview实现复杂页面三种以上布局 瀑布流多布 scrollview嵌套recyclerView 显示不全滑动冲突
- ScrollView、ListView、RecycleView嵌套其他的滑动布局引发的自动滑动问题
- 一个 ScrollView 里面包含 viewpager 嵌套 listview 或 RecyclerView 极少代码实现的流畅滑动效果 处理一个两层滑动 view 的自定义布局,以最少的代码实现,
- ScrollView中嵌套ScrollView或ListView而且内部ScrollView或ListView也可滑动
- ScrollView中嵌套ScrollView或ListView并且内部ScrollView或ListView也可滑动
- 新手上路,scrollview 嵌套listview scrollview嵌套scrollview滑动冲突等问题的学习
- 【Android界面实现】解决ScrollView中嵌套Listview,Listview中嵌套Listview显示不完整和滑动冲突的问题
- ListView嵌套ScrollView后,设置dividerHeight属性后高度计算
- android 关于 ScrollView嵌套GridView,ListView 显示和滑动的问题
- 解决:ScrollView中嵌套ListView,使得listview上下滑动卡顿现象
- android 关于 ScrollView嵌套GridView,ListView 显示和滑动的问题
- 【Android界面实现】解决ScrollView中嵌套Listview,Listview中嵌套Listview显示不完整和滑动冲突的问题
- ScrollView中若嵌套有滑动的控件(如:WebView,ListView或GridView)焦点问题
- android 在一个scrollView里面嵌套一个需要滑动的控件(listView、gridView)
- 在ScrollView中嵌套ListView时,需要根据内容来设置listView的高度
- 使ListView和其他的布局一起滑动(给ListView加ScrollView)
- ScrollView中若嵌套有滑动的控件(如:WebView,ListView或GridView)时的焦点问题
- ScrollView中嵌套ListView 滑动冲突
- Android小知识——ScrollView内的内部嵌套LinearLayout布局导致滑动条占位置