Android 分享控件的实现代码

如今很多应用都提供向外分享信息的功能,在进行分享操作时,一般是从屏幕底部弹出所有具备分享功能的应用列表,再由用户进行选择

现在我就来模仿实现这种效果,不仅使分享控件从屏幕底部弹出,还要使分享控件能够上下拖动,这就需要使用到 design 包提供的 BottomSheetDialog 控件了

首先,声明 BottomSheetDialog 对话框的主布局 dialog_bottom_sheet.xml

当中,RecyclerView 用于展示提供分享功能的应用列表

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="14dp"
  android:orientation="vertical">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="14dp"
    android:text="进一步的说明 -> leavesC"
    android:textAppearance="@style/TextAppearance.AppCompat"
    android:textSize="16sp"/>

  <View
    android:layout_width="match_parent"
    android:layout_height="0.6dp"
    android:background="#ddd"/>

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="12dp"
    android:paddingStart="14dp"
    android:text="分享文本信息到..."
    android:textAppearance="@style/TextAppearance.AppCompat"
    android:textSize="14sp"/>

  <android.support.v7.widget.RecyclerView
    android:id="@+id/rv_appList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

RecyclerView 单个子项使用的布局 item_app.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
  android:paddingBottom="8dp"
  android:paddingLeft="16dp"
  android:paddingRight="16dp"
  android:paddingTop="8dp">

  <ImageView
    android:id="@+id/iv_appIcon"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:scaleType="centerCrop"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:src="@mipmap/ic_launcher"/>

  <TextView
    android:id="@+id/tv_appName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    android:ellipsize="end"
    android:maxLength="6"
    android:textSize="12sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/iv_appIcon"
    tools:text="之乎者也"/>

</android.support.constraint.ConstraintLayout>

RecyclerView 配套使用的 Adapter : AppShareAdapter

/**
 * 作者:叶应是叶
 * 时间:2018/3/28 20:30
 * 描述:https://github.com/leavesC
 */
public class AppShareAdapter extends RecyclerView.Adapter<AppShareAdapter.ViewHolder> {
  public interface OnClickListener {
    void onClick(int position);
  }
  public interface OnLongClickListener {
    void onLongClick(int position);
  }

  private List<App> appList;
  private LayoutInflater layoutInflater;
  private OnClickListener clickListener;
  private OnLongClickListener longClickListener;
  AppShareAdapter(Context context, List<App> appList) {
    this.layoutInflater = LayoutInflater.from(context);
    this.appList = appList;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.item_app, parent, false);
    return new AppShareAdapter.ViewHolder(view);
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.iv_appIcon.setBackground(appList.get(position).getAppIcon());
    holder.tv_appName.setText(appList.get(position).getAppName());
    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (clickListener != null) {
          clickListener.onClick(holder.getAdapterPosition());
        }
      }
    });
    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
        if (longClickListener != null) {
          longClickListener.onLongClick(holder.getAdapterPosition());
        }
        return true;
      }
    });
  }

  @Override
  public int getItemCount() {
    return appList.size();
  }

  void setClickListener(OnClickListener clickListener) {
    this.clickListener = clickListener;
  }

  void setLongClickListener(OnLongClickListener longClickListener) {
    this.longClickListener = longClickListener;
  }

  class ViewHolder extends RecyclerView.ViewHolder {
    private ImageView iv_appIcon;
    private TextView tv_appName;
    ViewHolder(View itemView) {
      super(itemView);
      iv_appIcon = itemView.findViewById(R.id.iv_appIcon);
      tv_appName = itemView.findViewById(R.id.tv_appName);
    }
  }
}

利用 Intent 找出所有提供分享功能的应用,初始化 BottomSheetDialog 即可

/**
 * 作者:叶应是叶
 * 时间:2018/3/28 20:30
 * 描述:https://github.com/leavesC
 */
