Swiping the Fly-in sliding menu android

893 views Asked by At

I'm new to programming android, I'm trying to make the effect of sliding menu with your finger, followed several tutorials over the internet but got no success, I found this library on the Internet, it works by clicking on the button, I have the same effect when sliding the finger. Thank you in advance for your help!

LayerStack.java

package com.example.slideoutmenu;

import com.example.slideoutmenu.LayerStack;
import com.example.slideoutmenu.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LayerStack extends Activity {

    //Declare
    private LinearLayout slidingPanel;
    private boolean isExpanded;
    private DisplayMetrics metrics; 
    private ListView listView;
    private RelativeLayout headerPanel;
    private RelativeLayout menuPanel;
    private int panelWidth;
    private ImageView menuViewButton;
    Button menu1, menu2,menu3,menu4 ;
    TextView txtpays;
    FrameLayout.LayoutParams menuPanelParameters;
    FrameLayout.LayoutParams slidingPanelParameters;
    LinearLayout.LayoutParams headerPanelParameters ;
    LinearLayout.LayoutParams listViewParameters;
    ImageView lampada;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layer_stack);      
        //Initialize
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        panelWidth = (int) ((metrics.widthPixels)*0.80);

        headerPanel = (RelativeLayout) findViewById(R.id.header);
        headerPanelParameters = (LinearLayout.LayoutParams) headerPanel.getLayoutParams();
        headerPanelParameters.width = metrics.widthPixels;
        headerPanel.setLayoutParams(headerPanelParameters);

        menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
        menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
        menuPanelParameters.width = panelWidth;
        menuPanel.setLayoutParams(menuPanelParameters);

        slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
        slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
        slidingPanelParameters.width = metrics.widthPixels;
        slidingPanel.setLayoutParams(slidingPanelParameters);

        listView = (ListView) findViewById(R.id.listView1);
        listViewParameters = (LinearLayout.LayoutParams) listView.getLayoutParams();
        listViewParameters.width = metrics.widthPixels;
        listView.setLayoutParams(listViewParameters);

        //Slide the Panel   ;
        //lampada = (ImageView) findViewById(R.id.imagelampada);
        menu1 = (Button) findViewById(R.id.menu_item_1);    
        txtpays = (TextView) findViewById(R.id.listepays);  
        menu2 = (Button) findViewById(R.id.menu_item_2);
        menu3 = (Button) findViewById(R.id.menu_item_3);
        menu4 = (Button) findViewById(R.id.menu_item_4);
        txtpays.setText("Clique acima para acessar o menu");

        clicabotao();


        //listaClica();

        menuViewButton = (ImageView) findViewById(R.id.menuViewButton);     
        menuViewButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(!isExpanded){
                    expandMenu();                                   
                }else{
                    expandList();               
                }              
            }
        });

    }   
    //############################################# FUNÇÃO CRIA MENUS CLICK ##########################################
    //    função: executar codigo individual de ação ao clicar em botão do menu aposentos   
    public void clicabotao(){

        // CONFIGURA ONCLICK DOS BOTÕES MENU
        menu1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    txtpays.setText("Clique acima para acessar o menu");
                    expandList();
                }catch(Exception e){
                    Log.i("Mensagem ",""+e.getMessage());
                }

            }
        });      
        menu2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub        
                try{
                    txtpays.setText("Clique acima para acessar o menu");
                    expandList();
                }catch(Exception e){
                    Log.i("Mensagem ",""+e.getMessage());
                }


            }
        });
        menu3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{

                    txtpays.setText("Clique acima para acessar o menu");

                    expandList();
                }catch(Exception e){
                    Log.i("Mensagem ",""+e.getMessage());
                }

            }
        });
        menu4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{

                    txtpays.setText("Clique acima para acessar o menu");
                    expandList();
                }catch(Exception e){
                    Log.i("Mensagem ",""+e.getMessage());
                }

            }
        });
    }
    //############################################# FUNÇÃO CRIA ITEM DA LISTVIEW #############################################
    //############################################# FUNÇÃO EXPANDE MENU APOSENTOS #############################################
    public void expandMenu(){
        new ExpandAnimation(slidingPanel, panelWidth,
        Animation.RELATIVE_TO_SELF, 0.0f,
        Animation.RELATIVE_TO_SELF, 0.75f, 0, 0.0f, 0, 0.0f);
        isExpanded = true;
    }
    //############################################# FUNÇÃO EXPANDE A LISTVIEW ############################################
    public void expandList(){
        new CollapseAnimation(slidingPanel,panelWidth,
        TranslateAnimation.RELATIVE_TO_SELF, 0.80f,
        TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
        isExpanded = false;
    }
    class ListViewItem{
        public int ThumbnailResource;
        public String Title;
        public String SubTitle;
    }
}

ExpandAnimation.java

package com.example.slideoutmenu;

import android.view.Gravity;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;

public class ExpandAnimation extends TranslateAnimation implements Animation.AnimationListener{

