如何使用片段将主内容和操作栏移动到导航抽屉中

问题描述 投票:0回答:1

我有一个自定义导航抽屉,使用片段从左到右打开。我想在导航打开时将我的内容和操作栏移动到右边,我读了一些来源,他们覆盖onDrawerSlide方法这样做,但是因为我使用片段来打开和关闭导航,我没有这样的方法来覆盖,我很感激有人可以帮助我(:

我试过this link移动内容

并使用this创建自定义导航视图

有主要活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container">

        <TextView
            android:id="@+id/homeContents"
            android:text="this is gonna be ur main page"
            android:layout_width="200dp"
            android:layout_height="46dp" />
    </FrameLayout>


</LinearLayout>

这是抽屉:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/rootLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="match_parent">


    <LinearLayout

        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/nav_profile"
            android:layout_gravity="center"
            android:layout_marginTop="40dp"
            android:layout_width="77dp"
            android:layout_height="77dp"
            android:src="@drawable/ic_user"
            />
        <LinearLayout
            android:layout_marginTop="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button

                android:drawableLeft="@drawable/ic_user_plus"
                android:drawablePadding="5dp"
                android:paddingLeft="10dp"
                android:id="@+id/sign_in"
                android:layout_width="91dp"
                android:layout_height="40dp"
                android:layout_marginStart="20dp"
                android:background="@drawable/more_btn_coop_req"
                android:text="SIGNUP"
                android:textColor="@color/blue" />
            <Button
                android:drawableLeft="@drawable/ic_sign_in_alt"
                android:drawablePadding="5dp"
                android:paddingLeft="10dp"
                android:id="@+id/sign_up"
                android:layout_width="91dp"
                android:layout_height="40dp"
                android:layout_marginStart="25dp"
                android:background="@drawable/more_btn_coop_req"
                android:text="ENTER"
                android:textColor="@color/blue" />
        </LinearLayout>
        <Spinner
            android:layout_marginTop="16dp"
            android:background="@drawable/spinner_background"
            android:id="@+id/cities_spinner"
            android:layout_gravity="center"
            android:layout_width="190dp"
            android:layout_height="40dp">

        </Spinner>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_marginTop="250dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:drawableLeft="@drawable/ic_check"
            android:drawablePadding="25dp"
            android:id="@+id/bookmark_centers"
            android:layout_gravity="center"
            android:background="@drawable/drawer_buttons_style"
            android:text="تخفیف های نشان شده"
            android:layout_width="186dp"
            android:layout_height="40dp" />
        <Button
            android:drawableLeft="@drawable/ic_store"
            android:id="@+id/followed_centers"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:background="@drawable/drawer_buttons_style"
            android:text="مراکز دنبال شده"
            android:layout_width="186dp"
            android:layout_height="40dp" />

        <Button
            android:id="@+id/tems"
            android:drawableLeft="@drawable/ic_info"
            android:background="@drawable/drawer_buttons_style"
            android:layout_marginTop="20dp"
            android:layout_gravity="center"
            android:text="شرایط استفاده"
            android:layout_width="186dp"
            android:layout_height="40dp" />
        <Button
            android:id="@+id/contact_us"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:drawableLeft="@drawable/ic_phone"
            android:background="@drawable/drawer_buttons_style"
            android:text="ارتباط با ما"
            android:layout_width="186dp"
            android:layout_height="40dp" />
        <Button
            android:id="@+id/share_us"
            android:layout_gravity="center"
            android:text="پیشنهاد به دوستان"
            android:layout_marginTop="10dp"
            android:drawableLeft="@drawable/ic_share_alt"
            android:background="@drawable/drawer_buttons_style"
            android:layout_width="186dp"
            android:layout_height="40dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/EXIT"
                android:layout_width="72sp"
                android:layout_height="40dp"
                android:layout_marginStart="32dp"
                android:background="@drawable/drawer_buttons_style"
                android:drawableLeft="@drawable/ic_sign_out_alt"
                android:text="خروج" />

            <Button
                android:id="@+id/edit"
                android:layout_width="105dp"
                android:layout_height="40dp"
                android:layout_marginStart="10dp"
                android:background="@drawable/drawer_buttons_style"
                android:drawableLeft="@drawable/ic_cog"
                android:text="ویرایش" />

        </LinearLayout>
        </LinearLayout>



</RelativeLayout>

有一个自定义工具栏和基本活动,合并了工具栏和抽屉,但我认为没有必要。这是菜单片段类:

package TestNavigationDrawer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.example.deathstroke.uniqueoff1.R;


public class MenuFragment extends Fragment implements 
View.OnTouchListener {

GestureDetector gestureDetector;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.slider_menu, container, 
false);
    RelativeLayout root =  rootView.findViewById(R.id.rootLayout);
    /*gestureDetector=new GestureDetector(getActivity(),new 
OnSwipeListener(){

        @Override
        public boolean onSwipe(Direction direction) {
            if (direction==Direction.up){
                //do your stuff
                ((MainActivity )  getActivity()).hideFragment();

            }

            if (direction==Direction.down){
                //do your stuff

            }
            return true;
        }


    });
    root.setOnTouchListener(this);*/
    return rootView;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    return true;
}
}

并且有mainActivity类:

package com.example.deathstroke.uniqueoff1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import TestNavigationDrawer.*;



public class MainActivity extends BaseActivity {

boolean isFragmentLoaded;
Fragment menuFragment;
TextView title;
ImageView menuButton;
RelativeLayout rootLayout;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.e("onCreate", "onCreate: " );
    super.onCreate(savedInstanceState);
    initAddlayout(R.layout.activity_main);
    Log.e("onCreate", "onCreate: " );
    rootLayout = findViewById(R.id.rootLayout);
    textView = findViewById(R.id.homeContents);
    title = findViewById(R.id.title_top);
    menuButton = findViewById(R.id.menu_icon);
    title.setText("Menu Activity");


    menuButton.setOnClickListener((View)-> {

            try {
                if (!isFragmentLoaded) {
                    loadFragment();
                    title.setText("Profile");
                } else {
                    if (menuFragment != null) {
                        if (menuFragment.isAdded()) {
                            hideFragment();
                        }
                    }
                }
            }catch (Exception e){
                Log.e("Menu", "onCreate: menu on click, e: " + e);
            }
    });
}

public void hideFragment(){
    FragmentTransaction fragmentTransaction =         
getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.slide_left, 
R.anim.slide_right);
    fragmentTransaction.remove(menuFragment);
    fragmentTransaction.commit();
    menuButton.setImageResource(R.drawable.ic_menu);
    isFragmentLoaded = false;
    title.setText("Main Activity");
}
public void loadFragment(){
    FragmentManager fm = getSupportFragmentManager();
    menuFragment = fm.findFragmentById(R.id.container);
    menuButton.setImageResource(R.drawable.ic_up_arrow);
    if(menuFragment == null){
        menuFragment = new MenuFragment();
        FragmentTransaction fragmentTransaction = 
getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.slide_left, 
R.anim.slide_right);
        fragmentTransaction.add(R.id.container,menuFragment);
        fragmentTransaction.commit();
    }

    isFragmentLoaded = true;
}
}

正如你所看到的,我正在使用loadFragment和hideFragment打开和关闭抽屉,所以我如何移动主要内容(有一个简单的textview,但考虑到它的很多东西)

android android-navigation-drawer
1个回答
0
投票

嘿,你可以使用这个我想你想要相同的输出

以下链接

© www.soinside.com 2019 - 2024. All rights reserved.