如今很多应用都提供向外分享信息的功能,在进行分享 *** 作时,一般是从屏幕底部d出所有具备分享功能的应用列表,再由用户进行选择
现在我就来模仿实现这种效果,不仅使分享控件从屏幕底部d出,还要使分享控件能够上下拖动,这就需要使用到 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:从零开始学android实现计算器功能示例分享(计算器源码)android文件上传示例分享(android图片上传)Android开发中实现用户注册和登陆的代码实例分享Android 第三方应用接入微信平台研究情况分享(二)android 网络编程之网络通信几种方式实例分享Android Activity切换(跳转)时出现黑屏的解决方法 分享Android应用中加入微信分享简单方法Android游戏源码分享之2048Android 第三方应用接入微信平台研究情况分享(一) 总结以上是内存溢出为你收集整理的Android 分享控件的实现代码全部内容,希望文章能够帮你解决Android 分享控件的实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)