android - Android 抽屉导航中的共享按钮

标签 android android-fragments xamarin.android navigation-drawer

我正在尝试在抽屉导航中实现共享按钮。我不确定我们如何实现这一点。任何帮助表示赞赏。这就是我已经走了多远。

菜单项 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。我想要实现的是,当有人单击共享按钮时,我希望显示一个包含所有可用应用程序的 fragment 来共享信息。我尝试根据 Google 教程设置 ShareActionProvider。感谢此处的任何帮助。

最佳答案

终于我自己找到了答案。我就是这样实现的。我创建了一个新的 Fragment 并在 onFragement Create 上移动了具有共享 Intent 的启动 Activity 。我了解到 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"));

供其他人引用

如果有人想用包信息构建自己的 fragment ,这就是我的初衷,这是如何获取所有包信息的方法。

 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);

        }

您现在可以使用该列表并将其绑定(bind)到 GridView / ListView ,具体取决于您想要的用户体验。

希望这对某人有帮助。

关于android - Android 抽屉导航中的共享按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38044929/

相关文章:

android - 如何在android中禁用复制粘贴

android - 偶尔从 (View).findViewByID 获取 null

android - 在 Android 4.1 上改造 2 TLS 1.1

android - 延迟确认按钮动画

android - 无法在 Activity 中使用 viewpager 创建 fragment 。

android - 无法将 fragment 中的 Google Maps API v2 膨胀两次

xaml - Xamarin forms 选定选项卡的 Shell 自定义图标

安卓动态分屏

android - 从 FragmentStatePagerAdapter 获取资源

c# - Xamarin.Android C# layout_weight 错误 : Must specify a unit,,例如 "px"(智能感知?)