导航抽屉在 android 棒棒糖中不工作

Navigation drawer not working in android lollipop

我在我的项目 Android 设计库中使用了导航视图,但我遇到了问题。从 API 15 到 20 一切正常,就像假设的那样,但是当我尝试 API 21 (5.0) 及更高版本时,即使没有显示汉堡包按钮,它也无法正常工作。而且我找不到问题出在哪里。有没有人遇到过类似的问题?我正在使用 C# 在 xamarin 中工作。这是代码:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V4.Widget;
using Android.Support.V7.App;
using Android.Support.Design.Widget;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using Android.Content;
using Android.Views;
using Android.Webkit;


namespace Cazin.Net
{
[Activity (Label = "Cazin.Net", Icon = "@mipmap/icon",Theme="@style/MyTheme")]
public class MainActivity : AppCompatActivity
{
    static readonly string TAG = "X:" + typeof(MainActivity).Name;
    List<FeedItem> feedList;
    ProgressDialog progressDialog;
    ProgressBar progressBar;
    ListView feedListView;
    LinearLayout linearHome;
    LinearLayout linearSport;
    LinearLayout linearMagazin;


    DrawerLayout drawerLayout;
    TextView textView;
    TextView textViewTemp;
    TextView textViewTemperature;
    TextView textViewHumidty;
    ImageView imageView;
    ActionBarDrawerToggle drawerToggle;

    public static string MTitle = "com.example.cazin_net.Title";
    public static string mWriter = "com.example.cazin_net.Writer";
    public static string mPubdate = "com.example.cazin_net.PubDate";
    public static string Description = "com.example.cazin_net.Description";
    public static string Image = "com.example.cazin_net.Image";

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

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        feedListView = FindViewById<ListView> (Resource.Id.feedItemlistview);
        linearHome = FindViewById<LinearLayout> (Resource.Id.linearLayoutHome);
        linearSport = FindViewById<LinearLayout> (Resource.Id.linearSport);
        linearMagazin = FindViewById<LinearLayout> (Resource.Id.linearMagazin);
        textView = FindViewById<TextView> (Resource.Id.textWeather);
        imageView = FindViewById<ImageView> (Resource.Id.imageweather);
        textViewTemp = FindViewById<TextView> (Resource.Id.textWeatherTemp);
        textViewTemperature = FindViewById<TextView> (Resource.Id.textWeatherTemperature);
        textViewHumidty = FindViewById<TextView> (Resource.Id.textWeatherHumidity);
        this.progressDialog = new ProgressDialog (this);
        this.progressBar = FindViewById<ProgressBar> (Resource.Id.myProgrsesbar);
        progressDialog.SetMessage ("Dobavljam vijesti...");
        GetItemList ();
        linearHome.Pressed = true;


        //setting up drawer layout
        drawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
        var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar> (Resource.Id.toolbar);
        SetSupportActionBar (toolbar);
        SupportActionBar.SetDisplayHomeAsUpEnabled (true);
        SupportActionBar.SetDisplayShowHomeEnabled(true);
        drawerToggle = new ActionBarDrawerToggle (this, drawerLayout, toolbar,
            Resource.String.open_drawer, Resource.String.close_drawer);
        drawerLayout.SetDrawerListener (drawerToggle);
        drawerToggle.SyncState ();


        linearHome.Click += (object sender, EventArgs e) => 
        {
            var intent = new Intent(this, typeof(MainActivity));
            Finish();
            StartActivity(intent);
        };
        linearSport.Click += (object sender, EventArgs e) => 
        {
            var intent = new Intent(this, typeof(SportActivity));
            StartActivity(intent);
        };
        linearMagazin.Click += (object sender, EventArgs e) => 
        {
            var intent = new Intent(this, typeof(MagazinActivity));
            StartActivity(intent);
            linearMagazin.Pressed = true;
        };


