【移动应用开发技术】Android中怎么实现一个分享控件_第1页
【移动应用开发技术】Android中怎么实现一个分享控件_第2页
【移动应用开发技术】Android中怎么实现一个分享控件_第3页
【移动应用开发技术】Android中怎么实现一个分享控件_第4页
【移动应用开发技术】Android中怎么实现一个分享控件_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】Android中怎么实现一个分享控件

Android中怎么实现一个分享控件,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。首先,声明BottomSheetDialog对话框的主布局dialog_bottom_sheet.xml

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

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/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="/apk/res/android"

xmlns:app="/apk/res-auto"

xmlns:tools="/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/**

*

作者:叶应是叶

*

时间:2018/3/28

20:30

*

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

*

作者:叶应是叶

*

时间:2018/3/28

20:30

*

描述:/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,

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

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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论