public class MainActivity extends AppCompatActivity {
  private List<App> appList;
  private BottomSheetDialog bottomSheetDialog;
  private Intent shareIntent;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, "https://github.com/leavesC");
  }
  public void originalShare(View view) {
    Intent intent = Intent.createChooser(shareIntent, "分享一段文本信息");
    if (shareIntent.resolveActivity(getPackageManager()) != null) {
      startActivity(intent);
    }
  }
  public void customizedShare(View view) {
    if (bottomSheetDialog == null) {
      bottomSheetDialog = new BottomSheetDialog(this);
      bottomSheetDialog.setContentView(R.layout.dialog_bottom_sheet);
      initBottomDialog();
    }
    bottomSheetDialog.show();
  }

  private void initBottomDialog() {
    appList = getShareAppList(this, shareIntent);
    AppShareAdapter appShareAdapter = new AppShareAdapter(this, appList);
    appShareAdapter.setClickListener(new AppShareAdapter.OnClickListener() {
      @Override
      public void onClick(int position) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setComponent(new ComponentName(appList.get(position).getPackageName(), appList.get(position).getMainClassName()));
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "https://github.com/leavesC");
        startActivity(intent);
      }
    });
    appShareAdapter.setLongClickListener(new AppShareAdapter.OnLongClickListener() {
      @Override
      public void onLongClick(int position) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + appList.get(position).getPackageName()));
        startActivity(intent);
      }
    });
    RecyclerView rv_appList = bottomSheetDialog.findViewById(R.id.rv_appList);
    if (rv_appList != null) {
      rv_appList.setLayoutManager(new GridLayoutManager(this, 4));
      rv_appList.setAdapter(appShareAdapter);
    }
  }

  public static List<App> getShareAppList(Context context, Intent intent) {
    List<App> appList = new ArrayList<>();
    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
    if (resolveInfoList == null || resolveInfoList.size() == 0) {
      return null;
    } else {
      for (ResolveInfo resolveInfo : resolveInfoList) {
        App appInfo = new App(resolveInfo.loadLabel(packageManager).toString(), resolveInfo.activityInfo.packageName,
            resolveInfo.activityInfo.name, resolveInfo.loadIcon(packageManager));
        appList.add(appInfo);
      }
    }
    return appList;
  }
}