        var navView = FindViewById<NavigationView> (Resource.Id.nav_view);
        navView.NavigationItemSelected += (object sender, NavigationView.NavigationItemSelectedEventArgs e) =>
        {

            switch (e.MenuItem.ItemId) {
            case(Resource.Id.nav_cazin):
                var intent = new Intent(this, typeof(CazinActivity));
                StartActivity(intent);
                break;
            case(Resource.Id.nav_vijesti):
                var vijesti = new Intent(this, typeof(VijestiActivity));
                StartActivity(vijesti);
                break;
            case(Resource.Id.nav_izdvojeno):
                var intentIzdvojeno = new Intent(this, typeof(IzdvojenoActivity));
                StartActivity(intentIzdvojeno);
                break;
            case(Resource.Id.nav_oglasi):
                var oglasi = new Intent(this, typeof(Oglasi));
                StartActivity(oglasi);
                break;
            case(Resource.Id.nav_diskusije):

                break;
            case(Resource.Id.nav_video):

                break;
            case(Resource.Id.nav_radio):

                break;
            default:
            break;
            }
            drawerLayout.CloseDrawer(navView);
        };
        var navViewRight = FindViewById<NavigationView> (Resource.Id.nav_viewright);
        navViewRight.NavigationItemSelected += (object sender, NavigationView.NavigationItemSelectedEventArgs e) => 
        {
            switch (e.MenuItem.ItemId)
            {
            case(Resource.Id.nav_facebook):
                var facebook = new Intent(this, typeof(FacebookActivity));
                StartActivity(facebook);
                break;
            case(Resource.Id.nav_twitter):
                var twitter = new Intent(this, typeof(TwitterActivity));
                StartActivity(twitter);
                break;
            case(Resource.Id.nav_googleplus):
                var googlePlus = new Intent(this, typeof(GooglePlus));
                StartActivity(googlePlus);
                break;
            default:
            break;
            }
        };
    }
    private void GetItemList()
    {
        this.progressDialog.Show ();
        Task<List<FeedItem>> task = Task.Factory.StartNew (() => {
            return FeedService.GetFeedItems ("http://www.cazin.net/vijesti/rss84234532133");
        });
        Task task2 = task.ContinueWith ((s) => {
            try {
                this.progressDialog.Dismiss ();
                this.feedList = s.Result;
                this.PopulateListView (this.feedList);
            } catch (AggregateException ex) {
                Toast.MakeText (this, ex.InnerException.Message, ToastLength.Short).Show ();
            }

        }, TaskScheduler.FromCurrentSynchronizationContext ());
    }
    void PopulateListView(List<FeedItem> list)
    {
        this.RunOnUiThread (() =>
        {
        var adapter = new FeedItemListAdapter(this, list);
        feedListView.Adapter = adapter;
                feedListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => 
                {
                    var feedItem = adapter[e.Position];
                    FeedItem itemFeed = new FeedItem()
                    {
                        Title = feedItem.Title,
                        PubDate = feedItem.PubDate,
                        Description = feedItem.Description,
                        Writer = feedItem.Writer,
                        Image = feedItem.Image

                    };
                    var newsDetail = new Intent(Application.Context, typeof(FeedDetails));
                    newsDetail.PutExtra(MTitle, itemFeed.Title);
                    newsDetail.PutExtra(mWriter, itemFeed.Writer);
                    newsDetail.PutExtra(mPubdate, itemFeed.PubDate.ToString());
                    newsDetail.PutExtra(Description, itemFeed.Description);
                    newsDetail.PutExtra(Image, itemFeed.Image);
                    StartActivity(newsDetail);
                };
        });
    }

    void GetWeather()
    {
        progressBar.Visibility = ViewStates.Visible;
        this.RunOnUiThread (async () => {
            RootObject myweather = await OpenWeatherMap.GetWeather(15.94, 44.97);
            textView.Text = "Lokacija: " + myweather.name.ToUpper();
            textViewTemperature.Text = "Temperatura: " + (int)myweather.main.temp + "°C";
            textViewTemp.Text = "Opis: " + myweather.weather[0].description;
            textViewHumidty.Text = "Vlažnost: " + myweather.main.humidity.ToString() + "%";
            if(myweather.weather[0].description == "clear sky")
                imageView.SetImageResource(Resource.Drawable.ic_sunny);
            else if(myweather.weather[0].description == "few clouds")
                imageView.SetImageResource(Resource.Drawable.ic_few_cluds);
            else if(myweather.weather[0].description == "overcast clouds" )
                imageView.SetImageResource(Resource.Drawable.ic_scattered_clouds);
            else if(myweather.weather[0].description == "scattered clouds" )
                imageView.SetImageResource(Resource.Drawable.ic_scattered_clouds);
            else if(myweather.weather[0].description == "broken clouds")
                imageView.SetImageResource(Resource.Drawable.ic_scattered_clouds);
            else if(myweather.weather[0].description == "shower rain")
                imageView.SetImageResource(Resource.Drawable.ic_shower_rain);
            else if(myweather.weather[0].description == "light rain")
                imageView.SetImageResource(Resource.Drawable.ic_shower_rain);
            else if(myweather.weather[0].description == "rain")
                imageView.SetImageResource(Resource.Drawable.ic_rain);
            else if(myweather.weather[0].description == "moderate rain")
                imageView.SetImageResource(Resource.Drawable.ic_shower_rain);
            else if(myweather.weather[0].description == "thunderstorm")
                imageView.SetImageResource(Resource.Drawable.ic_rain);
            else if(myweather.weather[0].description == "snow")
                imageView.SetImageResource(Resource.Drawable.ic_snow);
            else if(myweather.weather[0].description == "mist")
                imageView.SetImageResource(Resource.Drawable.ic_mist);
            progressBar.Visibility = ViewStates.Gone;
        });
    }
    public override bool OnCreateOptionsMenu (Android.Views.IMenu menu)
    {
        MenuInflater.Inflate (Resource.Menu.menu, menu);
        return base.OnCreateOptionsMenu (menu);
    }
    public override bool OnOptionsItemSelected (IMenuItem item)
    {
        var navViewRight = FindViewById<NavigationView> (Resource.Id.nav_viewright);
        switch (item.ItemId)
        {
        case Resource.Id.action_help:
            drawerLayout.OpenDrawer (navViewRight);
            GetWeather ();
            return true;
        default:
            break;
        }

        return base.OnOptionsItemSelected(item);
       }

   }
}