    private LinearLayout slidingLayout;
    int panelWidth;

    public ExpandAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
            float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {

        super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);

        //Initialize
        slidingLayout = layout;
        panelWidth = width;
        setDuration(400);
        setFillAfter( false );
        setInterpolator(new AccelerateDecelerateInterpolator());
        setAnimationListener(this);
        slidingLayout.startAnimation(this);
    }


    public void onAnimationEnd(Animation arg0) {

        //Create margin and align left
        LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
        params.leftMargin = panelWidth;
        params.gravity = Gravity.LEFT;     
        slidingLayout.clearAnimation();
        slidingLayout.setLayoutParams(params);
        slidingLayout.requestLayout();

    }

    public void onAnimationRepeat(Animation arg0) {

    }

    public void onAnimationStart(Animation arg0) {

    }

}

CollapseAnimation.java

package com.example.slideoutmenu;

import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;


public class CollapseAnimation extends TranslateAnimation implements TranslateAnimation.AnimationListener{

    private LinearLayout slidingLayout;
    int panelWidth;

    public CollapseAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
            float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {

        super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);

        //Initialize
        slidingLayout = layout;
        panelWidth = width;
        setDuration(400);
        setFillAfter( false );
        setInterpolator(new AccelerateDecelerateInterpolator());
        setAnimationListener(this);

        //Clear left and right margins
        LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
        params.rightMargin = 0;
        params.leftMargin = 0;
        slidingLayout.setLayoutParams(params);
        slidingLayout.requestLayout();       
        slidingLayout.startAnimation(this);

    }
    public void onAnimationEnd(Animation animation) {

    }

    public void onAnimationRepeat(Animation animation) {

    }

    public void onAnimationStart(Animation animation) {

    }

}

activity_layer_stack.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="match_parent" 
    android:layout_weight="1"
    android:background="@drawable/blue_bg">

    <!-- Menu Panel -->
    <RelativeLayout
           android:id="@+id/menuPanel"
           android:layout_height="match_parent"
           android:layout_width="wrap_content"
           android:gravity="right"
           android:background="@drawable/agua_menu"
           android:orientation="vertical" >

            <Button
                android:id="@+id/menu_title_1"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="0dp" 
                android:paddingLeft="15dp"             
                android:gravity="center_vertical"                
                android:textColor="@android:color/white"
                android:text="@string/menu_title">   
            </Button>


            <Button
                android:id="@+id/menu_item_1"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="15dp"             
                android:gravity="center_vertical"
                android:layout_below="@+id/menu_title_1"          
                android:textColor="@android:color/white"
                android:text="Cozinha">     
            </Button>     
            <Button
                android:id="@+id/menu_item_2"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/menu_item_1"
                android:layout_marginLeft="15dp"
                android:gravity="center_vertical"                          
                android:textColor="@android:color/white"
                android:text="Sala">
            </Button>
            <Button
                android:id="@+id/menu_item_3"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/menu_item_2"
                android:layout_marginLeft="15dp"
                android:gravity="center_vertical"          
                android:textColor="@android:color/white"
                android:text="Suite">
            </Button>
          <!--  <View
                android:id="@+id/menu_item_divider_4"
                android:layout_width="fill_parent"
                android:layout_height="0.5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_below="@+id/menu_item_3"
                android:background="#353535"/>  -->
            <Button
                android:id="@+id/menu_item_4"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/menu_item_3"
                android:layout_marginLeft="15dp"
                android:gravity="center_vertical"          
                android:textColor="@android:color/white"
                android:text="Quarto Hospedes">
            </Button>
            </RelativeLayout>

       <!-- Sliding Panel -->     
        <LinearLayout
                android:id="@+id/slidingPanel"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="left"
                android:orientation="vertical"
                android:background="@drawable/fundo_branco" >

                <RelativeLayout
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="50dp">

                        <ImageView
                            android:id="@+id/menuViewButton"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="15dp"
                            android:clickable="true"
                            android:contentDescription="@string/description"
                            android:src="@drawable/icon_menu1"
                            android:visibility="visible" />


                </RelativeLayout>
                 <TextView
                    android:id="@+id/listepays"
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    android:layout_below="@+id/menu_item_divider_2"
                    android:layout_marginLeft="15dp"
                    android:gravity="center_vertical"
                    android:textAppearance="?android:attr/textAppearanceMedium"  
                    android:textColor="#00BFFF"
                    android:textColorHighlight="#550022" >
                    </TextView> 
            <View
                android:id="@+id/listaDivide"
                android:layout_width="fill_parent"
                android:layout_height="0.1dp"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="0dp"
                android:layout_below="@+id/menuViewButton"
                android:background="#000000"/>

            <View
                android:id="@+id/listaDivide2"
                android:layout_width="fill_parent"
                android:layout_height="0.1dp"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="0dp"
                android:layout_below="@+id/listepays"
                android:background="#000000"/>  




        </LinearLayout>
</FrameLayout>
0

There are 0 answers