xamarin 点击片段上的普通按钮打开导航抽屉

3j86kqsm  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(128)

Want to do

Open navigation drawer by clicking normal button on Fragment as same as clicking navigation icon.
Before clicked button Image
After clicked button Image

Relation of code and xml

MainActivity.cs - activity_main.xml
EventFragment.cs - event_fragment.xml
FilterNavi.cs - filter_navigation.xml (I am not sure that which is the best for FilterNavi.cs Fragment or Activity.)

Code****Error Message

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Code

EventFragment.cs

public class EventFragment : AndroidX.Fragment.App.FragmentNavigationView.IOnNavigationItemSelectedListener, TabLayout.IOnTabSelectedListener
{
    DrawerLayout drawer;
    Button btn_drawer ;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.Inflate(Resource.Layout.event_fragment, container, false);

        drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout2);

        btn_drawer = FindViewById<Button>(Resource.Id.btn_drawer);
        btn_drawer.Click += Btn_Drawer_Click;
    }

    private void Btn_Drawer_Click(object sender, EventArgs args)
    {
        drawer.OpenDrawer(GravityCompat.Start);
    }

filter_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
 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_layout2"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:fitsSystemWindows="true"
                                    tools:openDrawer="start">

  <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"
    android:fitsSystemWindows="true">

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

        <TextView
            android:id="@+id/test"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:text="test"
            android:textSize="12dp"
            android:gravity="center">
        </TextView>
    </LinearLayout>

  </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

What I tried

I tried getView() and getActivity() but error told me that they don't exist in the current context .

m_drawer = (DrawerLayout) getActivity().findViewById(Resource.Id.drawer_layout2);

I also tried to get m_drawer and update view.

m_drawer was not null but navigation drawer didn't open.

View viewFilter = inflater.Inflate(Resource.Layout.filter_navigation, container, false);
        m_drawer = viewFilter.FindViewById<DrawerLayout>(Resource.Id.drawer_layout2);
        View view = inflater.Inflate(Resource.Layout.event_fragment, container, false);
6vl6ewon

6vl6ewon1#

我无法通过单击片段上普通按钮来打开导航抽屉但是我找到了替代方法
我用的是普通的Activity,它的右半边有半透明的背景。你可以通过layout_weight来改变它的宽度。如果我不想让它变成半透明的,我就设置白色背景。

样式.xml

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

活动.cs

namespace SMP_Android
{
    // add Theme
    [Activity(Label = "FilterActivity", Theme = "@style/Theme.AppCompat.Translucent")]
public class FilterActivity : AppCompatActivity // select AppCompatActivity
    {
    }
}

活动.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="aa000000"> // add background

相关问题