AXML代码:

   <?xml version="1.0" encoding="utf-8"?>
   <android.support.v4.widget.DrawerLayout       
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<ListView
    android:id="@+id/feedItemlistview"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:paddingTop="80dp"
    android:dividerHeight="10dp" />
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/mainLayout"
        android:layout_alignParentBottom="true"
        android:background="?attr/colorPrimary"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:weightSum="100">
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="25"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/linearLayoutHome"
            android:padding="0dp"
            android:weightSum="100"
            android:background="@drawable/Selector"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:id="@+id/imageView1"
                android:src="@drawable/ic_home_black_24dp"
                android:layout_weight="50"
                android:scaleType="fitCenter"
                android:adjustViewBounds="false" />
            <TextView
                android:text="Početna"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:textStyle="bold"
                android:id="@+id/textView1"
                android:gravity="center_horizontal" />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="25"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/linearMagazin"
            android:weightSum="100"
            android:padding="0dp"
            android:background="@drawable/Selector"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:id="@+id/imageView2"
                android:layout_weight="50"
                android:src="@drawable/ic_whatshot_black_24dp"
                android:scaleType="fitCenter"
                android:adjustViewBounds="false" />
            <TextView
                android:text="Magazin"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:textStyle="bold"
                android:id="@+id/textView2"
                android:gravity="center_horizontal" />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="25"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/linearSport"
            android:weightSum="100"
            android:padding="0dp"
            android:background="@drawable/Selector"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50"
                android:id="@+id/imageView3"
                android:src="@drawable/ic_directions_bike_black_24dp"
                android:scaleType="fitCenter"
                android:adjustViewBounds="false" />
            <TextView
                android:text="Sport"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:textStyle="bold"
                android:id="@+id/textView3"
                android:gravity="center_horizontal" />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="25"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/linearLayout4"
            android:weightSum="100"
            android:padding="0dp"
            android:clickable="true"
            android:background="@drawable/Selector"
            android:focusable="false"
            android:focusableInTouchMode="false">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:id="@+id/imageView4"
                android:layout_weight="50"
                android:src="@drawable/ic_photo_library_black_24dp"
                android:scaleType="fitCenter"
                android:adjustViewBounds="false" />
            <TextView
                android:text="Galerija"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:textStyle="bold"
                android:id="@+id/textView4"
                android:gravity="center_horizontal" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:id="@+id/nav_view"
    app:theme="@style/NavigationDrawerStyle"
    app:menu="@menu/navigationmenu"
    android:background="@android:color/white"
    app:headerLayout="@layout/header" />
<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:weightSum="100"
    android:layout_gravity="right"
    android:background="@android:color/white"
    android:id="@+id/nav_viewright"
    app:theme="@style/NavigationDrawerStyle"
    app:menu="@menu/navigationmenuright">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="60"
        android:weightSum="100"
        android:orientation="vertical"
        android:background="@android:color/holo_blue_bright">
        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/myProgrsesbar"
            android:progressDrawable="@drawable/circular_progress"
            android:layout_gravity="center" />
        <ImageView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:scaleType="fitCenter"
            android:weightSum="60"
            android:id="@+id/imageweather" />
        <TextView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:gravity="center"
            android:text="Weather"
            android:weightSum="40"
            android:textStyle="bold"
            android:fontFamily="sans-serif"
            android:textColor="@android:color/white"
            android:textSize="18dp"
            android:id="@+id/textWeather"
            android:layout_marginTop="30dp" />
        <TextView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:gravity="center"
            android:text="Weather"
            android:weightSum="40"
            android:textStyle="bold"
            android:fontFamily="sans-serif"
            android:textColor="@android:color/white"
            android:textSize="18dp"
            android:id="@+id/textWeatherTemperature"
            android:layout_marginTop="4dp" />
        <TextView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="Weather"
            android:gravity="center"
            android:weightSum="40"
            android:textStyle="bold"
            android:fontFamily="sans-serif"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            android:textSize="18dp"
            android:id="@+id/textWeatherTemp"
            android:layout_marginTop="4dp" />
        <TextView
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:gravity="center"
            android:text="Weather"
            android:textStyle="bold"
            android:weightSum="40"
            android:fontFamily="sans-serif"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            android:textSize="18dp"
            android:id="@+id/textWeatherHumidity"
            android:layout_marginTop="4dp" />
    </LinearLayout>
   </android.support.design.widget.NavigationView>
 </android.support.v4.widget.DrawerLayout>

如果有人将来遇到类似问题,这就是我的问题的解决方案。正如我猜测的那样,问题不在代码中。我通过组件添加了设计库,但这不是最新版本,您将在 android lollipop 或更高版本中遇到错误,因此通过 NuGet 包添加它或进行更新,您将获得最新版本。