Android中怎么實(shí)現(xiàn)一個(gè)分享控件,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
十多年專注成都網(wǎng)站制作,成都定制網(wǎng)頁(yè)設(shè)計(jì),個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)頁(yè)設(shè)計(jì),高端網(wǎng)頁(yè)制作,對(duì)成都廣告推廣等多個(gè)方面,擁有豐富的網(wǎng)站營(yíng)銷(xiāo)經(jīng)驗(yàn)。
首先,聲明 BottomSheetDialog
對(duì)話框的主布局 dialog_bottom_sheet.xml
當(dāng)中,RecyclerView 用于展示提供分享功能的應(yīng)用列表
leavesC" android:textAppearance="@style/TextAppearance.AppCompat" android:textSize="16sp"/>
RecyclerView 單個(gè)子項(xiàng)使用的布局 item_app.xml
RecyclerView 配套使用的 Adapter : AppShareAdapter
/** * 作者:葉應(yīng)是葉 * 時(shí)間:2018/3/28 20:30 * 描述:https://github.com/leavesC */ public class AppShareAdapter extends RecyclerView.Adapter{ public interface OnClickListener { void onClick(int position); } public interface OnLongClickListener { void onLongClick(int position); } private List appList; private LayoutInflater layoutInflater; private OnClickListener clickListener; private OnLongClickListener longClickListener; AppShareAdapter(Context context, List 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 找出所有提供分享功能的應(yīng)用,初始化 BottomSheetDialog 即可
/** * 作者:葉應(yīng)是葉 * 時(shí)間:2018/3/28 20:30 * 描述:https://github.com/leavesC */ public class MainActivity extends AppCompatActivity { private ListappList; 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 getShareAppList(Context context, Intent intent) { List appList = new ArrayList<>(); PackageManager packageManager = context.getPackageManager(); List 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; } }
關(guān)于Android中怎么實(shí)現(xiàn)一個(gè)分享控件問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。