0

我正试图在Navigation Drawer中实现Share按钮。我不知道我们如何实现这一点。任何帮助表示赞赏。这是我走了多远。Android导航抽屉中的Share按钮

菜单项XML

 <?xml version="1.0" encoding="utf-8" ?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android"> 
     <group android:checkableBehavior="single" 
      android:id="@+id/app"> 
     <item 
      android:id="@+id/nav_fibonacci" 
      android:icon="@drawable/ic_cards" 
      android:title="@string/FibonacciSeriesTabTitle" /> 
     <item 
      android:id="@+id/nav_tshirt" 
      android:icon="@drawable/ic_tshirt" 
      android:title="@string/TShirtSizesTabTitle" /> 
     <item 
      android:id="@+id/nav_standard" 
      android:icon="@drawable/ic_cards" 
      android:title="@string/StandardSeriesTabTitle" /> 
     <item 
      android:id="@+id/nav_settings" 
      android:icon="@drawable/ic_settings" 
      android:title="@string/SettingsTitle" /> 
     </group> 
     <group android:checkableBehavior="single" 
      android:id="@+id/contact"> 

     <item 
      android:id="@+id/nav_share" 
      android:icon="@drawable/ic_share" 
      android:title="@string/ShareTitle" 
      android:actionProviderClass="android.widget.ShareActionProvider"/> 

     </group> 
    </menu> 

Navigation Drawer 

    <?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" 
     xmlns:ads="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/drawer_layout" 
     android:fitsSystemWindows="true"> 
     <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.Light" /> 
      <FrameLayout 
       android:id="@+id/content_frame" 
       android:layout_below="@id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
      <com.google.android.gms.ads.AdView 
        android:id="@+id/adView" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        ads:adSize="SMART_BANNER" 
        ads:adUnitId="ca-app-pub-8875162019282514/8222601087" 
        android:gravity="bottom" /> 

     </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:menu="@menu/navmenu" 
      app:headerLayout="@layout/header" /> 

    </android.support.v4.widget.DrawerLayout> 

Navigation Activity 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 

using Android.Support.V7.App; 
using Android.Support.Design.Widget; 
using Android.Support.V4.Widget; 
using Android.Support.V7.Widget; 
using TestNavigationAndroid.Fragments; 
using TestNavigationAndroid.Ads; 
using Android.Gms.Ads; 
using Android.Support.V4.View; 

namespace TestNavigationAndroid 
{ 
    [Activity(MainLauncher = true, Theme = "@style/MyTheme", Icon = "@drawable/icon")] 
    public class NavigationDrawerActivity : AppCompatActivity 
    { 
     DrawerLayout drawerLayout; 


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

      // Create UI 
      SetContentView(Resource.Layout.navigation); 
      drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout); 

      // Init toolbar 
      var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar); 
      SetSupportActionBar(toolbar); 

      // Attach item selected handler to navigation view 
      var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view); 
      navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected; 

      // Create ActionBarDrawerToggle button and add it to the toolbar 
      var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer); 
      drawerLayout.SetDrawerListener(drawerToggle); 
      drawerToggle.SyncState(); 

      //if first time you will want to go ahead and click first item. 
      if (bundle == null) 
      { 
       SwitchFragments(Resource.Id.nav_fibonacci); 
      } 
      AdView _bannerad = FindViewById<AdView>(Resource.Id.adView); 
      //_bannerad = AdMobWrapper.ConstructStandardBanner(this, AdSize.SmartBanner, "ca-app-pub-8875162019282514/8222601087"); 
      //dMobWrapper.CustomBuild(_bannerad); 
      _bannerad.LoadAd(new AdRequest.Builder().Build()); 
      // drawerLayout.AddView(_bannerad); 
      // ShowShareActivity(); 
     } 

     void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e) 
     { 
      SwitchFragments(e.MenuItem.ItemId); 
      //if (e.MenuItem.ItemId.Equals(Resource.Id.nav_share)) 
      //{ 
      // ShowShareActivity(e.MenuItem); 
      //} 
      //else 
      //{ 

      //} 
     } 

     private void SwitchFragments(int ItemId) 
     { 
      Android.Support.V4.App.Fragment fragment = null; 
      switch (ItemId) 
      { 
       case (Resource.Id.nav_fibonacci): 
        fragment = FibonacciSeries.NewInstance(); 
        // React on 'Home' selection 
        break; 
       case (Resource.Id.nav_tshirt): 
        // React on 'Messages' selection 
        fragment = TshirtSize.NewInstance(); 
        break; 
       case (Resource.Id.nav_standard): 
        // React on 'Friends' selection 
        fragment = StandardFragment.NewInstance(); 
        break; 
       case (Resource.Id.nav_share): 
        // React on 'Friends' selection 
        ShowShareActivity(); 
        break; 

      } 
      SupportFragmentManager.BeginTransaction() 
       .Replace(Resource.Id.content_frame, fragment) 
       .Commit(); 
      // Close drawer 
      drawerLayout.CloseDrawers(); 
     } 

     private void ShowShareActivity() 
     { 
      Intent sharingIntent = new Intent(Intent.ActionSend); 
      sharingIntent.SetType("text/plain"); 
      String shareBody = "Here is the share content body"; 
      sharingIntent.PutExtra(Intent.ExtraSubject, "Subject Here"); 
      sharingIntent.PutExtra(Intent.ExtraText, shareBody); 
      var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view); 
      var menu = navigationView.Menu; 
      IMenuItem item = menu.FindItem(Resource.Id.nav_share); 

      // Fetch and store ShareActionProvider 
      ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.GetActionProvider(item); 

      mShareActionProvider.SetShareIntent(sharingIntent); 
     } 
    } 
} 

我使用Xamarin.Android。我试图实现的是当有人点击共享按钮时,我的世界喜欢显示一个包含所有可用应用程序的片段来共享这些信息。我尝试根据Google教程设置ShareActionProvider。任何帮助在这里表示赞赏。

回答

1

最后我自己找到了答案。这是我实施的方式。我创建了一个新的Fragment和onFragement创建我用共享意图移动了开始活动。我学到的是ShareActionProvider不能在导航抽屉中。它仅用于ActionBar。

Intent sharingIntent = new Intent(Intent.ActionSend); 
      sharingIntent.SetType("text/plain"); 
      StringBuilder sb = new StringBuilder(); 
      sb.Append("Hi, I am using the Scrum Planning Poker. I like this and I want you to check it out."); 
      sb.Append("https://play.google.com/store/apps/details?id=" + this.Context.PackageName); 
      sharingIntent.AddFlags(ActivityFlags.ClearWhenTaskReset); 
      sharingIntent.PutExtra(Intent.ExtraSubject, "Test"); 
      sharingIntent.PutExtra(Intent.ExtraText, sb.ToString()); 
      StartActivity(Intent.CreateChooser(sharingIntent, "Test")); 

别人参考

这是如何,如果有人想构建自己的片段与是我的原意包信息获取所有的包信息。

private IList<ResolveInfo> GetPckages() 
     { 
      List<string> package = new List<string>(); 

      Intent sendIntent = new Intent(); 
      sendIntent.SetAction(Intent.ActionSend); 
      sendIntent.PutExtra(Intent.ExtraText, "test"); 
      sendIntent.SetType("text/plain"); 
      return this.Activity.PackageManager.QueryIntentActivities(sendIntent, 0); 

     } 

您现在可以使用该列表并根据您想要的用户体验将其绑定到网格视图/列表视图。

希望这可以帮助别人。