很多人在开发过程中都会设计到首页里面的排版功能,整个页面是一个可滑动的scrollView,上面是一个viewpager轮播图,然后下面又有带滑动的ListView,我们一般会先禁用ListView的滑动功能,让整个ScrollView在首页上下滑动就可以了。
禁用listView的滑动其实就是自定义一个View重写里面的onMeasure方法就行了,然后对于轮播图和ScrollView的滑动冲突我也是通过自定义的两个InsideViewPager和PagerScrollView来处理,后面会贴出3种自定义View的代码。最后就是一个界面体验的小问题,所有的可滑动的View控件,在滑动到屏幕边缘的时候都会有一个并不是很美观的阴影效果,这里我们可以用相同的方式来解决:
xml文件里面:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="none"
android:overScrollMode="never"
android:fillViewport="true">
代码里面:
scrollView = getActivity().findViewById(R.id.scrollView);
scrollView.setHorizontalFadingEdgeEnabled(false);
下面贴出解决滑动冲突的代码:
自定义ScrollView:
package cn.wifi.youngrefersto.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class PagerScrollView extends ScrollView{
private GestureDetector mGestureDetector;
public PagerScrollView(Context context) {
super(context);
init();
}
public PagerScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public PagerScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mGestureDetector = new GestureDetector(getContext(),
new YScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev)
&& mGestureDetector.onTouchEvent(ev);
}
private class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
if (Math.abs(distanceY) >= Math.abs(distanceX)) {
return true;
}
return false;
}
}
}
自定义ViewPager:
package cn.wifi.youngrefersto.view;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class InsideViewPager extends ViewPager {
float curX = 0f;
float downX = 0f;
OnSingleTouchListener onSingleTouchListener;
public InsideViewPager(Context context) {
super(context);
}
public InsideViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
curX = ev.getX();
// TODO Auto-generated method stub
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
downX = curX;
}
int curIndex = getCurrentItem();
if (curIndex == 0) {
if (downX <= curX) {
getParent().requestDisallowInterceptTouchEvent(false);
} else {
getParent().requestDisallowInterceptTouchEvent(true);
}
} else if (curIndex == getAdapter().getCount() - 1) {
if (downX >= curX) {
getParent().requestDisallowInterceptTouchEvent(false);
} else {
getParent().requestDisallowInterceptTouchEvent(true);
}
} else {
getParent().requestDisallowInterceptTouchEvent(true);
}
return super.onTouchEvent(ev);
}
public void onSingleTouch() {
if (onSingleTouchListener != null) {
onSingleTouchListener.onSingleTouch();
}
}
public interface OnSingleTouchListener {
public void onSingleTouch();
}
public void setOnSingleTouchListner(
OnSingleTouchListener onSingleTouchListener) {
this.onSingleTouchListener = onSingleTouchListener;
}
}自定义ListView:
package cn.wifi.youngrefersto.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class NoScrollListView extends ListView{
public NoScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 设置不滚动
*/
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}