如何从片段中打开活动菜单 [英] How To Open Menu In Activity From Fragment

查看:111
本文介绍了如何从片段中打开活动菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作添加了活动片段的应用程序.现在,我在片段部分添加了搜索栏,因此我想在单击图像时打开导航抽屉(处于活动状态)和操作菜单.

I am making app in which I have added fragment in activity. Now I have added search bar in fragment section, so I want to open navigation drawer(which is in activty) and action menu) when clicked on Image.

片段看起来像这样-此处的图片

片段XML-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:id="@+id/swiper"
        android:layout_height="match_parent">


        <LinearLayout
            android:layout_width="match_parent"

            android:layout_height="match_parent"
            android:orientation="vertical">
            <androidx.cardview.widget.CardView
                app:cardUseCompatPadding="true"
                android:layout_margin="8dp"
                app:cardCornerRadius="8dp"
                android:layout_width="match_parent"
                android:layout_height="60dp">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/imageView2"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_marginStart="8dp"
                        android:src="@drawable/ic_menu_black_24dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <EditText

                        android:id="@+id/edit_search"
                        android:layout_width="297dp"
                        android:layout_height="match_parent"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginBottom="8dp"
                        android:background="@android:color/transparent"
                        android:cursorVisible="false"
                        android:elevation="5dp"
                        android:fontFamily="@font/robolightfont"
                        android:gravity="center"
                        android:hint="Search"
                        android:inputType="text"
                        android:minWidth="100dp"
                        android:textColor="@color/white"
                        android:textColorHint="@color/white"
                        android:textStyle="normal"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintLeft_toLeftOf="@+id/imgmore"
                        app:layout_constraintLeft_toRightOf="@+id/imageView2"
                        app:layout_constraintStart_toEndOf="@+id/imageView2"
                        app:layout_constraintTop_toTopOf="parent" />

                    <ImageView
                        android:id="@+id/imgmore"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_marginTop="8dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginBottom="8dp"
                        android:src="@drawable/ic_more_vert_black_24dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                </androidx.constraintlayout.widget.ConstraintLayout>

            </androidx.cardview.widget.CardView>
          <!--  <EditText
                android:padding="10dp"
                android:background="@drawable/rounddesign"
                android:layout_margin="12dp"
                android:elevation="5dp"
                android:fontFamily="@font/robotolight"
                android:id="@+id/edit_search"
                android:textColorHint="@color/white"
                android:textColor="@color/white"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:gravity="center"
                android:textStyle="normal"
                android:cursorVisible="false"
                style="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
                android:hint="Search"
                android:visibility="visible"/>-->

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/list"
                android:background="@color/mainback"

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />

        </LinearLayout>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>



</RelativeLayout>

主要活动XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.NewMainActivity"
    tools:openDrawer="start">

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

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <ImageView
            android:id="@+id/logimage"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:visibility="gone"
            android:background="@drawable/ic_access_time_black_24dp"
            android:layout_margin="20dp"
            android:layout_gravity="center" />
        <TextView
            android:id="@+id/logtxt"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:fontFamily="@font/robolightfont"
            android:layout_marginTop="30dp"
            android:textSize="20dp"
            android:text="It Seems, No Notification Has Been Logged Yet !\n\nCome Back Once You Start Receiving Notification"/>

        <Button

            android:id="@+id/button_check"
            android:visibility="gone"
            android:gravity="center"
            android:textColor="@color/mainback"
            android:layout_margin="14dp"

            android:fontFamily="@font/robofont"
            android:background="@drawable/roundbtndesign"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Retry"/>
        <ImageView
            android:id="@+id/acimage"

            android:layout_width="80dp"
            android:layout_gravity="center"
            android:layout_margin="30dp"
            android:layout_height="80dp"
            android:visibility="gone"
            android:background="@drawable/ic_tag_faces_black_24dp"/>

        <TextView
            android:id="@+id/actitle"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Notification Access"
            android:layout_gravity="center"
            android:fontFamily="@font/fontmossemi"
            android:layout_margin="10dp"
            android:visibility="gone"
            android:gravity="center"/>
        <TextView
            android:id="@+id/acsubtext"

            android:visibility="gone"
            android:textSize="15sp"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="To Get Started, Enable Notification Access For Notification Log"
            android:layout_gravity="center"
            android:fontFamily="@font/robolightfont"
            android:layout_margin="10dp"
            android:gravity="center"/>

        <Button
            android:id="@+id/button_allow"
            android:visibility="gone"
            android:gravity="center"
            android:background="@drawable/roundbtndesign"
            android:textColor="@color/mainback"
            android:layout_margin="24dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Allow Permission"/>

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

        </FrameLayout>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"

        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer" />



</androidx.drawerlayout.widget.DrawerLayout>

现在如何从fragment.java代码中打开操作菜单和抽屉菜单.请帮忙

Now how can I open both action menu and drawer menu from fragment.java code. Please Help in this out

推荐答案

在您的活动中创建一个公共方法,这将打开导航抽屉并从您的片段中调用该方法.

Create a public method in your activity which will open the navigation drawer and call that method from your fragment.

活动代码:

public void openDrawer(){
    drawer.openDrawer(Gravity.LEFT);
}

在您的片段图片中,点击代码:

In your fragment image on click code:

imageView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        ((MainActivity)getActivity()).openDrawer();
    }
}

希望这对您有用!

这篇关于如何从片段中打开活动菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