如今很多应用都提供向外分享信息的功能,在进行分享操作时,一般是从屏幕底部弹出所有具备分享功能的应用列表,再由用户进行选择
现在我就来模仿实现这种效果,不仅使分享控件从屏幕底部弹出,还要使分享控件能够上下拖动,这就需要使用到 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 分享功能的实现代码 一个Activity中,取出设备上安装的所有支持分享动作的Activity,在grid中显示。 实例代码: 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
本文向大家介绍基于KO+BootStrap+MVC实现的分页控件代码分享,包括了基于KO+BootStrap+MVC实现的分页控件代码分享的使用技巧和注意事项,需要的朋友参考一下 JS: HTML: 以上所述是小编给大家介绍的基于KO+BootStrap+MVC实现的分页控件代码分享,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!
本文向大家介绍Android实现截图和分享功能的代码,包括了Android实现截图和分享功能的代码的使用技巧和注意事项,需要的朋友参考一下 先给大家展示下效果图吧 直接上代码: xml的布局: activity的方法: 截取工具: 总结 以上所述是小编给大家介绍的Android实现截图和分享功能的代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊
本文向大家介绍Android 分享功能的实现,包括了Android 分享功能的实现的使用技巧和注意事项,需要的朋友参考一下 Android 分享功能的实现 Android程序里面的分享功能分为第三方程序分享,就是使用QQ空间,QQ微博,新浪微博,人人等第三方包进行分享; 还有就是用本地程序进行分享,如短信,UC浏览器,蓝牙等. 他们的区别是使用第三方包进行分享手机系统不用安装该类程序,而本地程序
本文向大家介绍ruby实现的文件自删除代码分享,包括了ruby实现的文件自删除代码分享的使用技巧和注意事项,需要的朋友参考一下 因为windows的文件删除机制和unix like的不一样,so不保证如下代码能在windows中使用,哪位童鞋帮我在windows中测试一下也好啊! 最后2句意思是你可以伪造代码或者干脆毁尸灭迹...
本文向大家介绍Android实现分享功能,包括了Android实现分享功能的使用技巧和注意事项,需要的朋友参考一下 Android应用中能很方便的完成这些功能,很多的应用中都有“分享”功能?如何分享呢?下面给大家说说看。 最近有人问到Android分享功能用那个比较好,使用Android自带的Intent来进行分享还是借助第三方呢,直接上代码: 一、使用Intent直接和第三方应用进行通信: 看代