当前位置:操作系统 > 安卓/Android >>

利用FrameLayout连接一组view实现流畅的左右滑动

前段时间在网上找到一个左右滑动的例子,广泛使用在uc,墨迹天气等知名软件中,网上实现了一个布局的类(具体代码详见附件),继承FrameLayout,声明如下:
        public class FlingGallery extends FrameLayout;
 
该类声明的变量如下:
 
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; 
 
其中主要变量有 mCurrentPosition:当前索引。
                             mCurrentViewNumber:当前view的索引。
                             mViews 用来存放一组滑动的view。
                             mAnimation 动画动作。
 
        类中还封装了类似跳转到下一个view和上一个view的常用切换方法,以及一些常用的布局方法。
 
在activity中可以这样使用:
 
 
@Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        mGallery = new FlingGallery(this); 
        //mGallery.setPaddingWidth(5); 
        mGallery.setAdapter(mBabyListAdapter); 
        mGallery.setIsGalleryCircular(true); 
 
        LinearLayout layout = new LinearLayout(getApplicationContext()); 
        layout = (LinearLayout) PublicFunctionAndUnit.getMainView( 
                GalleryTest.this, layout, mGallery); 
        layout.setBackgroundResource(R.drawable.main_bg); 
 
        setContentView(layout); 
 
    } 
在adapter中把要显示的views放入其中,代码如下:
 
BaseAdapter mBabyListAdapter = new BaseAdapter() { 
 
@Override
public int getCount() { 
return 7; 

 
@Override
public Object getItem(int position) { 
return null; 

 
@Override
public long getItemId(int position) { 
return 0; 

 
@Override
public View getView(int position, View convertView, ViewGroup parent) { 
// GalleryViewItem item = new GalleryViewItem(GalleryTest.this, 
// position); 
LinearLayout item = getLinearLayout(GalleryTest.this, position); 
return item; 

}; 
private int[] itemViewIds = new int[] { R.layout.item1, R.layout.item2, 
R.layout.item3, R.layout.item4, R.layout.item5, R.layout.item6, 
R.layout.item7 }; 
 
private LinearLayout getLinearLayout(Context context, int position) { 
LinearLayout layout = new LinearLayout(context); 
LayoutInflater factory = LayoutInflater.from(context); 
View item = factory.inflate(itemViewIds[position], null); 
layout.addView(item, new LinearLayout.LayoutParams( 
LinearLayout.LayoutParams.FILL_PARENT, 
LinearLayout.LayoutParams.FILL_PARENT)); 
return layout; 

        在使用过程中,发现该类,只有跳转到上一个和下一个view 的方法,不能指定跳转到某个view,仔细观察代码后,这个方法可以使用它所给的nextd等实现,代码如下:
public void moveToViewbyId(int id) 
        { 
                  int num=id-mCurrentPosition; 
                  if(num!=0) 
                  { 
                           if(num>0) 
                           { 
                                    for(int i=0;i<num;i++) 
                                    { 
                                              moveNext(); 
                                    } 
                           } 
                           else
                           { 
                                    for(int i=0;i<(0-num);i++) 
                                    { 
                                              movePrevious(); 
 &n

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,