标签:
前言:
好几天电脑打不开CSDN博客,也不知道怎么回事,今天下班回来突然能打开了,遂将周末实现的一个效果贴上。
实现功能:
获取手机应用图标,名称,时间(安装时间/更新时间),大小,侧滑删除应用,点击应用图标分享等功能。
目标效果:
思路:RecylerView+swipereveallayout
贴上dependencies
介绍:Glide实现图片加载,EventBus通信,swipereveallayout实现侧滑。
获取数据源:
private ArrayList<AppInfoModel> getData() {
ArrayList<AppInfoModel> list = new ArrayList<>();
PackageManager mPackageManager = getApplicationContext().getPackageManager();
packageInfoList = (ArrayList<PackageInfo>) mPackageManager.getInstalledPackages(0);
for (PackageInfo packageInfo : packageInfoList) {
AppInfoModel model = new AppInfoModel();
model.setName(getApplicationName(packageInfo.applicationInfo.packageName, mPackageManager));
model.setIcon(getAppliactionIcon(packageInfo, mPackageManager));
model.setTime(getDate(packageInfo.lastUpdateTime));
model.setSize(new File(packageInfo.applicationInfo.sourceDir).length() / 1024 / 1024 + "MB");
list.add(model);
}
return list;
}
获取应用名:
/**
* 获取应用的名称
*/
public String getApplicationName(String packageName, PackageManager packageManager) {
String applicationName = null;
try {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
} catch (PackageManager.NameNotFoundException e) {
}
return applicationName;
}
获取应用的Icon
/**
* 获取应用的Icon
*/
public Drawable getAppliactionIcon(PackageInfo packageInfo, PackageManager packageManager) {
Drawable appliactionIcon = packageInfo.applicationInfo.loadIcon(packageManager);
return appliactionIcon;
}
生成时间
/**
* 生成时间
*/
public static String getDate(long time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(time);
String formatedDate = simpleDateFormat.format(date);
return formatedDate;
}
EventBus实现通信
//事件3接收者:在主线程接收
public void onEventMainThread(ClickEvent event) {
switch (event.type) {
case ClickEvent.CLICK_ITEM:
int position = (int) event.data;
File apkFile = new File(packageInfoList.get(position).applicationInfo.sourceDir);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(apkFile));
startActivity(intent);
break;
}
}
适配器:
package com.example.wangchang.testqqfile.adapter;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.chauthai.swipereveallayout.ViewBinderHelper;
import com.example.wangchang.testqqfile.R;
import com.example.wangchang.testqqfile.bean.AppInfoModel;
import com.example.wangchang.testqqfile.event.ClickEvent;
import java.util.ArrayList;
import de.greenrobot.event.EventBus;
/**
* Created by WangChang on 2016/5/7.
*/
public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.DemoViewHolder> {
private ArrayList<AppInfoModel> list = new ArrayList<>();
private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper();
public void getData(ArrayList<AppInfoModel> data) {
list.clear();
if (data != null) {
list.addAll(data);
}
notifyDataSetChanged();
}
@Override
public DemoAdapter.DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new DemoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
}
@Override
public void onBindViewHolder(DemoAdapter.DemoViewHolder holder, int position) {
holder.setData(list.get(position));
}
public void saveStates(Bundle outState) {
viewBinderHelper.saveStates(outState);
}
public void restoreStates(Bundle inState) {
viewBinderHelper.restoreStates(inState);
}
@Override
public int getItemCount() {
return list != null ? list.size() : 0;
}
public class DemoViewHolder extends RecyclerView.ViewHolder {
private TextView name, time, size;
private ImageView icon;
private SwipeRevealLayout swipeRevealLayout;
private View deleteLayout;
public DemoViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
time = (TextView) itemView.findViewById(R.id.time);
size = (TextView) itemView.findViewById(R.id.size);
icon = (ImageView) itemView.findViewById(R.id.icon);
swipeRevealLayout= (SwipeRevealLayout) itemView.findViewById(R.id.swipe_layout);
deleteLayout = itemView.findViewById(R.id.delete_layout);
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new ClickEvent(ClickEvent.CLICK_ITEM, getAdapterPosition()));
}
});
deleteLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
}
});
}
void setData(AppInfoModel model) {
if (model != null) {
viewBinderHelper.bind(swipeRevealLayout,model.getName());
name.setText(model.getName());
time.setText(model.getTime());
size.setText(model.getSize());
icon.setImageDrawable(model.getIcon());
}
}
}
}
item布局,包含侧滑布局
<?xml version="1.0" encoding="utf-8"?>
<com.chauthai.swipereveallayout.SwipeRevealLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:dragEdge="right"
app:mode="same_level">
<FrameLayout
android:id="@+id/delete_layout"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:background="#ffcc0000">
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="Delete"
android:textColor="@android:color/white" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/icon"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:singleLine="true"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="10sp" />
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#aaaaaa"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#E0E0E0" />
</LinearLayout>
</FrameLayout>
</com.chauthai.swipereveallayout.SwipeRevealLayout>
具体请参考:
https://github.com/chthai64/SwipeRevealLayout
实现效果:
侧滑效果:
分享效果:
效果就到这,源码下载地址
标签:
原文地址:http://blog.csdn.net/qq_16131393/article/details/51367597