본문 바로가기

개발도구/aOS - 안드로이드 개발

[Android] 좌우로 슬라이딩

MainActivity.java

package com.rosa.test.slideview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
   
    private MenuSlideView mSlideView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mSlideView = (MenuSlideView)findViewById(R.id.menu_slide);    
    }
}

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">   
    <Button
        android:id="@+id/menu_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Slide Button"
        android:layout_gravity="center_horizontal"/>   
    <com.rosa.test.slideview.MenuSlideView
        android:id="@+id/menu_slide"               
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        />           
</FrameLayout>


MenuSlideView.java

package com.rosa.test.slideview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.GestureDetector.OnGestureListener;
import android.widget.HorizontalScrollView;

public class MenuSlideView extends HorizontalScrollView{
       
    private Context mContext;   
    private int mScreenWidth;
   
    //Gesture
    private GestureDetector mGesture;   
    private GestureDetector.OnGestureListener mGesturesListener = new OnGestureListener(){

        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            Log.d("ROSA", "start onDown");
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,     float velocityY) {
            // TODO Auto-generated method stub
            Log.d("ROSA", "start onFling");           
           
            if(velocityX > 0){
                smoothScrollTo(0, 0);
            }else{
                smoothScrollTo(mScreenWidth, 0);
            }                       
               
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub
            Log.d("ROSA", "start onLongPress");
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // TODO Auto-generated method stub
            Log.d("ROSA", "start onScroll");
           
            smoothScrollBy((int)distanceX, 0);
           
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub
            Log.d("ROSA", "start onShowPress");
           
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            Log.d("ROSA", "start onSingleTapUp");
            return false;
        }
       
    };
   
    public MenuSlideView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mContext = context;
        createSubView();
    }

    public MenuSlideView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        mContext = context;       
        createSubView();
    }
   
    public MenuSlideView(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        mContext = context;       
        createSubView();
    }
   
    private void createSubView(){
       
        mGesture = new GestureDetector(mContext, mGesturesListener);
       
        //scroll view setting
        setHorizontalScrollBarEnabled(false);
       
        //get screen size
        Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        mScreenWidth = display.getWidth();       
       
        //set sub layout
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.menu_layout, null);       
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 100);
        addView(v, params);
    }
   
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mGesture.onTouchEvent(ev);
               
        int action = ev.getAction();
       
        switch(action){
        case MotionEvent.ACTION_UP:           
            if(getScrollX() < mScreenWidth/2){
                smoothScrollTo(0, 0);
            }else{
                smoothScrollTo(mScreenWidth, 0);               
            }               
            break;
        }       
        return true;
    }
}


menu_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">       
    <ImageView
        android:id="@+id/default_gesture"
        android:layout_width="320dip"
        android:layout_height="fill_parent"
        android:src="@drawable/icon"
        android:background="#C0E0FF"/>       
    <ImageView
        android:id="@+id/custom_gesture"
        android:layout_width="320dip"
        android:layout_height="fill_parent"
        android:src="@drawable/icon"       
        android:background="#F0C0FF"/>       
</LinearLayout>