需要有关android布局设计的帮助

时间:2013-10-25 15:16:11

标签: android mobile

我对android开发很感兴趣。我想在android中制作类似于主页选项卡的屏幕。在主页选项卡上有两个视图。通过向左或向右拖动视图,我可以移动视图以显示下一个视图。请帮助我如何在Android中做到这一点。我需要详细回复或一些教程链接。

以下是截屏。我想制作完全相同的副本。

屏幕1

enter image description here

http://screencast.com/t/pJaLLHT74fH0

屏幕2

http://screencast.com/t/QCaOlmVJv5sJ

http://screencast.com/t/QCaOlmVJv5sJ

2 个答案:

答案 0 :(得分:1)

实现此目的的一种方法是使用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/

答案 1 :(得分:0)

我假设您已经实现了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;
    }

}



}