Android仿知乎分享

使用 BottomSheetDialog 实现仿知乎分享控件

  1. 首先获取手机内所有支持分享的应用,得到 ResolveInfo 对象,利用反射获取应用图标等信息
  2. 然后用 RecyclerView 的 GridLayoutManager 网格布局展示,自己实现点击事件就可以了

    下面来一张图看的更加直观些

  3. 首先获取手机内支持分享的这些应用封装为Util类:
    /**
     * 获取手机内所有支持分享的应用列表
     */
    public static ArrayList<AppInfo> getShareAppList(Context context, Intent intent) {
        ArrayList<AppInfo> shareAppInfos = new ArrayList<>();
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> resolveInfos = getShareApps(context, intent);
        if (null == resolveInfos) {
            return null;
        } else {
            for (ResolveInfo resolveInfo : resolveInfos) {
                AppInfo appInfo = new AppInfo();
                appInfo.setPkgName(resolveInfo.activityInfo.packageName);
                appInfo.setLaunchClassName(resolveInfo.activityInfo.name);
                appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString());
                appInfo.setAppIcon(resolveInfo.loadIcon(packageManager));
                shareAppInfos.add(appInfo);
            }
        }
        return shareAppInfos;
    }
    /**
     * 查询手机内所有支持分享的应用列表
     */
    public static List<ResolveInfo> getShareApps(Context context, Intent intent) {
        List<ResolveInfo> resolveInfoList;
        PackageManager pm = context.getPackageManager();
        resolveInfoList = pm.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
        // 按名称排序
        ResolveInfo.DisplayNameComparator comparator = new ResolveInfo.DisplayNameComparator(pm);
        Collections.sort(resolveInfoList, comparator);
        return resolveInfoList;
    }
    接下来就是打开activity的按钮展示分享的应用列表了:
    private ArrayList<AppInfo> mAppinfoList;
    private AppInfoAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private Intent shareIntent;
    {
        shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "www.baidu.com");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    @Override
    protected void onStart() {
        super.onStart();
        mAppinfoList = Utils.getShareAppList(this, shareIntent);
    }
    private void initView() {
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              showDialog();
            }
        });
    }
    private void showDialog() {
        BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(this);
        mBottomSheetDialog.setContentView(R.layout.dialog_bottom_sheet);
        initBottomDialog(mBottomSheetDialog);
        mBottomSheetDialog.show();
    }
    
    private void initBottomDialog(final Dialog dialog) {
        mAdapter = new AppInfoAdapter(this, mAppinfoList);
        mRecyclerView = dialog.findViewById(R.id.list_view);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickLitener(new AppInfoAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(AppInfo appInfo, View view, int position) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setComponent(new ComponentName(appInfo.getPkgName(), appInfo.getLaunchClassName()));
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "www.baidu.com");
                startActivity(intent);
            }
            @Override
            public void onItemLongClick(AppInfo appInfo, View view, int position) {
                // 打开应用信息界面
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + appInfo.getPkgName()));
                startActivity(intent);
            }
        });
    }
    这样到此就结束了,可以实现自定义仿知乎分享的需求,以下链接是完整代码,需要的可以点击下载  https://download.csdn.net/download/lou_liang/10691605
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值