这里提供上述示例代码: ShareDialog_Demo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android自带API实现分享功能

    前言 在做项目的过程中需要实现文字和图片的分享,有两种方式: 1. 使用android sdk中自带的Intent.ACTION_SEND实现分享. 2. 使用shareSDK.友盟等第三方的服务. 鉴于使用的方便,此次只介绍使用Android sdk中自带的方式来实现分享的功能. 分享文字 /** * 分享文字内容 * * @param dlgTitle * 分享对话框标题 * @param subject * 主题 * @param content * 分享内容(文字) */ private

  • Android仿QQ空间动态界面分享功能

    先看看效果: 用极少的代码实现了 动态详情 及 二级评论 的 数据获取与处理 和 UI显示与交互,并且高解耦.高复用.高灵活. 动态列表界面MomentListFragment支持 下拉刷新与上拉加载 和 模糊搜索,反复快速滑动仍然非常流畅. 缓存机制使得数据可在启动界面后瞬间加载完成. 动态详情界面MomentActivity支持 (取消)点赞.(删除)评论.点击姓名跳到个人详情 等. 只有1张图片时图片放大显示,超过1张则按九宫格显示. 用到的CommentContainerView和Mom

  • android实现多图文分享朋友圈功能

    很多安卓程序员都在寻找如何调用系统分享可以实现朋友圈多图加文字分享的功能,小编经过测试入坑后,为你整理以下内容: private void shareMultiplePictureToTimeLine(File... files) { Intent intent = new Intent(); ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTim

  • Android 分享功能的实现

     Android 分享功能的实现 Android程序里面的分享功能分为第三方程序分享,就是使用QQ空间,QQ微博,新浪微博,人人等第三方包进行分享; 还有就是用本地程序进行分享,如短信,UC浏览器,蓝牙等. 他们的区别是使用第三方包进行分享手机系统不用安装该类程序,而本地程序分享就需要. 这里主要讲的是使用本地程序进行分享. 效果如下图所示: 实现代码如下所示; Intent email = new Intent(android.content.Intent.ACTION_SEND); emai

  • Android系统自带分享图片功能

    简介 记录一个利用系统分享功能进行图片分享的工具类(代码是用Kotlin写的,都是比较简单的语法,部分可能需要自定义的地方都已经标出).调用方式比较简单: Util.startShareImage(this) //this为当前的Activity实例 权限 记得添加文件操作权限, 另外需要注意6.0版本以上的权限管理 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <

  • Android 分享控件的实现代码

    如今很多应用都提供向外分享信息的功能,在进行分享操作时,一般是从屏幕底部弹出所有具备分享功能的应用列表,再由用户进行选择 现在我就来模仿实现这种效果,不仅使分享控件从屏幕底部弹出,还要使分享控件能够上下拖动,这就需要使用到 design 包提供的 BottomSheetDialog 控件了 首先,声明 BottomSheetDialog 对话框的主布局 dialog_bottom_sheet.xml 当中,RecyclerView 用于展示提供分享功能的应用列表 <?xml version=&quo

  • Android Shape控件美化实现代码

    如果你对Android系统自带的UI控件感觉不够满意,可以尝试下自定义控件,我们就以Button为例,很早以前Android123就写到过Android Button按钮控件美化方法里面提到了xml的selector构造.当然除了使用drawable这样的图片外今天Android开发网谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape.gradient.stroke.corners等.  我们就以目前系统的Button的selector为例说下: <s

  • Android日历控件的实现方法

    本文实例为大家分享了Android日历控件的实现代码,供大家参考,具体内容如下 1.效果图: 2.弹窗Dialog:SelectDateDialog: public class SelectDateDialog { private static final String TAG = "SelectDateDialog"; private Dialog dialog; private TextView dateText; private int selectYear, selectMon

  • Android实现字母导航控件的示例代码

    目录 自定义属性 Measure测量 坐标计算 绘制 Touch事件处理 数据组装 显示效果 今天分享一个以前实现的通讯录字母导航控件,下面自定义一个类似通讯录的字母导航 View,可以知道需要自定义的几个要素,如绘制字母指示器.绘制文字.触摸监听.坐标计算等,自定义完成之后能够达到的功能如下: 完成列表数据与字母之间的相互联动; 支持布局文件属性配置; 在布局文件中能够配置相关属性,如字母颜色.字母字体大小.字母指示器颜色等属性. 主要内容如下: 自定义属性 Measure测量 坐标计算 绘制

  • Android 倒计时控件 CountDownView的实例代码详解

    一个精简可自定义的倒计时控件,使用 Canvas.drawArc() 绘制.实现了应用开屏页的圆环扫过的进度条效果. 代码见https://github.com/hanjx-dut/CountDownView 使用 allprojects { repositories { ... maven { url 'https://jitpack.io' } } } dependencies { implementation 'com.github.hanjx-dut:CountDownView:1.1'

  • Android设置控件阴影的三种方法

    本文实例为大家分享了Android设置控件阴影的方法,供大家参考,具体内容如下 第一种方式:elevation View的大小位置都是通过x,y确定的,而现在有了z轴的概念,而这个z值就是View的高度(elevation),而高度决定了阴影(shadow)的大小. View Elevation(视图高度) View的z值由两部分组成,elevation和translationZ(它们都是Android L新引入的属性). eleavation是静态的成员,translationZ是用来做动画.

  • android倒计时控件示例

    本文为大家分享了android倒计时控件,供大家参考,具体代码如下 /* * Copyright (C) 2012 The * Project * All right reserved. * Version 1.00 2012-2-11 * Author veally@foxmail.com */ package com.ly.sxh.view; import android.content.Context; import android.database.ContentObserver; im

  • Android常见控件使用详解

    本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下 1.TextView 主要用于界面上显示一段文本信息 2.Button 用于和用户交互的一个按钮控件 //为Button点击事件注册一个监听器 public class Click extends Activity{ private Button button; @Override ptotected void onCreate(Bundle savedInstanceState) { super.onCreat

  • Android UI控件之ImageSwitcher实现图片切换效果

    本文实例为大家分享了geSwitcher实现图片切换效果的具体代码,供大家参考,具体内容如下 从该名字就可以看出来,ImageSwitcher是一个图片切换控件,可以在一系列的图片中,逐张的显示特定的图片,利用该控件可以实现图片浏览器中的上一张,下一张的功能.其使用方法也较 为简单,不过需要注意的是ImageSwitcher在使用的时候需要一个ViewFactory,用来区分显示图片的容器和他的父窗口. 具体的用法直接看实例,照例,先上效果图 看看下一张的效果: 布局文件就不多谈了直接看Main

  • Android UI控件之ListView实现圆角效果

    今天在Android群里面有人再求圆角ListView的实现方式,正好自己以前实现过.因此就共享了现在将其实现方式写在博客中共他人学习.给出实现方式之前顺带加点自己的想法,感觉上android中方形的ListView还是太"硬性",没有圆角的有亲和力.连Apple也为了"圆角"这个设计去申请专利. 看来圆角确实比较适合现在人们的喜好吧. 照老规矩先上两张效果图吧: 第一张: 第二张: 该方式主要就是需要重新去实现自己的ListView代码如下: package co

随机推荐