转载请注明出处 http://blog.csdn.net/u011510784/article/details/49766099
private void initData() { head_tv_content.setText("0"); viewFlow.setmSideBuffer(urlsForNet.length); viewFlow.setFlowIndicator(CFIndic); viewFlow.setTimeSpan(3000); viewFlow.startAutoFlowTimer(); // 启动自动播放 viewFlow.setAdapter(new CirculateAdapter(urlsForNet)); viewFlow.setOnViewSwitchListener(new ViewSwitchListener() { @Override public void onSwitched(View view, int position) { head_tv_content.setText("第" + (position+1)+"张"); } }); }setmSideBuffer是设置轮播图片的数量,在CirculateAdapter类中需要注意的地方是这个adapter类的getCount()方法返回了一个Integer.MAX_VALUE,以便可以保证viewFlow的循环,我们修改后的这个项目中getCount()()方法只需要返回正常position.
View currentView = makeAndAddView(position, true,(recycleViews.isEmpty() ? null : recycleViews.remove(0))); mLoadedViews.addLast(currentView); for (int offset = 1; mSideBuffer - offset >= 0; offset++) { int leftIndex = position - offset; int rightIndex = position + offset; if (leftIndex >= 0) mLoadedViews.addFirst(makeAndAddView(leftIndex,false, (recycleViews.isEmpty() ? null : recycleViews.remove(0)))); if (rightIndex < mAdapter.getCount()) mLoadedViews.addLast(makeAndAddView(rightIndex, true, </span>
<span style="font-family:SimHei;font-size:14px;"> (recycleViews.isEmpty() ? null : recycleViews.remove(0)))); }makeAndAddView()方法的作用是根据传入的position得到adapter中相应的view,即最终会调用 adapter.getView()方法来返回一个view.在for循环之前,先添加当前position对应的图片,因为当前position也可能不为0,然后再通过for循环在依次添加其他图片.额外的一张图片也是在这里添加的,因为mAdapter.getCount()返回到是Integer.MAX_VALUE,这里的mLoadedView集合用的是LinkedList<>,方便快速的插入与删除.到这里图片初始化完成,接下来是滑动,滑动的细节源代码写的很清楚就不在一一贴代码了,下面看他实现循环滚动的方法:
private void postViewSwitched(int direction) { if (direction == 0) return; if (direction > 0) { // to the right mCurrentAdapterIndex++; mCurrentBufferIndex++; View recycleView = null; // Remove view outside buffer range if (mCurrentAdapterIndex > mSideBuffer) { recycleView = mLoadedViews.removeFirst(); detachViewFromParent(recycleView); mCurrentBufferIndex--; } // Add new view to buffer int newBufferIndex = mCurrentAdapterIndex + mSideBuffer; if (newBufferIndex < mAdapter.getCount()) mLoadedViews.addLast(makeAndAddView(newBufferIndex, true, recycleView)); } else { // to the left mCurrentAdapterIndex--; mCurrentBufferIndex--; View recycleView = null; // Remove view outside buffer range if (mAdapter.getCount() - 1 - mCurrentAdapterIndex > mSideBuffer) { recycleView = mLoadedViews.removeLast(); detachViewFromParent(recycleView); } // Add new view to buffer int newBufferIndex = mCurrentAdapterIndex - mSideBuffer; if (newBufferIndex > -1) { mLoadedViews.addFirst(makeAndAddView(newBufferIndex, false, recycleView)); mCurrentBufferIndex++; } } requestLayout(); setVisibleView(mCurrentBufferIndex, true); if (mIndicator != null) { mIndicator.onSwitched(mLoadedViews.get(mCurrentBufferIndex), mCurrentAdapterIndex); } if (mViewSwitchListener != null) { mViewSwitchListener .onSwitched(mLoadedViews.get(mCurrentBufferIndex), mCurrentAdapterIndex); } }
private void postViewSwitched(int direction) { if (direction == 0) return; if (direction > 0) { // to the right if (mCurrentScreen == mSideBuffer + 1) { mCurrentAdapterIndex = -1; mCurrentBufferIndex = 0; mCurrentScreen = 1; } mCurrentAdapterIndex++; mCurrentBufferIndex++; } else { // to the left if (mCurrentScreen == 0) { mCurrentAdapterIndex = mSideBuffer; mCurrentBufferIndex = mSideBuffer + 1; mCurrentScreen = mSideBuffer; } mCurrentAdapterIndex--; mCurrentBufferIndex--; } requestLayout(); setVisibleView(mCurrentBufferIndex, true); if (mIndicator != null) { mIndicator.onSwitched(mLoadedViews.get(mCurrentBufferIndex), mCurrentAdapterIndex); } if (mViewSwitchListener != null) { mViewSwitchListener .onSwitched(mLoadedViews.get(mCurrentBufferIndex), mCurrentAdapterIndex); } }当然里面还有很多的细节处理,具体见源码吧!