Android copy Zhihu share controls
Using BottomSheetDialog to implement imitation Zhihu sharing control main steps:
- Firstly, all apps that support sharing in the mobile phone are acquired to obtain the ResolveInfo object, and the application ICONS and other information are obtained by reflection
- Then use RecyclerView GridLayoutManager grid layout display, their own click events are almost complete
screenshots
zhihu | imitation | native |
Simple implementation
-
Write a layout for the sharing interface
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:text="I am the Advertising column." android:textAppearance="@style/TextAppearance.AppCompat" android:textSize="18sp"/> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@android:color/darker_gray"/> <android.support.v7.widget.RecyclerView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>Copy the code
-
Display in the Activity using the BottomSheetDialog control
BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(this); mBottomSheetDialog.setContentView(R.layout.dialog_bottom_sheet); mBottomSheetDialog.show();Copy the code
-
Write a RecyclerViewAdapter and appinfo_item layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/img_list_item" android:layout_width="48dp" android:layout_height="48dp" android:scaleType="centerCrop" tools:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/text_list_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:ellipsize="end" android:singleLine="true" tools:text=Share "11111111111111"/> </LinearLayout>Copy the code
-
Get a list of all the apps on your phone that support sharing
public static List<ResolveInfo> getShareApps(Context context, Intent intent) { List<ResolveInfo> resolveInfoList; PackageManager pm = context.getPackageManager(); resolveInfoList = pm.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); return resolveInfoList; }Copy the code
-
Return the data to the Activity, and then set up the Adapter. See ZhihuShareDialog for the code
disadvantages
-
Applications obtained through PackageManager’s queryIntentActivities method are in the order in which they are installed, while Lollipop(5.0) has support for native sharing to automatically rank frequently used applications at the top. Of course, you can also implement your own application sorting, but more difficult, you can refer to the source code ResolverActivity
advantages
- You can customize the sharing interface, such as adding advertising columns like Zhihu, designing a set of your own UI, setting the order of applications, and so on
Download source address: github.com/iMeiji/Zhih…