1:左右滑动控件类 FlingGallery.java
package com.vaode.common.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;
import com.vaode.common.app.BaseApplication;
/**
* @author 王祝龙
* date 2014/01/25
* function 左右滑动适配器
*/
public class FlingGallery extends RelativeLayout {
//============================== 不可被设置的参数,系统运行私有状态参数 ==============================
// 底图
private FrameLayout bottomFrameLayout;
// 底图视图的索引值
private int bottomFrameIndex = -1;
// 视图播放组件
private ViewFlipper viewFlipper;
// 是否处于播放中
private boolean isPlaying;
// 是否处于动画中
private boolean isAnimating;
// 是否处于可移动中
private boolean movedAble;
// 点下X坐标
private float rawX;
// 点下Y坐标
private float rawY;
// 当前移动的距离
private float currentMovedDistance;
// 当前视图
private View currentView;
// 下一个视图
private View nextView;
// 当前pageNumber
private int currentPageNumber = -1;
//============================== 可以被设置的参数,改变系统运行状态 ==============================
// 内容适配器
private BaseAdapter baseAdapter;
// 预加载视图个数
private int befLoadExtraViewCount = 2;
// 最小滑动距离40dp(将手机屏幕宽度平均分成320份,每一份为1dp)
private float minMoveDistance = BaseApplication.getWidth(40F);
// 视图与视图之间的偏移量比率
private float viewOffset = 0.005F;
// 动画是否左移
private boolean leftMove = true;
// 是否停止滑动
private boolean stopMove;
// 动画时间(以时间戳毫秒为单位)
private long durationMillis = 256L;
// 默认自动切换时间间隔(以时间戳毫秒为单位)
private int milliseconds = 3000;
// 当前视图被改变监听接口
private OnCurrentViewChangedListener onCurrentViewChangedListener;
// 页码被改变监听接口
private OnPageChangedListener onPageChangedListener;
// Gallery被点击事件
private OnGalleryClickListener onGalleryClickListener;
/**
* @param context
*/
public FlingGallery(final Context context) {
super(context);
layout(context);
}
/**
* @param context
* @param attrs
*/
public FlingGallery(final Context context, final AttributeSet attrs) {
super(context, attrs);
layout(context);
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public FlingGallery(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
layout(context);
}
/**
* 初始化布局方法
* @param context
*/
private final void layout(final Context context){
/** 添加底图 */
bottomFrameLayout = new FrameLayout(context);
final RelativeLayout.LayoutParams bottomFrameLayoutParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
bottomFrameLayoutParam.addRule(RelativeLayout.CENTER_IN_PARENT);
addView(bottomFrameLayout, bottomFrameLayoutParam);
/** 添加左右滑动主控件 */
viewFlipper = new ViewFlipper(context){
@Override
public void onDetachedFromWindow() {
try{
super.onDetachedFromWindow();
} catch (Exception e) {
/** 若正在播放中,则停止动画 */
if(isPlaying && isFlipping()){
stopFlipping();
}
/** 若设置自动播放,则播放动画 */
if(isPlaying && !isFlipping()){
setInAnimation(null);
setOutAnimation(null);
startFlipping();
setInAnimation(getInMoveAnimation(0F, true));
setOutAnimation(getOutMoveAnimation(0F, true));
}
}
}
@Override
public void setDisplayedChild(int whichChild) {
currentPageNumber = getRealPageNumber(whichChild);
if(onCurrentViewChangedListener != null){
onCurrentViewChangedListener.onCurrentViewChanged(viewFlipper.getChildAt(currentPageNumber),
currentPageNumber);
}
super.setDisplayedChild(currentPageNumber);
if(onPageChangedListener != null){
onPageChangedListener.onPageChanged(currentPageNumber);
}
}
};
final RelativeLayout.LayoutParams viewFlipperParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
viewFlipperParam.addRule(RelativeLayout.CENTER_IN_PARENT);
addView(viewFlipper, viewFlipperParam);
}
/**
* 获取进入动画
* @param scrollXRelativePersent 有正负之分
* @param showNext 显示下一个
* @return
*/
private final Animation getInMoveAnimation(final float scrollXRelativePersent, final boolean showNext){
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());
TranslateAnimation translateAnimation = null;
if(scrollXRelativePersent == 0F){
translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
(showNext ? leftMove : !leftMove) ? 1F + viewOffset : -1F - viewOffset,
Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F);
} else{
translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, - scrollXRelativePersent,
Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F);
}
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
isAnimating = true;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
/** 删除底部替换视图 */
if(bottomFrameLayout.getChildCount() > 0){
bottomFrameLayout.removeAllViews();
}
/** 若设置自动播放,则播放动画 */
if(isPlaying && !viewFlipper.isFlipping()){
if(scrollXRelativePersent != 0F){
viewFlipper.setInAnimation(null);
viewFlipper.setOutAnimation(null);
}
viewFlipper.startFlipping();
if(scrollXRelativePersent != 0F){
viewFlipper.setInAnimation(getInMoveAnimation(0F, true));
viewFlipper.setOutAnimation(getOutMoveAnimation(0F, true));
}
}
isAnimating = false;
}
});
translateAnimation.setDuration(durationMillis);
animationSet.addAnimation(translateAnimation);
return animationSet;
}
/**
* 获取离开动画
* @param scrollXRelativePersent 有正负之分
* @param showNext 显示下一个
* @return
*/
private final Animation getOutMoveAnimation(final float scrollXRelativePersent, final boolean showNext){
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());
TranslateAnimation translateAnimation = null;
if(scrollXRelativePersent == 0F){
translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0F,
Animation.RELATIVE_TO_SELF, (showNext ? leftMove : !leftMove) ? -1F : 1F,
Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F);
} else{
translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF,
scrollXRelativePersent > 0F ? scrollXRelativePersent - 1F : 1F + scrollXRelativePersent,
Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F);
}
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if(currentView != null){
currentView.scrollTo(0, 0);
}
}
});
translateAnimation.setDuration(durationMillis);
animationSet.addAnimation(translateAnimation);
return animationSet;
}
/**
* 获取还原动画
* @param scrollXRelativePersent 有正负之分
* @return
*/
private final Animation getBackInAnimation(final float scrollXRelativePersent){
if(currentView != null){
currentView.scrollTo(0, 0);
}
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, - scrollXRelativePersent,
Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F);
translateAnimation.setInterpolator(new DecelerateInterpolator());
translateAnimation.setDuration(durationMillis);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
isAnimating = true;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
isAnimating = false;
}
});
return translateAnimation;
}
/**
* 获取还原动画
* @param scrollXRelativePersent 有正负之分
* @return
*/
private final Animation getBackOutAnimation(final float scrollXRelativePersent){
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF,
scrollXRelativePersent > 0F ? scrollXRelativePersent - 1F : 1F + scrollXRelativePersent,
Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0F);
translateAnimation.setInterpolator(new DecelerateInterpolator());
translateAnimation.setDuration(durationMillis);
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
/** 删除底部替换视图 */
if(bottomFrameLayout.getChildCount() > 0){
bottomFrameLayout.removeAllViews();
}
}
});
return translateAnimation;
}
/**
* 获取当前pageNumber
* @return
*/
public final int getCurrentPageNumber() {
return currentPageNumber;
}
/**
* 填充内容
* @param baseAdapter
*/
public final void setBaseAdapter(final BaseAdapter baseAdapter) {
this.baseAdapter = baseAdapter;
/** 设置进入进出动画 */
viewFlipper.setInAnimation(getInMoveAnimation(0F, true));
viewFlipper.setOutAnimation(getOutMoveAnimation(0F, true));
/** 停止播放动画 */
if(isPlaying && viewFlipper.isFlipping()){
viewFlipper.stopFlipping();
}
/** 加载视图信息 */
if(viewFlipper.getChildCount() > 0){
viewFlipper.removeAllViews();
}
if(baseAdapter != null && baseAdapter.getCount() > 0){
for (int i = 0; i < baseAdapter.getCount(); i++) {
View galleryItemView = baseAdapter.getView(i, null, null);
galleryItemView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
viewFlipper.addView(galleryItemView);
if(onCurrentViewChangedListener != null && (i < (befLoadExtraViewCount + 1)
|| (baseAdapter.getCount() - i) < (befLoadExtraViewCount + 1))){
onCurrentViewChangedListener.onCurrentViewChanged(galleryItemView, i);
}
}
/** 设置默认选中第一个 */
if(baseAdapter.getCount() > 0){
currentPageNumber = (currentPageNumber == -1 ? 0 : currentPageNumber);
}
} else{
currentPageNumber = -1;
}
/** 重新播放动画 */
if(isPlaying && !viewFlipper.isFlipping()){
viewFlipper.startFlipping();
}
}
/**
* 获取内容适配器
* @return
*/
public final BaseAdapter getBaseAdapter() {
return baseAdapter;
}
/**
* 设置 预加载视图个数
* @param befLoadExtraViewCount
*/
public final void setBefLoadExtraViewCount(final int befLoadExtraViewCount) {
this.befLoadExtraViewCount = befLoadExtraViewCount;
}
/**
* 获取预加载视图个数
* @return
*/
public final int getBefLoadExtraViewCount() {
return befLoadExtraViewCount;
}
/**
* 设置 最小滑动距离
* @param minMoveDistance
*/
public final void setMinMoveDistance(final float minMoveDistance) {
this.minMoveDistance = BaseApplication.getWidth(minMoveDistance);
}
/**
* 设置视图与视图之间的偏移量比率
* @param viewOffset
*/
public final void setViewOffset(final float viewOffset) {
this.viewOffset = viewOffset;
}
/**
* 设置动画是左移还是右移
* @param leftMove
*/
public final void setLeftMove(final boolean leftMove) {
this.leftMove = leftMove;
/** 设置进入进出动画 */
viewFlipper.setInAnimation(getInMoveAnimation(0F, true));
viewFlipper.setOutAnimation(getOutMoveAnimation(0F, true));
}
/**
* 设置停止滑动
* @param stopMove
*/
public final void setStopMove(final boolean stopMove) {
this.stopMove = stopMove;
}
/**
* 设置 动画时间(以时间戳毫秒为单位)
* @param durationMillis
*/
public final void setDurationMillis(final long durationMillis) {
this.durationMillis = durationMillis;
}
/**
* 设置 自动切换时间间隔(以时间戳毫秒为单位)
* @param milliseconds
*/
public final void setFlipInterval(final int milliseconds){
this.milliseconds = milliseconds;
viewFlipper.setFlipInterval(this.milliseconds);
}
/**
* 设置当前视图被改变监听接口
* @param onCurrentViewChangedListener
*/
public final void setOnCurrentViewChangedListener(final OnCurrentViewChangedListener onCurrentViewChangedListener) {
this.onCurrentViewChangedListener = onCurrentViewChangedListener;
}
/**
* 设置页面被改变监听接口
* @param onPageChangedListener
*/
public final void setOnPageChangedListener(final OnPageChangedListener onPageChangedListener) {
this.onPageChangedListener = onPageChangedListener;
}
/**
* 设置Gallery被点击事件
* @param onGalleryClickListener
*/
public final void setOnGalleryClickListener(final OnGalleryClickListener onGalleryClickListener) {
this.onGalleryClickListener = onGalleryClickListener;
}
/**
* 获取真正的页面号码
* @param pageNumber
* @return
*/
public final int getRealPageNumber(int pageNumber){
if(pageNumber < 0){
pageNumber += baseAdapter.getCount();
} else if(pageNumber >= baseAdapter.getCount()){
pageNumber -= baseAdapter.getCount();
}
return pageNumber;
}
/**
* 获取指定页面的视图
* @param pageNumber
* @return
*/
public final View getFlipperChildView(final int pageNumber){
if(baseAdapter != null && baseAdapter.getCount() > 0){
return viewFlipper.getChildAt(getRealPageNumber(pageNumber));
} else {
return null;
}
}
/**
* 设置 pageNumber
* @param pageNumber
*/
public final void setPageNumber(final int pageNumber) {
final int realPageNumber = getRealPageNumber(pageNumber);
if(realPageNumber != currentPageNumber){
/** 若正在播放中,则停止播放动画 */
if(isPlaying && viewFlipper.isFlipping()){
viewFlipper.stopFlipping();
}
/** 设置进入进出动画 */
viewFlipper.setInAnimation(getInMoveAnimation(0F, realPageNumber > currentPageNumber));
viewFlipper.setOutAnimation(getOutMoveAnimation(0F, realPageNumber > currentPageNumber));
// 设置具体页面
viewFlipper.setDisplayedChild(realPageNumber);
/** 重新开始播放动画 */
if(isPlaying && !viewFlipper.isFlipping()){
viewFlipper.setInAnimation(null);
viewFlipper.setOutAnimation(null);
viewFlipper.startFlipping();
viewFlipper.setInAnimation(getInMoveAnimation(0F, true));
viewFlipper.setOutAnimation(getOutMoveAnimation(0F, true));
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(!stopMove && baseAdapter != null && baseAdapter.getCount() > 1){
final int actionId = event.getAction();
if(actionId == MotionEvent.ACTION_DOWN){
movedAble = !isAnimating;
/** 获取按下点坐标,并且停止播放 */
if(movedAble){
rawX = event.getRawX();
rawY = event.getRawY();
/** 若正在播放中,则停止动画 */
if(isPlaying && viewFlipper.isFlipping()){
viewFlipper.stopFlipping();
}
return true;
}
} else if(movedAble && actionId == MotionEvent.ACTION_MOVE){
/** 获取当前移动距离 */
currentMovedDistance = rawX - event.getRawX();
float maxViewMoveDistance = ((float) viewFlipper.getWidth()) * 0.95F;
currentMovedDistance = (Math.abs(currentMovedDistance) > maxViewMoveDistance
? (currentMovedDistance > 0F ? maxViewMoveDistance : - maxViewMoveDistance) : currentMovedDistance);
/** 移动视图*/
if(Math.abs(currentMovedDistance) >= BaseApplication.getWidth(4F)){
// 获取下一页码
int nextPageNumber = getRealPageNumber(currentPageNumber + (currentMovedDistance > 0F
? (leftMove ? 1 : -1) : (leftMove ? -1 : 1)));
if(nextPageNumber != bottomFrameIndex){
/** 获取下一个视图 */
nextView = baseAdapter.getView(nextPageNumber, null, null);
nextView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
/** 将下一个视图添加到底部视图 */
if(bottomFrameLayout.getChildCount() > 0){
bottomFrameLayout.removeAllViews();
}
bottomFrameLayout.addView(nextView);
/** 调用视图变化监听器 */
if(onCurrentViewChangedListener != null){
onCurrentViewChangedListener.onCurrentViewChanged(nextView, nextPageNumber);
}
bottomFrameIndex = nextPageNumber;
}
// 获取当前视图
currentView = viewFlipper.getChildAt(currentPageNumber);
// 滑动当前视图
currentView.scrollTo((int) currentMovedDistance, 0);
/** 滑动下一个视图 */
if(nextView != null){
if(currentMovedDistance > 0F){
/** 左移 */
nextView.scrollTo((int) (currentMovedDistance - (float) bottomFrameLayout.getWidth()
- (viewOffset * (float) bottomFrameLayout.getWidth())), 0);
} else{
/** 右移 */
nextView.scrollTo((int) (currentMovedDistance + (float) bottomFrameLayout.getWidth()
+ (viewOffset * (float) bottomFrameLayout.getWidth())), 0);
}
}
} else{
if(currentView != null){
currentView.scrollTo(0, 0);
}
if(nextView != null){
nextView.scrollTo(0, 0);
}
}
return true;
} else if(movedAble && actionId == MotionEvent.ACTION_UP){
// 获取当前移动距离
currentMovedDistance = rawX - event.getRawX();
/** 处理当前点击视图 */
if(((float) Math.hypot(currentMovedDistance, rawY - event.getRawY())) < BaseApplication.getWidth(4F)
&& onGalleryClickListener != null){
onGalleryClickListener.onGalleryClicked(currentPageNumber);
}
/** 设置可以滑动的最大距离 */
float maxViewMoveDistance = ((float) viewFlipper.getWidth()) * 0.95F;
currentMovedDistance = (Math.abs(currentMovedDistance) > maxViewMoveDistance
? (currentMovedDistance > 0F ? maxViewMoveDistance : - maxViewMoveDistance) : currentMovedDistance);
if(Math.abs(currentMovedDistance) >= minMoveDistance){
/** 切换动画 */
// 设置进入动画
viewFlipper.setInAnimation(getInMoveAnimation((float) (nextView == null ? 0
: nextView.getScrollX()) / (float) bottomFrameLayout.getWidth(), true));
// 设置离开动画
viewFlipper.setOutAnimation(getOutMoveAnimation((float) (currentView == null ? 0
: currentView.getScrollX()) / (float) viewFlipper.getWidth(), true));
if(currentMovedDistance > 0F){
if(leftMove){
viewFlipper.showNext();
} else{
viewFlipper.showPrevious();
}
} else{
if(leftMove){
viewFlipper.showPrevious();
} else{
viewFlipper.showNext();
}
}
} else{
// 还原 底图视图的索引值
bottomFrameIndex = -1;
/** 以动画还原视图 */
viewFlipper.setInAnimation((currentView != null && currentView.getScrollX() != 0) ?
getBackInAnimation((float) currentView.getScrollX() / (float) viewFlipper.getWidth()) : null);
viewFlipper.setOutAnimation(null);
if(isPlaying && !viewFlipper.isFlipping()){
viewFlipper.startFlipping();
} else {
viewFlipper.setDisplayedChild(currentPageNumber);
}
if(nextView != null && nextView.getScrollX() != 0){
nextView.startAnimation(getBackOutAnimation((float) nextView.getScrollX()
/ (float) bottomFrameLayout.getWidth()));
}
viewFlipper.setInAnimation(getInMoveAnimation(0F, true));
viewFlipper.setOutAnimation(getOutMoveAnimation(0F, true));
}
return true;
}
}
return false;
}
//============================== 关于 viewFlipper 的设置 ==============================
/**
* 开始播放
*/
public final void startFlipping(){
if(!isPlaying){
isPlaying = true;
if(!viewFlipper.isFlipping()){
viewFlipper.startFlipping();
}
}
}
/**
* 停止播放
*/
public final void stopFlipping(){
if(isPlaying){
isPlaying = false;
if(viewFlipper.isFlipping()){
viewFlipper.stopFlipping();
}
}
}
/**
* 下一个视图
*/
public final void showNext() {
setPageNumber(getRealPageNumber(currentPageNumber + 1));
}
/**
* 上一个视图
*/
public final void showPrevious() {
setPageNumber(getRealPageNumber(currentPageNumber - 1));
}
/**
* 是否处于播放中
* @return
*/
public final boolean isFlipping(){
return viewFlipper.isFlipping();
}
//============================== 监听接口 ==============================
/**
* 当前视图被改变监听接口
*/
public static interface OnCurrentViewChangedListener {
/**
* @param currentView 当前视图
* @param currentPageNumber 当前页码
*/
void onCurrentViewChanged (final View currentView, final int currentPageNumber);
}
/**
* 页码被改变监听接口
*/
public static interface OnPageChangedListener {
/**
* @param currentPageNumber 当前页码
*/
void onPageChanged (final int currentPageNumber);
}
/**
* 被点击接口
*/
public static interface OnGalleryClickListener {
/**
* @param currentPageNumber 当前页码
*/
void onGalleryClicked(final int currentPageNumber);
}
}
2:测试 Activity类(TestFlingGalleryActivity.java)
package com.learnmate.lms.client.activity;
import java.util.HashSet;
import java.util.Set;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.learnmate.lms.client.R;
import com.vaode.common.widget.FlingGallery;
import com.vaode.common.widget.FlingGallery.OnCurrentViewChangedListener;
import com.vaode.common.widget.FlingGallery.OnGalleryClickListener;
import com.vaode.common.widget.FlingGallery.OnPageChangedListener;
/**
* @author 王祝龙
* date 2014/04/25
* function FlingGallery 测试 Activity
*/
public class TestFlingGalleryActivity extends Activity implements OnCurrentViewChangedListener,
OnPageChangedListener, OnGalleryClickListener {
private FlingGallery flingGallery;
private FlingGalleryAdapter flingGalleryAdapter;
private TextView pageNumberText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_fling_gallery);
flingGallery = (FlingGallery) findViewById(R.id.flingGalleryId);
pageNumberText = (TextView) findViewById(R.id.pageNumberTextId);
flingGallery.setOnCurrentViewChangedListener(this);
flingGallery.setOnPageChangedListener(this);
flingGallery.setOnGalleryClickListener(this);
flingGalleryAdapter = new FlingGalleryAdapter(this);
flingGallery.setBaseAdapter(flingGalleryAdapter);
pageNumberText.setText("当前页面为【" + 0 + "】");
flingGallery.startFlipping();
}
@Override
public void onCurrentViewChanged(View currentView, int currentPageNumber) {
/** 加载当前图片 */
LinearLayout homePageLayout = (LinearLayout) currentView;
if(homePageLayout.getChildCount() == 0){
homePageLayout.addView(flingGalleryAdapter.getItem(currentPageNumber));
}
/** 去除相邻的图片 */
int extraCount = flingGallery.getBefLoadExtraViewCount() + 1;
if(flingGalleryAdapter.getCount() > (extraCount * 2)){
Set<Integer> positionSet = new HashSet<Integer>();
positionSet.add(flingGallery.getRealPageNumber(currentPageNumber - extraCount));
positionSet.add(flingGallery.getRealPageNumber(currentPageNumber + extraCount));
for(int positionVal : positionSet){
if(Math.abs(positionVal - currentPageNumber) >= extraCount){
LinearLayout viewFlipperLayout = (LinearLayout) flingGallery.getFlipperChildView(positionVal);
if(viewFlipperLayout != null && viewFlipperLayout.getChildCount() > 0){
viewFlipperLayout.removeAllViews();
}
}
}
}
}
@Override
public void onPageChanged(int currentPageNumber) {
pageNumberText.setText("当前页面为【" + currentPageNumber + "】");
}
@Override
public void onGalleryClicked(int currentPageNumber) {
Toast.makeText(this, "页码号 【" + currentPageNumber + "】 被点击了……", Toast.LENGTH_SHORT).show();
}
/**
* 主要logo适配器
*/
private final class FlingGalleryAdapter extends BaseAdapter {
private final int[] home_page_logos = {R.drawable.pi_1, R.drawable.pi_2,
R.drawable.pi_3, R.drawable.pi_4, R.drawable.pi_5, R.drawable.pi_6};
private Context context;
public FlingGalleryAdapter(Context context){
this.context = context;
}
@Override
public int getCount() {
return home_page_logos.length;
}
@Override
public ImageView getItem(int position) {
ImageView homePageLogoImg = new ImageView(context);
homePageLogoImg.setBackgroundResource(home_page_logos[position]);
homePageLogoImg.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
return homePageLogoImg;
}
@Override
public long getItemId(int position) {
return ((long) home_page_logos[position]);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout homePageLayout = new LinearLayout(context);
homePageLayout.setOrientation(LinearLayout.VERTICAL);
homePageLayout.setGravity(Gravity.CENTER);
return homePageLayout;
}
}
}
3:布局文件test_fling_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<com.vaode.common.widget.FlingGallery
android:id="@+id/flingGalleryId"
android:layout_width="300dp"
android:layout_height="265dp" >
<TextView
android:id="@+id/pageNumberTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:textColor="@android:color/white"
android:textSize="24dp" />
</com.vaode.common.widget.FlingGallery>
</LinearLayout>