美文网首页自定义控件
android BottomSheetDialog优雅实现分享菜

android BottomSheetDialog优雅实现分享菜

作者: 大大大寒 | 来源:发表于2017-07-06 14:50 被阅读449次

    BottomSheetDialog是23.2中加的一个新的控件,继承AppCompatDialog类
    官方描述Base class for Dialogs styled as a bottom sheet.
    使用起来 很简单,

     BottomSheetDialog dialog = new BottomSheetDialog(this);
     dialog.setContentView(view);
    dialog.show();
    

    创建一个BottomSheetDialog 然后setContentView,剩下的就是对view进行操作
    我们就是实现一个分享菜单先上效果图

    Gif_20170706143933.gif

    实现

       BottomSheetDialog dialog = new BottomSheetDialog(MainActivity.this);
                    View view = View.inflate(MainActivity.this, R.layout.bottom_dialog, null);
                    dialog.setContentView(view);
                    recyclerView = (RecyclerView) dialog.findViewById(R.id.recycler);
                    recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this, 3));
                    recyclerView.setHasFixedSize(true);
                    recyclerView.setAdapter(new Adapter());
                    dialog.show();
    
    

    adapter

     public class Adapter extends RecyclerView.Adapter<MainActivity.ViewHolde> {
            Adapter() {
                list = getShareAppList();
                if (list == null) {
                    return;
                }
            }
    
            @Override
            public ViewHolde onCreateViewHolder(ViewGroup parent, int viewType) {
                return new ViewHolde(getLayoutInflater().inflate(R.layout.recycler_item, null));
            }
    
            @Override
            public void onBindViewHolder(ViewHolde holder, final int position) {
                holder.appTextView.setText(list.get(position).appName);
                holder.iconImageView.setImageDrawable(list.get(position).icon);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
                        Intent shareIntent = new Intent(Intent.ACTION_SEND);
                        AppBean appBean = list.get(position);
                        shareIntent.setComponent(new ComponentName(appBean.pkgName, appBean.appLauncherClassName));
                        shareIntent.setType("text/plain");
                        shareIntent.putExtra(Intent.EXTRA_TEXT, "分享内容");
                        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(shareIntent);
                    }
                });
            }
    
            @Override
            public int getItemCount() {
                return list == null ? 0 : list.size();
            }
        }
    

    获取应用分享代码

    
     public List<ResolveInfo> getShareApps(Context context) {
            List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
            Intent intent = new Intent(Intent.ACTION_SEND, null);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setType("text/plain");
    //      intent.setType("*/*");
            PackageManager pManager = context.getPackageManager();
            mApps = pManager.queryIntentActivities(intent,
                    PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            return mApps;
        }
    
        private List<AppBean> getShareAppList() {
            List<AppBean> shareAppInfos = new ArrayList<AppBean>();
            PackageManager packageManager = getPackageManager();
            List<ResolveInfo> resolveInfos = getShareApps(MainActivity.this);
            if (null == resolveInfos) {
                return null;
            } else {
                for (ResolveInfo resolveInfo : resolveInfos) {
                    AppBean appBean = new AppBean();
                    appBean.pkgName = (resolveInfo.activityInfo.packageName);
    //              showLog_I(TAG, "pkg>" + resolveInfo.activityInfo.packageName + ";name>" + resolveInfo.activityInfo.name);
                    appBean.appLauncherClassName = (resolveInfo.activityInfo.name);
                    appBean.appName = (resolveInfo.loadLabel(packageManager).toString());
                    appBean.icon = (resolveInfo.loadIcon(packageManager));
                    shareAppInfos.add(appBean);
                }
            }
            return shareAppInfos;
        }
    

    xml和AppBean 就不放了
    github完整代码
    https://github.com/hanjole/bottomsheetdialogshare
    错误请指出,有用点个赞 tks

    相关文章

      网友评论

        本文标题:android BottomSheetDialog优雅实现分享菜

        本文链接:https://www.haomeiwen.com/subject/lnphhxtx.html