最近在写一个launcher,于是整理了一下获取应用的工具类和横向的显示所有app的方法。里面使用了横向的recyclerview来显示,内容也很简单。
结尾有源码的下载地址。
1.导入依赖
implementation 'com.android.support:recyclerview-v7:27.1.1'
2.显示布局
注意:这里我使用了px的像素来适配屏幕,这个是适配的一种方式。
如果直接复制可能会报@dimen/x150找不到的错误。
这个在源码的valus里面可以找到。
3.适配器
适配器里面的内容很普通,就是把获取到的值进行设置到布局里面。
package com.luo.recyclerviewallapp.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
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.LinearLayout;
import android.widget.TextView;
import com.luo.recyclerviewallapp.R;
import com.luo.recyclerviewallapp.bean.AppInfo;
import java.util.List;
/**
* Created by luo on 2019/8/12.
*/
public class AllAdapter extends RecyclerView.Adapter {
private View mView;
private List mlist;
private Context mContext;
private VH mVH;
private OnItemClickListener listener;
private OnItemLongClickListener longClickListener;
private AppInfo appInfo = null;
public AllAdapter(Context context, List list) {
this.mlist = list;
this.mContext = context;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_allapp, parent, false);
mVH = new VH(mView);
return mVH;
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
appInfo = mlist.get(position);
//获取到的app的图标和名字进行设置,也可以通过判断包名来修改图片
holder.icon.setBackground(appInfo.getAppIcon());
holder.name.setText(appInfo.getAppName());
final int itemposition = position;
holder.itemlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(itemposition);
}
}
});
holder.itemlayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (longClickListener != null) {
longClickListener.onClick(itemposition);
}
return true;
}
});
}
@Override
public int getItemCount() {
return mlist.size();
}
//点击接口
public interface OnItemClickListener {
void onClick(int position);
}
//公共的点击方法
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
//长按的点击接口
public interface OnItemLongClickListener {
void onClick(int position);
}
//公共的长按的接口
public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
this.longClickListener = longClickListener;
}
public class VH extends RecyclerView.ViewHolder {
public LinearLayout itemlayout;
public TextView name;
public ImageView icon;
public VH(View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.item_app_icon);
itemlayout = (LinearLayout) itemView.findViewById(R.id.item_layout);
name = (TextView) itemView.findViewById(R.id.item_app_name);
}
}
}
4.获取所有应用的工具类
package com.luo.recyclerviewallapp.utils;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ResolveInfo.DisplayNameComparator;
import com.luo.recyclerviewallapp.bean.AppInfo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by luo on 2019/8/12.
*/
public class AppUtils {
/**
* get all app info
*
* @param context
* @return
*/
public static final ArrayList getAppInfos(Context context) {
ArrayList appInfos = new ArrayList();
PackageManager manager = context.getPackageManager();
Intent mainIntent = new Intent("android.intent.action.MAIN", null);
mainIntent.addCategory("android.intent.category.LAUNCHER");
//通过查看是否有LAUNCHER属性来添加
List resolveInfos = manager.queryIntentActivities(mainIntent, 0);
Collections.sort(resolveInfos, new DisplayNameComparator(manager));
if (appInfos != null) {
appInfos.clear();
for (ResolveInfo resolveInfo : resolveInfos) {
if (resolveInfo.activityInfo.packageName.equals("com.mstar.tv.tvplayer.ui")) {
continue;
}
//这里是过滤apk,可以加入一些你认为不需要显示的apk
AppInfo appInfo = new AppInfo();
appInfo.setAppIcon(resolveInfo.loadIcon(manager));
appInfo.setAppName((String) resolveInfo.loadLabel(manager));
appInfo.setPackageName(resolveInfo.activityInfo.packageName);
appInfos.add(appInfo);
}
}
return appInfos;
}
}
5.bean文件
因为bean文件只是有一点,这里截图一下,有需要可以在源码中直接复制。
6.MainActivit代码
package com.luo.recyclerviewallapp;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.luo.recyclerviewallapp.adapter.AllAdapter;
import com.luo.recyclerviewallapp.bean.AppInfo;
import com.luo.recyclerviewallapp.utils.AppUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Created by luo on 2019/8/12.
*/
public class MainActivity extends AppCompatActivity {
private List allApp;
private LinearLayoutManager linearLayoutManager;
private LinearLayoutManager linearLayoutManagertwo;
private AllAdapter allMyAdapater;
private RecyclerView allAppRecycler;
private RecyclerView allAppRecyclertwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
allApp = new ArrayList();
allApp = AppUtils.getAppInfos(this);
allMyAdapater = new AllAdapter(this, allApp);
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManagertwo = new LinearLayoutManager(this);
}
private void initView() {
allAppRecycler = (RecyclerView) findViewById(R.id.recyclerview);
allAppRecyclertwo = (RecyclerView) findViewById(R.id.recyclerviewtwo);
//把显示设置成横向,默认是竖列
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
linearLayoutManagertwo.setOrientation(LinearLayoutManager.HORIZONTAL);
allAppRecyclertwo.setLayoutManager(linearLayoutManagertwo);
allAppRecyclertwo.setAdapter(allMyAdapater);
allAppRecycler.setLayoutManager(linearLayoutManager);
allAppRecycler.setAdapter(allMyAdapater);
//点击跳转apk
allMyAdapater.setOnItemClickListener(new AllAdapter.OnItemClickListener() {
@Override
public void onClick(int position) {
//这里通过包名来跳转
Intent mIntent = new Intent();
PackageManager packageManager = getPackageManager();
AppInfo appInfo = allApp.get(position);
mIntent = packageManager.getLaunchIntentForPackage(appInfo.getPackageName());
if (mIntent != null) {
startActivity(mIntent);
}
}
});
//长按卸载apk,但是我这里没有做刷新的操作,需要注意一下
allMyAdapater.setOnItemLongClickListener(new AllAdapter.OnItemLongClickListener() {
@Override
public void onClick(int position) {
AppInfo appInfo = allApp.get(position);
uninstall(appInfo.getPackageName());
}
});
}
public boolean uninstall(String packageName) {
boolean b = checkApplication(packageName);
if (b) {
Uri packageURI = Uri.parse("package:".concat(packageName));
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(packageURI);
this.startActivity(intent);
allMyAdapater.notifyDataSetChanged();
return true;
}
return false;
}
private boolean checkApplication(String packageName) {
if (packageName == null || "".equals(packageName)) {
return false;
}
return true;
}
}
7.整体思路
1.通过recyclerview的横向来显示布局
2.获取的时候通过LAUNCHER来得到apk
3.布局的焦点框是通过写一个白色矩形框来实现
4.跳转apk可以通过Adapter的点击事件来实现
总体来说,recyclerview来实现横向的显示,可以gridview的横向踩的坑少许多。
8.源码路径
https://github.com/githubsmallluo/RecyclerViewAllAPP/tree/master
可以给作者评论,关注加喜欢吗?
如果还存在不懂的地方可以联系一下作者,我会帮忙解答!