扩展ViewFlow避免和ViewPager滑动冲突,同时支持无限循环,并完美和CircleFlowIndicator结合

首先,为了避免滑动冲突,我们要继承ViewFlow,重写onInterceptTouchEvent

 public class MyViewFlow extends ViewFlow {
private ViewPager mPager; public MyViewFlow(Context context, AttributeSet attrs) {
super(context, attrs);
} public void setViewPager(ViewPager viewPager) {
mPager = viewPager;
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mPager != null)
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mPager.requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
mPager.requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_CANCEL:
mPager.requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_MOVE:
mPager.requestDisallowInterceptTouchEvent(true);
break;
}
return super.onInterceptTouchEvent(ev);
}
}

调用setViewPager指定viewPager后,滑动便不再冲突

接下来,我们实现无限循环滚动

 public class AdapterBanner extends BaseAdapter {

     private LayoutInflater mInflater;
private static final int[] ids = { R.drawable.banner, R.drawable.banner, R.drawable.banner, R.drawable.banner}; public AdapterBanner(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} @Override
public int getCount() {
//return ids.length;
return Integer.MAX_VALUE;//返回很大的值使得getView中的position不断增大来实现循环
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_fr_acmain_nearby_banner, null);
}
convertView.findViewById(R.id.imgView).setBackgroundResource(ids[position%ids.length]);
return convertView;
} }

关键代码在第13行和31行的红色部分。不过这样实现之后,和CircleFlowIndicator结合使用,会发现程序ANR(无响应),原因是CircleFlowIndicator会调用ViewFlow.getViewCount()来绘制圆点,显然这个数是Integer.MAX_VALUE,圆点数太多了导致ANR,我们需要继续扩展MyViewFlow的getViewCount()。如下。

     private int mCount;
public void setCount(int count){
mCount=count;
}
@Override
public int getViewsCount() {
return mCount;
}

需要调用MyViewFlow.setCount(int count)指定真实的数目,这样做了之后,发现程序不会ANR了。CircleFlowIndicator绘制的圆点数目也正常。但是会发现,无论怎么滑动,圆点状态都不变化,分析ViewFlow的源码,发现要重写一下onScrollChanged,我们继续扩展MyViewFlow,代码如下。

     private int mLastIndex;
@Override
public void setAdapter(Adapter adapter, int initialPosition){
super.setAdapter(adapter,initialPosition);
mLastIndex = initialPosition;
} @Override
protected void onScrollChanged(int h, int v, int oldh, int oldv) {
//super.onScrollChanged(h, v, oldh, oldv);
if (mIndicator != null) {
/*
* The actual horizontal scroll origin does typically not match the
* perceived one. Therefore, we need to calculate the perceived
* horizontal scroll origin here, since we use a view buffer.
*/
int hPerceived = h + (mCurrentAdapterIndex%mCount - mCurrentBufferIndex)
* getChildWidth(); if(mLastIndex%mCount==mCount-1 && mCurrentAdapterIndex>mLastIndex) {
oldh=0;
hPerceived = 0;
}
if(mLastIndex%mCount==0&&mCurrentAdapterIndex<mLastIndex)
hPerceived=h+(mCount-1-mCurrentBufferIndex)*getChildWidth(); LogUtil.e(Config.MYTAG,"mCurrentAdapterIndex="+mCurrentAdapterIndex);
LogUtil.e(Config.MYTAG,"mLastIndex="+mLastIndex);
LogUtil.e(Config.MYTAG, "hPerceived=" + hPerceived); mIndicator.onScrolled(hPerceived, v, oldh, oldv); mLastIndex=mCurrentAdapterIndex;
}

当然,有几个变量mIndicator,mCurrentAdapterIndex,mCurrentBufferIndex是沿用父类ViewFlow的,如果找不到,需要将父类ViewFlow中这些变量改为public即可(虽然从oop角度,这种做法很脏,但很省事,大半夜码文字很累,我很懒)。

至此,大功告成!

整体MyViewFlow代码如下:

package common.control.viewflow;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Adapter; import com.xxx.android.main.Config; import common.util.LogUtil; public class MyViewFlow extends ViewFlow {
private ViewPager mPager; public MyViewFlow(Context context, AttributeSet attrs) {
super(context, attrs);
} public void setViewPager(ViewPager viewPager) {
mPager = viewPager;
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mPager != null)
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mPager.requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
mPager.requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_CANCEL:
mPager.requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_MOVE:
mPager.requestDisallowInterceptTouchEvent(true);
break;
}
return super.onInterceptTouchEvent(ev);
}
private int mCount;
public void setCount(int count){
mCount=count;
}
@Override
public int getViewsCount() {
return mCount;
}
private int mLastIndex;
@Override
public void setAdapter(Adapter adapter, int initialPosition){
super.setAdapter(adapter,initialPosition);
mLastIndex = initialPosition;
} @Override
protected void onScrollChanged(int h, int v, int oldh, int oldv) {
//super.onScrollChanged(h, v, oldh, oldv);
if (mIndicator != null) {
/*
* The actual horizontal scroll origin does typically not match the
* perceived one. Therefore, we need to calculate the perceived
* horizontal scroll origin here, since we use a view buffer.
*/
int hPerceived = h + (mCurrentAdapterIndex%mCount - mCurrentBufferIndex)
* getChildWidth(); if(mLastIndex%mCount==mCount-1 && mCurrentAdapterIndex>mLastIndex) {
oldh=0;
hPerceived = 0;
}
if(mLastIndex%mCount==0&&mCurrentAdapterIndex<mLastIndex)
hPerceived=h+(mCount-1-mCurrentBufferIndex)*getChildWidth(); LogUtil.e(Config.MYTAG,"mCurrentAdapterIndex="+mCurrentAdapterIndex);
LogUtil.e(Config.MYTAG,"mLastIndex="+mLastIndex);
LogUtil.e(Config.MYTAG, "hPerceived=" + hPerceived); mIndicator.onScrolled(hPerceived, v, oldh, oldv); mLastIndex=mCurrentAdapterIndex;
}
}
}

自动播放就不用讲了,网上很多。同学们如果有收获,记得回赞。

上一篇:JavaSE中线程与并行API框架学习笔记1——线程是什么?


下一篇:PS鼠绘盛开的枝头梅花效果