【问题标题】:Share button in Android Navigation DrawerAndroid 导航抽屉中的分享按钮
【发布时间】:2016-06-27 02:01:16
【问题描述】:

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

菜单项 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。任何帮助在这里表示赞赏。

【问题讨论】:

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


    【解决方案1】:

    最后我自己找到了答案。这就是我实施的方式。我创建了一个新的 Fragment 和 onFragement Create 我以共享意图移动了开始活动。我学到的是 ShareActionProvider 不能在 Navigation Drawer 中。它仅适用于 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);
    
            }
    

    您现在可以使用列表并将其绑定到网格视图/列表视图,具体取决于您想要的用户体验。

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      相关资源
      最近更新 更多