网上找了很多关于FlingGallery类拖动的例子,还是很不错的,但是感觉不是很完美,拖动的只能是界面一致的布局,很不灵活,我觉得如果能够拖动不同的布局页面,就是拖动一下换一个页面布局,那就很完美了,于是我就在原有的基础上修改了一点点代码,最终被我给解决了这个问题。下面我就给大家看看我修改后的代码:先贴上几张图
FlingGalleryActivity.java 实现类:- package com.droidful.flinggallery;
-
- import Android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.CheckBox;
- import android.widget.LinearLayout;
- import android.widget.TableLayout;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class FlingGalleryActivity extends Activity
- {
- private final String[] mLabelArray = {"View1", "View2", "View3", "View4"};
-
- private FlingGallery mGallery;
- private CheckBox mCheckBox;
- private TextView t1;
- // Note: The following handler is critical to correct function of
- // the FlingGallery class. This enables the FlingGallery class to
- // detect when the motion event has ended by finger being lifted
-
- @Override
- public boolean onTouchEvent(MotionEvent event)
- {
- return mGallery.onGalleryTouchEvent(event);
- }
-
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
-
- mGallery = new FlingGallery(this);
- mGallery.setPaddingWidth(5);
- mGallery.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mLabelArray)
- {
- @Override
- public View getView(int position, View convertView, ViewGroup parent)
- {
- Log.d("msg", "count="+position);
- // if (convertView != null && convertView instanceof GalleryViewItem)
- // {
- // GalleryViewItem galleryView = (GalleryViewItem) convertView;
- //
- // galleryView.mEdit1.setText("");
- // galleryView.mText1.setText(mLabelArray[position]);
- // galleryView.mText1.setBackgroundColor(mColorArray[position]);
- // galleryView.mText2.setText(mLabelArray[position]);
- // galleryView.mText2.setBackgroundColor(mColorArray[position]);
- // Log.d("msg", "count="+position);
- // return galleryView;
- // }
- return new GalleryViewItem(getApplicationContext(), position);
- }
- });
-
- LinearLayout layout = new LinearLayout(getApplicationContext());
- layout.setOrientation(LinearLayout.VERTICAL);
-
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.MATCH_PARENT);
-
- //layoutParams.setMargins(10, 10, 10, 10);
- layoutParams.weight = 1.0f;
-
- layout.addView(mGallery, layoutParams);
-
- mCheckBox = new CheckBox(getApplicationContext());
- mCheckBox.setText("Gallery is Circular");
- mCheckBox.setPadding(50, 10, 0, 10);
- mCheckBox.setTextSize(30);
- mCheckBox.setChecked(true);
- mCheckBox.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View view)
- {
- mGallery.setIsGalleryCircular(mCheckBox.isChecked());
- }
- });
-
- layout.addView(mCheckBox, new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT,
- LinearLayout.LayoutParams.WRAP_CONTENT));
-
- setContentView(layout);
-
-
- }
- private class GalleryViewItem extends TableLayout
- {
- public GalleryViewItem(Context context, int position)
- {
- super(context);
-
- this.setOrientation(LinearLayout.VERTICAL);
- this.setLayoutParams(new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT));
-
- if(position==0){
- View view = (View)getLayoutInflater().inflate(R.layout.main01,null);
- //给里面控件设置事件监听
- t1 = (TextView)view.findViewById(R.id.main_view01);
- t1.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- Toast.makeText(getApplicationContext(), t1.getText().toString(), Toast.LENGTH_SHORT).show();
- }
- });
- this.addView(view,new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.FILL_PARENT));
- }else if(position==1){
- View view = (View)getLayoutInflater().inflate(R.layout.main02,null);
- this.addView(view,new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.FILL_PARENT));
- }else if(position==2){
- View view = (View)getLayoutInflater().inflate(R.layout.main03,null);
- this.addView(view,new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.FILL_PARENT));
- }else if(position==3){
- View view = (View)getLayoutInflater().inflate(R.layout.main04,null);
- this.addView(view,new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.FILL_PARENT));
- }
- }
- }
- }
FlingGallery.java 工具类- package com.droidful.flinggallery;
-
- import android.content.Context;
- import android.view.GestureDetector;
- import android.view.KeyEvent;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.view.animation.Interpolator;
- import android.view.animation.Transformation;
- import android.widget.Adapter;
- import android.widget.FrameLayout;
- import android.widget.LinearLayout;
-
- // TODO:
-
- // 1. In order to improve performance Cache screen bitmap and use for animation
- // 2. Establish superfluous memory allocations and delay or replace with reused objects
- // Probably need to make sure we are not allocating objects (strings, etc.) in loops
-
- //为了提高性能,使用缓存屏幕图动画
- //建立多余的内存分配和延迟或替换为重用对象可能需要确保我们不分配对象(字符串,等等。)在循环
- public class FlingGallery extends FrameLayout
- {
- // Constants 常量
-
- private final int swipe_min_distance = 120;
- private final int swipe_max_off_path = 250;
- private final int swipe_threshold_veloicty = 400;
-
- // Properties 属性
-
- private int mViewPaddingWidth = 0;
- private int mAnimationDuration = 250;
- private float mSnapBorderRatio = 0.5f;
- private boolean mIsGalleryCircular = true;
-
- // Members 成员
-
- private int mGalleryWidth = 0;
- private boolean mIsTouched = false;
- private boolean mIsDragging = false;
- private float mCurrentOffset = 0.0f;
- private long mScrollTimestamp = 0;
- private int mFlingDirection = 0;
- private int mCurrentPosition = 0;
- private int mCurrentViewNumber = 0;
-
- private Context mContext;
- private Adapter mAdapter;
- private FlingGalleryView[] mViews;
- private FlingGalleryAnimation mAnimation;
- private GestureDetector mGestureDetector;
- private Interpolator mDecelerateInterpolater;
-
- public FlingGallery(Context context)
- {
- super(context);
-
- mContext = context;
- mAdapter = null;
-
- mViews = new FlingGalleryView[3];
- mViews[0] = new FlingGalleryView(0, this);
- mViews[1] = new FlingGalleryView(1, this);
- mViews[2] = new FlingGalleryView(2, this);
-
- mAnimation = new FlingGalleryAnimation();
- mGestureDetector = new GestureDetector(new FlingGestureDetector());
- mDecelerateInterpolater = AnimationUtils.loadInterpolator(mContext, android.R.anim.decelerate_interpolator);
- }
-
- public void setPaddingWidth(int viewPaddingWidth)
- {
- mViewPaddingWidth = viewPaddingWidth;
- }
-
- public void setAnimationDuration(int animationDuration)
- {
- mAnimationDuration = animationDuration;
- }
-
- public void setSnapBorderRatio(float snapBorderRatio)
- {
- mSnapBorderRatio = snapBorderRatio;
- }
-
- public void setIsGalleryCircular(boolean isGalleryCircular)
- {
- if (mIsGalleryCircular != isGalleryCircular)
- {
- mIsGalleryCircular = isGalleryCircular;
-
- if (mCurrentPosition == getFirstPosition())
- {
- // We need to reload the view immediately to the left to change it to circular view or blank
- //我们需要重新加载视图立即到左边来改变它圆视图或空白
- mViews[getPrevViewNumber(mCurrentViewNumber)].recycleView(getPrevPosition(mCurrentPosition));
- }
-
- if (mCurrentPosition == getLastPosition())
- {
- // We need to reload the view immediately to the right to change it to circular view or blank
- //我们需要重新加载视图立即向右改变它圆视图或空白
- mViews[getNextViewNumber(mCurrentViewNumber)].recycleView(getNextPosition(mCurrentPosition));
- }
- }
- }
-
- public int getGalleryCount()
- {
- return (mAdapter == null) ? 0 : mAdapter.getCount();
- }
-
- public int getFirstPosition()
- {
- return 0;
- }
-
- public int getLastPosition()
- {
- return (getGalleryCount() == 0) ? 0 : getGalleryCount() - 1;
- }
-
- private int getPrevPosition(int relativePosition)
- {
- int prevPosition = relativePosition - 1;
-
- if (prevPosition < getFirstPosition())
- {
- prevPosition = getFirstPosition() - 1;
-
- if (mIsGalleryCircular == true)
- {
- prevPosition = getLastPosition();
- }
- }
-
- return prevPosition;
- }
-
- private int getNextPosition(int relativePosition)
- {
- int nextPosition = relativePosition + 1;
-
- if (nextPosition > getLastPosition())
- {
- nextPosition = getLastPosition() + 1;
-
- if (mIsGalleryCircular == true)
- {
- nextPosition = getFirstPosition();
- }
- }
-
- return nextPosition;
- }
-
- private int getPrevViewNumber(int relativeViewNumber)
- {
- return (relativeViewNumber == 0) ? 2 : relativeViewNumber - 1;
- }
-
- private int getNextViewNumber(int relativeViewNumber)
- {
- return (relativeViewNumber == 2) ? 0 : relativeViewNumber + 1;
- }
-
- @Override
- protected void onLayout(boolean changed, int left, int top, int right, int bottom)
- {
- super.onLayout(changed, left, top, right, bottom);
-
- // Calculate our view width 计算我们的视图的宽度
- mGalleryWidth = right - left;
-
- if (changed == true)
- {
- // Position views at correct starting offsets 在正确的开始偏移位置的观点
- mViews[0].setOffset(0, 0, mCurrentViewNumber);
- mViews[1].setOffset(0, 0, mCurrentViewNumber);
- mViews[2].setOffset(0, 0, mCurrentViewNumber);
- }
- }
-
- public void setAdapter(Adapter adapter)
- {
- mAdapter = adapter;
- mCurrentPosition = 0;
- mCurrentViewNumber = 0;
-
- // Load the initial views from adapter 加载初始视图从适配器
- mViews[0].recycleView(mCurrentPosition);
- mViews[1].recycleView(getNextPosition(mCurrentPosition));
- mViews[2].recycleView(getPrevPosition(mCurrentPosition));
-
- // Position views at correct starting offsets 在正确的开始偏移位置的观点
- mViews[0].setOffset(0, 0, mCurrentViewNumber);
- mViews[1].setOffset(0, 0, mCurrentViewNumber);
- mViews[2].setOffset(0, 0, mCurrentViewNumber);
- }
-
- private int getViewOffset(int viewNumber, int relativeViewNumber)
- {
- // Determine width including configured padding width 确定宽度包括配置填充宽度
- int offsetWidth = mGalleryWidth + mViewPaddingWidth;
-
- // Position the previous view one measured width to left 位置之前的观点一个测量宽度到左
- if (viewNumber == getPrevViewNumber(relativeViewNumber))
- {
- return offsetWidth;
- }
-
- // Position the next view one measured width to the right 位置下一个视图向右一个测量宽度
- if (viewNumber == getNextViewNumber(relativeViewNumber))
- {
- return offsetWidth * -1;
- }
-
- return 0;
- }
-
- void movePrevious()
- {
- // Slide to previous view 滑到上一个视图
- mFlingDirection = 1;
- processGesture();
- }
-
- void moveNext()
- {
- // Slide to next view 滑到下一个视图
- mFlingDirection = -1;
- processGesture();
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- switch (keyCode)
- {
- case KeyEvent.KEYCODE_DPAD_LEFT:
- movePrevious();
- return true;
-
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- moveNext();
- return true;
-
- case KeyEvent.KEYCODE_DPAD_CENTER:
- case KeyEvent.KEYCODE_ENTER:
- }
-
- return super.onKeyDown(keyCode, event);
- }
-
- public boolean onGalleryTouchEvent(MotionEvent event)
- {
- boolean consumed = mGestureDetector.onTouchEvent(event);
-
- if (event.getAction() == MotionEvent.ACTION_UP)
- {
- if (mIsTouched || mIsDragging)
- {
- processScrollSnap();
- processGesture();
- }
- }
-
- return consumed;
- }
-
- void processGesture()
- {
- int newViewNumber = mCurrentViewNumber;
- int reloadViewNumber = 0;
- int reloadPosition = 0;
-
- mIsTouched = false;
- mIsDragging = false;
-
- if (mFlingDirection > 0)
- {
- if (mCurrentPosition > getFirstPosition() || mIsGalleryCircular == true)
- {
- // Determine previous view and outgoing view to recycle 确定之前的观点和外向视图回收
- newViewNumber = getPrevViewNumber(mCurrentViewNumber);
- mCurrentPosition = getPrevPosition(mCurrentPosition);
- reloadViewNumber = getNextViewNumber(mCurrentViewNumber);
- reloadPosition = getPrevPosition(mCurrentPosition);
- }
- }
-
- if (mFlingDirection < 0)
- {
- if (mCurrentPosition < getLastPosition() || mIsGalleryCircular == true)
- {
- // Determine the next view and outgoing view to recycle 确定下一个视图和外向视图回收
- newViewNumber = getNextViewNumber(mCurrentViewNumber);
- mCurrentPosition = getNextPosition(mCurrentPosition);
- reloadViewNumber = getPrevViewNumber(mCurrentViewNumber);
- reloadPosition = getNextPosition(mCurrentPosition);
- }
- }
-
- if (newViewNumber != mCurrentViewNumber)
- {
- mCurrentViewNumber = newViewNumber;
-
- // Reload outgoing view from adapter in new position 重新加载外向视图从适配器在新位置
- mViews[reloadViewNumber].recycleView(reloadPosition);
- }
-
- // Ensure input focus on the current view 确保输入关注当前视图
- mViews[mCurrentViewNumber].requestFocus();
-
- // Run the slide animations for view transitions 运行这个幻灯片动画视图转换
-
- mAnimation.prepareAnimation(mCurrentViewNumber);
- this.startAnimation(mAnimation);
-
- // Reset fling state 重置扔状态
- mFlingDirection = 0;
- }
-
- void processScrollSnap()
- {
- // Snap to next view if scrolled passed snap position 对齐到下一个视图如果滚动通过快速的位置
- float rollEdgeWidth = mGalleryWidth * mSnapBorderRatio;
- int rollOffset = mGalleryWidth - (int) roll