java - 如何从片段中打开活动中的菜单
问题描述
我正在制作应用程序,其中我在活动中添加了片段。现在我在片段部分添加了搜索栏,所以我想在单击图像时打开导航抽屉(处于活动状态)和操作菜单)。
片段看起来像这样 -图片在这里
片段 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 代码中打开操作菜单和抽屉菜单。请帮忙解决这个问题
解决方案
在您的活动中创建一个公共方法,该方法将打开导航抽屉并从您的片段中调用该方法。
活动代码:
public void openDrawer(){
drawer.openDrawer(Gravity.LEFT);
}
在点击代码的片段图像中:
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).openDrawer();
}
}
希望这对你有用!
推荐阅读
- python - 检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (16, 1) 的数组
- typescript - 为什么通过类型签名设置 void 返回类型会被忽略?
- google-apps-script - 表单下拉列表的动态编辑创建额外的列
- java - docker中没有连接,端口暴露和发布
- c# - 在 C# 中正确组织项目之间的依赖关系
- composer-php - 无法安装 Composer,安装出错已经尝试了几种解决方案
- firebase - Actions-on-Google 无法在 Dialogflow SDK 上获取 UPDATES_USER_ID
- c# - nuget 是否创建单独的包?
- sublimetext3 - 无法更改用户键绑定以在不同浏览器中打开 sublime?
- sql - 如何在openquery中获取列的前9位?