国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 分享控件的實現(xiàn)代碼

2019-10-22 18:13:31
字體:
供稿:網(wǎng)友

如今很多應(yīng)用都提供向外分享信息的功能,在進行分享操作時,一般是從屏幕底部彈出所有具備分享功能的應(yīng)用列表,再由用戶進行選擇

Android,分享控件,分享

現(xiàn)在我就來模仿實現(xiàn)這種效果,不僅使分享控件從屏幕底部彈出,還要使分享控件能夠上下拖動,這就需要使用到 design 包提供的 BottomSheetDialog 控件了

首先,聲明 BottomSheetDialog 對話框的主布局 dialog_bottom_sheet.xml

當中,RecyclerView 用于展示提供分享功能的應(yīng)用列表

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

/** * 作者:葉應(yīng)是葉 * 時間: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 找出所有提供分享功能的應(yīng)用,初始化 BottomSheetDialog 即可

/** * 作者:葉應(yīng)是葉 * 時間: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;  }}

Android,分享控件,分享

這里提供上述示例代碼: ShareDialog_Demo

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 建始县| 山阳县| 彰化县| 泸溪县| 西盟| 台中市| 海阳市| 兰溪市| 镇巴县| 靖安县| 九江县| 涟源市| 天台县| 永福县| 墨竹工卡县| 海门市| 新密市| 青神县| 喀什市| 绥滨县| 五寨县| 霍城县| 武胜县| 阳东县| 永吉县| 水城县| 无为县| 石景山区| 周至县| 贵溪市| 衡阳县| 元江| 微博| 淄博市| 兴宁市| 阿巴嘎旗| 原阳县| 江西省| 宜兰县| 舞阳县| 乌苏市|