仿知乎分享界面
前言
最近在做一个资讯类的APP,上面需要一个分享功能,项目不大,如果去使用官方的SDK还需要审查之类的,感觉太麻烦。偶然看到知乎的分享界面做得不错,拿到我这个项目中正合适,在网上查了一下资料,使用BottomSheetDialogFragment
结合系统自带的分享功能就可以做到它的效果。
知乎分享界面:
自己完成的效果图:
)
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/fragment_share_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginTop="16dp" android:text="@string/shareTo" android:textColor="@color/black" android:gravity="center_vertical" android:textSize="16sp"/> <android.support.v7.widget.RecyclerView android:id="@+id/fragment_share_recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" /> </LinearLayout>
数据
获取手机中可接收我们分享的数据的App集合,方法很简单:
public static List<ResolveInfo> getShareList(Context context) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0); return list; }
如果你只想显示指定的APP,或者把一些APP的显示位置提前,在这里可以通过APP的包名进行筛选或过滤。
例如你想把腾讯旗下的APP提前一点,可以这么做:
//调整顺序,把微信、QQ提到前面来 Collections.sort(list, new Comparator<ResolveInfo>() { @Override public int compare(ResolveInfo resolveInfo, ResolveInfo t1) { ActivityInfo activityInfo1 = resolveInfo.activityInfo; ActivityInfo activityInfo2 = t1.activityInfo; if (activityInfo1.packageName.contains("com.tencent.") && !activityInfo2.packageName.contains("com.tencent.")) { return -1; } else if (!activityInfo1.packageName.contains("com.tencent.") && activityInfo2.packageName.contains("com.tencent.")) { return 1; } return 0; } });
之后我们就能很容易获取到这些App的名字和图标。
Drawable icon = list.get(i).loadIcon(context.getPackageManager()); String label = list.get(i).loadLabel(context.getPackageManager()).toString();
界面代码
新建一个Fragment
继承BottomSheetDialogFragment
,BottomSheetDialogFragment
的使用很简单,界面代码的编写与在普通的Fragment
一样,然后我们只需调用它的show()/dismiss()
即可让它显示或关闭。
完整的shareFragment
代码:
public class ShareFragment extends BottomSheetDialogFragment { private List<ResolveInfo> mShareResolveInfoList; private List<ShareItem> mShareList; private Context mContext; private static String mTitle; private static String mUrl; public static ShareFragment getInstance(String title, String url) { mTitle = title; mUrl = url; return new ShareFragment(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mContext = getContext(); View view = inflater.inflate(R.layout.fragment_share, container, false); initData(); initViews(view); return view; } private void initViews(View view) { RecyclerView recyclerView = view.findViewById(R.id.fragment_share_recyclerView); recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3)); ShareRecyclerViewAdapter adapter = new ShareRecyclerViewAdapter(mShareList, mContext); adapter.setOnClickShareItemListener(new ShareRecyclerViewAdapter.OnClickShareItemListener() { @Override public void OnClick(int position) { Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, "我在讯博上发现一篇好文章:" + "\n" + mTitle + "\n" + mUrl); intent.setType("text/plain"); ActivityInfo activityInfo = mShareResolveInfoList.get(position).activityInfo; intent.setClassName(activityInfo.packageName, activityInfo.name); startActivity(intent); dismiss(); } }); recyclerView.setAdapter(adapter); } private void initData() { mShareList = new ArrayList<>(); mShareResolveInfoList = ShareUtil.getShareList(mContext); for (int i = 0; i < mShareResolveInfoList.size(); i++) { ShareItem item = new ShareItem(); item.setIcon(mShareResolveInfoList.get(i).loadIcon(mContext.getPackageManager())); item.setLabel(mShareResolveInfoList.get(i).loadLabel(mContext.getPackageManager()).toString()); mShareList.add(item); } } }
这里我实现的是分享文字,如果要分享其他东西例如图片,把intent.setType("text/plain")
修改成intent.setType("image/jpeg")
,再在intent.putExtra()
中传去入分享的东西即可。
把分享的显示封装成一个工具类方法,方便在任何界面调用
public static void share(FragmentManager fragmentManager, String title, int id) { String url = "xxx"; ShareFragment.getInstance(title, url).show(fragmentManager, "dialog"); }
最后
文章中如果有什么错误或可以改进的地方,欢迎在评论区给我留言。
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28