一直想从网上找一个类似“知乎”里文章分享的控件。但是好像都没有特别像的,于是乎只能自己做一个了。
先来整理整理思路:
1,用到的主要东西就是BottomSheetDialog这个东西,要把这东西引入工程需要在工程的app. gradle里添加下面的代码:
ext {
supportVersion = '23.3.0'
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:${supportVersion}"
compile "com.android.support:design:${supportVersion}"
}
2,写一个九宫格样式的RecyclerView,为什么不是GrideView?因为GrideView与BottomSheetDialog的手势有冲突。
至于如何查询手机内所有支持分享的应用列表有篇文章:http://blog.csdn.net/wangjia55/article/details/8285403
主界面代码
public class MainActivity extends Activity implements View.OnClickListener {
private Button btnAndroid;
private Button btnIos;
private Button btnBottomSheet;
private HorizontalListView h_list_view;
private ListView listView;
private GridView gridView;
HorizontalListViewAdapter h_adapter;
ArrayList<AppInfo> list;
RecyclerViewAdapter recyclerViewAdapter;
RecyclerView mRecyclerView;
private GoogleApiClient client;
BottomSheetDialog d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
list = getShareAppList();
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
private void initView() {
btnBottomSheet = (Button) findViewById(R.id.bottom_btn);
btnAndroid = (Button) findViewById(R.id.android_btn);
btnIos = (Button) findViewById(R.id.ios_btn);
btnAndroid.setOnClickListener(this);
btnIos.setOnClickListener(this);
btnBottomSheet.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.android_btn:
Intent data = new Intent(Intent.ACTION_SEND);
data.setData(Uri.parse("mailto:"));
data.setType("text/plain");
data.putExtra(Intent.EXTRA_TEXT, "www.baidu.com");
startActivity(data);
break;
case R.id.bottom_btn:
showBottomDialog();
break;
}
}
private void showBottomDialog() {
d = Utility.getBottomDialog(this, R.layout.dialog_bottom_sheet);
initBottomDialog(d);
setBehaviorCallback();
d.show();
}
private void initBottomDialog(final Dialog d) {
recyclerViewAdapter = new RecyclerViewAdapter(this,list);
mRecyclerView = (RecyclerView)d.findViewById(R.id.list_view);
mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
mRecyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.setOnItemClickLitener(new RecyclerViewAdapter.OnItemClickLitener() {
@Override
public void onItemClick(AppInfo parent,View view, int position) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
AppInfo appInfo = parent;
shareIntent.setComponent(new ComponentName(appInfo.getPkgName(), appInfo.getLaunchClassName()));
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "www.baidu.com");
startActivity(shareIntent);
}
@Override
public void onItemLongClick(AppInfo parent,View view, int position) {
}
});
private ArrayList<AppInfo> getShareAppList() {
ArrayList<AppInfo> shareAppInfos = new ArrayList<>();
PackageManager packageManager = getPackageManager();
List<ResolveInfo> resolveInfos = getShareApps(MainActivity.this);
if (null == resolveInfos) {
return null;
} else {
for (ResolveInfo resolveInfo : resolveInfos) {
AppInfo appInfo = new AppInfo();
appInfo.setPkgName(resolveInfo.activityInfo.packageName);
appInfo.setLaunchClassName(resolveInfo.activityInfo.name);
appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString());
appInfo.setAppIcon(resolveInfo.loadIcon(packageManager));
shareAppInfos.add(appInfo);
}
}
return shareAppInfos;
}
public List<ResolveInfo> getShareApps(Context context) {
List<ResolveInfo> mApps;
Intent intent = new Intent(Intent.ACTION_SEND, null);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("text/plain");
PackageManager pManager = context.getPackageManager();
mApps = pManager.queryIntentActivities(intent,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
return mApps;
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Main Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
Utility代码
public class Utility {
public static BottomSheetDialog getBottomDialog(Context context, int viewId, boolean canceledOnTouchOutside, boolean cancelable) {
final BottomSheetDialog d = new BottomSheetDialog(context);
// d.requestWindowFeature(Window.FEATURE_NO_TITLE);
// d.setCanceledOnTouchOutside(canceledOnTouchOutside);
// d.setCancelable(cancelable);
d.setContentView(viewId);
return d;
}
public static BottomSheetDialog getBottomDialog(Context context, int viewId) {
return getBottomDialog(context, viewId, true, true);
}
}
还有 RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private Context mContext;
private List<Integer> mHeight;
private ArrayList<AppInfo> appList;
public interface OnItemClickLitener {
void onItemClick(AppInfo parent, View view, int position);
void onItemLongClick(AppInfo parent, View view , int position);
}
private OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}
public RecyclerViewAdapter(Context mContext, ArrayList<AppInfo> date) {
this.appList = date;
this.mContext = mContext;
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_share_bottomsheet_recyclerview_item, parent, false);
return new ViewHolder(view);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onBindViewHolder(final RecyclerViewAdapter.ViewHolder holder, int position) {
holder.tv.setText(appList.get(position).getAppName());
holder.img.setBackground(appList.get(position).getAppIcon());
if (mOnItemClickLitener != null)
{
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemClick(appList.get(pos), holder.itemView, pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemLongClick(appList.get(pos),holder.itemView, pos);
return false;
}
});
}
}
@Override
public int getItemCount() {
return appList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView tv;
ImageView img;
public ViewHolder(View view) {
super(view);
tv = (TextView) view.findViewById(R.id.text_list_item);
img = (ImageView) view.findViewById(R.id.img_list_item);
}
}
}
ok到这 全部核心的代码就都已经在这了
值得注意的地方:
1,BottomSheetDialog里面要用RecyclerView,要不然会有手势滑动的冲突。
2,RecyclerView没有写好的setOnItemClickLitener,所以需要在adapter内自己写一个,这种代码网上有很多,所以就不再赘述了。
最后上两张效果图: