android 共享代码,Android 分享控件的实现代码

如今很多应用都提供向外分享信息的功能,在进行分享操作时,一般是从屏幕底部弹出所有具备分享功能的应用列表,再由用户进行选择

4d253a3177381c1c8975998f496fc528.png

现在我就来模仿实现这种效果,不仅使分享控件从屏幕底部弹出,还要使分享控件能够上下拖动,这就需要使用到 design 包提供的 BottomSheetDialog 控件了

首先,声明 BottomSheetDialog 对话框的主布局 dialog_bottom_sheet.xml

当中,RecyclerView 用于展示提供分享功能的应用列表

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">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="14dp"

android:text="进一步的说明 -> leavesC"

android:textAppearance="@style/TextAppearance.AppCompat"

android:textSize="16sp"/>

android:layout_width="match_parent"

android:layout_height="0.6dp"

android:background="#ddd"/>

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:id="@+id/rv_appList"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

RecyclerView 单个子项使用的布局 item_app.xml

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">

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"/>

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="之乎者也"/>

RecyclerView 配套使用的 Adapter : AppShareAdapter

/**

* 作者:叶应是叶

* 时间: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 找出所有提供分享功能的应用,初始化 BottomSheetDialog 即可

/**

* 作者:叶应是叶

* 时间:2018/3/28 20:30

* 描述:https://github.com/leavesC

*/

public class MainActivity extends AppCompatActivity {

private List 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 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;

}

}

dd0afc4efb07d3a90fb7ab1ec6d606ef.gif

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值