我是android开发的初学者。我想在 android 中制作类似的屏幕,如主页选项卡。在主页选项卡上,它有两个视图。通过向左或向右拖动视图,我可以移动视图以显示下一个视图。请帮助我如何在android中做到这一点。我需要详细回复或一些教程链接。
这是屏幕截图。我想制作完全相同的副本。
屏幕一
http://screencast.com/t/pJaLLHT74fH0
屏幕 2
我是android开发的初学者。我想在 android 中制作类似的屏幕,如主页选项卡。在主页选项卡上,它有两个视图。通过向左或向右拖动视图,我可以移动视图以显示下一个视图。请帮助我如何在android中做到这一点。我需要详细回复或一些教程链接。
这是屏幕截图。我想制作完全相同的副本。
屏幕一
http://screencast.com/t/pJaLLHT74fH0
屏幕 2
实现这一点的一种方法是使用 ViewPager 类。Android 的开发者网站有一个很好的解释和一个如何在这里使用 ViewPager 的例子:
http://developer.android.com/training/animation/screen-slide.html
相反,如果您想在选项卡之间滑动,它们还提供了以下示例:
http://developer.android.com/training/implementing-navigation/lateral.html
对于底部的圆形指示器,您可以实现 Jake Wharton 的 ViewPagerIndicator 库。您可以下载它并在此处找到几个示例:http: //viewpagerindicator.com/
我假设您已经实现了 view1 和 view2。所以,这是我的解决方案:
public class FatherLayout extends RelativeLayout implements GestureDetector.OnGestureListener, GestureDetector.OnScrollListener{
private YourView view1 ;
private YourView view2;
int acitveView; // this var indicates which view is active
// your gesture detector here that you handle the drag events!
private GestureDetectorCompat mDetector;
public FatherLayout(Context context){
super(context);
mDetector = new GestureDetectorCompat(this,this);
mDetector.setOnScrollListener(this);
this.addView(view2); // i assume that you want the view1 appears on the top
this.addView(view1);
activeView = 1;
invalidate();
}
/*Following YOUR GESTURE DETECTOR IMPLEMENTATION HERE*/
@Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// Be sure to call the superclass implementation
return super.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent event) {
Log.d(DEBUG_TAG,"onDown: " + event.toString());
return false;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
return false;
}
@Override
public void onLongPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// HERE YOU WILL DO ALL THE JOB.
// FOR INSTANCE IF YOU WANT TO move the view1 you will call something like this :
if(activeView == 1){
view1.setVisible(View.VISIBLE);
view2.setVisible(View.INVISIBLE);
activeView = 0;
}else if(activeView == 0){
view1.setVisible(View.INVISIBLE);
view2.setVisible(View.VISIBLE);
activeView = 1;
}
return false;
}
@Override
public void onShowPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
return false;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return false;
}
}
}