使用 BottomSheetDialog 实现仿知乎分享控件
- 首先获取手机内所有支持分享的应用,得到 ResolveInfo 对象,利用反射获取应用图标等信息
- 然后用 RecyclerView 的 GridLayoutManager 网格布局展示,自己实现点击事件就可以了
下面来一张图看的更加直观些
首先获取手机内支持分享的这些应用封装为Util类:
接下来就是打开activity的按钮展示分享的应用列表了:/** * 获取手机内所有支持分享的应用列表 */ 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; }
这样到此就结束了,可以实现自定义仿知乎分享的需求,以下链接是完整代码,需要的可以点击下载 https://download.csdn.net/download/lou_liang/10691605private 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); } }); }