gpt4 book ai didi

android - DrawaerLayout.onDrawerOpened 从未被调用

转载 作者:行者123 更新时间:2023-11-30 01:46:22 24 4
gpt4 key购买 nike

我想更改 DrawerLayout 中的 TextView 。所以我用我自己的类扩展它。问题是当我调试这段代码时,永远不会调用 onDrawerOpened,特别是在打开抽屉时。

    import android.support.v4.widget.DrawerLayout;

public class MyDrawerLayout extends DrawerLayout implements DrawerLayout.DrawerListener {
//Context context;

public MyDrawerLayout(Context c) {
this(c, null);
}

public MyDrawerLayout(Context c, AttributeSet attrs) {
this(c, attrs, 0);
}

public MyDrawerLayout (Context c, AttributeSet attrs, int defStyle) {
super (c, attrs, defStyle);
setDrawerListener(this);
}

public void onDrawerOpened(View drawerView) {

LinearLayout root = (LinearLayout)(drawerView.findViewById(R.id.root));
TextView t = (TextView) (root.findViewById(R.id.textView));
t.setText(((MainActivity) MainActivity.main_activity).globalScenarioName);
Snackbar.make(((MainActivity)MainActivity.main_activity).container, "NAVVIEW Open", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}

public void onDrawerSlide (View drawerView, float slide) {

}

public void onDrawerClosed (View drawerView) {

}

public void onDrawerStateChanged (int newState) {}
}

activity_main.xml 布局看起来很标准:

<net.mycom.myproject.widget.MyDrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

</net.mycom.myproject.widget.MyDrawerLayout>

最佳答案

在你的构造函数中,你应该调用 setDrawerListener(this),将你的类注册为监听器,因为它正在实现 DrawerLayout.DrawerListener

public MyDrawerLayout(Context c) {
this(c, null);
}

public MyDrawerLayout(Context c, AttributeSet attrs) {
this(c, attrs, 0);
}

public MyDrawerLayout (Context c, AttributeSet attrs, int defStyle) {
super (c, attrs, defStyle);
setDrawerListener(this);
}

您不必保留对上下文的引用。 View 具有方法 getContext()

关于android - DrawaerLayout.onDrawerOpened 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33659915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com