利用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, <
相关新闻>>
- 发表评论
-
- 最新评论 更多>>