Domanda

I've been stuck on this problem for 2 days, trying to solve on my own. I want to my app to have navigation drawer, fragments are difficult for me to understand. I don't want to change any of my code. I have seen this example, but it is not helping me to understand much.

I just have no idea, how to implement navigation without drawer.

Any help is appreciated.

Thanks.

È stato utile?

Soluzione

using this code you can do any sliding animation menu layout is the one slides form left and actual layout is your layout visible to user first. total layout contains both. i got the same problem once so i have done this code its working very fine

all the best :)

Code for MainActivity.java

package com.example.slidemenuanimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements AnimationListener {

    int ActualLayoutWidth, MenuLayoutWidth, TotalLayoutWidth;
    RelativeLayout actualLayout, totalLayout;
    LinearLayout menuLayout;
    RelativeLayout.LayoutParams menuLayoutParams, actualLayoutParams;
    FrameLayout.LayoutParams totallayoutParams;

    ImageView menulayoutImage, actualLayoutImage;

    TranslateAnimation MenuClickRightAnim, MenuClickLeftAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Display display = getWindowManager().getDefaultDisplay();
        ActualLayoutWidth = display.getWidth();
        MenuLayoutWidth = display.getWidth();
        TotalLayoutWidth = ActualLayoutWidth + MenuLayoutWidth;

        menuLayout = (LinearLayout) findViewById(R.id.menu_layout);
        actualLayout = (RelativeLayout) findViewById(R.id.actuallayout);
        totalLayout = (RelativeLayout) findViewById(R.id.totallayout);

        // parmeters for total layout
        totallayoutParams = new FrameLayout.LayoutParams(TotalLayoutWidth,
                FrameLayout.LayoutParams.FILL_PARENT, 3);
        totallayoutParams.setMargins(-MenuLayoutWidth, 0, 0, 0);
        totalLayout.setLayoutParams(totallayoutParams);

        // parmeters for menu layout
        menuLayoutParams = new RelativeLayout.LayoutParams(MenuLayoutWidth,
                RelativeLayout.LayoutParams.FILL_PARENT);
        menuLayout.setLayoutParams(menuLayoutParams);

        // parameters for actual layout
        actualLayoutParams = new RelativeLayout.LayoutParams(ActualLayoutWidth,
                RelativeLayout.LayoutParams.FILL_PARENT);
        actualLayoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.menu_layout);
        actualLayout.setLayoutParams(actualLayoutParams);

        // intializing animations
        // menu moves right
        MenuClickRightAnim = new TranslateAnimation(0, MenuLayoutWidth, 0, 0);
        MenuClickRightAnim.setFillAfter(true);
        MenuClickRightAnim.setAnimationListener(this);
        MenuClickRightAnim.setDuration(750);

        // menu moves left
        MenuClickLeftAnim = new TranslateAnimation(0, -MenuLayoutWidth, 0, 0);
        MenuClickLeftAnim.setAnimationListener(this);
        MenuClickLeftAnim.setFillAfter(true);
        MenuClickLeftAnim.setDuration(750);

        // init widgets
        menulayoutImage = (ImageView) findViewById(R.id.imagein_menulayout);

        menulayoutImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                totalLayout.setAnimation(MenuClickLeftAnim);
                totalLayout.startAnimation(MenuClickLeftAnim);
            }
        });

        actualLayoutImage = (ImageView) findViewById(R.id.imagein_actuallayout);

        actualLayoutImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                totalLayout.setAnimation(MenuClickRightAnim);
                totalLayout.startAnimation(MenuClickRightAnim);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onAnimationEnd(Animation animation) {

        if (animation == MenuClickRightAnim) {

            // setting params for total layout because naimtion transfers raw
            // pizels not actual layout
            totalLayout.clearAnimation();
            totallayoutParams = new FrameLayout.LayoutParams(TotalLayoutWidth,
                    FrameLayout.LayoutParams.FILL_PARENT, 3);
            totallayoutParams.setMargins(0, 0, 0, 0);
            totalLayout.setLayoutParams(totallayoutParams);

        }
        if (animation == MenuClickLeftAnim) {

            totalLayout.clearAnimation();
            totallayoutParams = new FrameLayout.LayoutParams(TotalLayoutWidth,
                    FrameLayout.LayoutParams.FILL_PARENT, 3);
            totallayoutParams.setMargins(-MenuLayoutWidth, 0, 0, 0);
            totalLayout.setLayoutParams(totallayoutParams);

        }

    }

    @Override
    public void onAnimationRepeat(Animation arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationStart(Animation arg0) {
        // TODO Auto-generated method stub

    }

}

code for xml activitymain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/totallayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F6F6F6"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/menu_layout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imagein_menulayout"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/actuallayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imagein_actuallayout"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/ic_launcher" />
    </RelativeLayout>

</RelativeLayout>

Altri suggerimenti

I too had this problem. if your problem is having multiple fragments you can fix it but it is not ideal. What you would have to do is make multiple activities and in each one you could have to recreate the naviagation drawer. But no matter what you will still have to set the view to a fragment.

BUT.. if you wanted to use NO fragments the only way to do it would be to add a drawer layout to each of your xml layouts (a navigation drawer is just a drawer layout on the side of your screen). This is what people did before google introduced their own navigation drawer...

I know this may not be the answer you are looking for but I hope it helps

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top