其他分享
首页 > 其他分享> > Android记录8--简单自定义Launcher实现,十位互联网一线高工手写Android高级知识

Android记录8--简单自定义Launcher实现,十位互联网一线高工手写Android高级知识

作者:互联网

/LanchuerTest/src/com/wwj/launcher/HeyWwj.java

Activity

package com.wwj.launcher;

import java.util.List;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.pm.ResolveInfo;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.GridView;

public class HeyWwj extends Activity {

private GridView mGridView;

private List mApps;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

loadApps();

mGridView = (GridView) findViewById(R.id.gridView1);

mGridView.setAdapter(new AppAdapter(this, mApps));

mGridView.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id) {

ResolveInfo info = mApps.get(position);

// 应用的包名

String pkg = info.activityInfo.packageName;

// 应用的主Activity

String cls = info.activityInfo.name;

ComponentName componentName = new ComponentName(pkg, cls);

Intent intent = new Intent();

intent.setComponent(componentName);

startActivity(intent);

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

}

});

}

/**

*/

private void loadApps() {

Intent intent = new Intent(Intent.ACTION_MAIN, null);

intent.addCategory(Intent.CATEGORY_LAUNCHER);

mApps = getPackageManager().queryIntentActivities(intent, 0);

}

}

自定义适配器

/LanchuerTest/src/com/wwj/launcher/AppAdapter.java

package com.wwj.launcher;

import java.util.List;

import android.content.Context;

import android.content.pm.ResolveInfo;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.GridView;

import android.widget.ImageView;

import android.widget.ImageView.ScaleType;

public class AppAdapter extends BaseAdapter {

List mApps;

Context context;

public AppAdapter(Context context, List apps) {

super();

this.context = context;

this.mApps = apps;

}

@Override

public int getCount() {

return mApps.size();

}

@Override

public Object getItem(int position) {

return mApps.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ImageView iv;

if (convertView == null) {

iv = new ImageView(context);

iv.setScaleType(ScaleType.FIT_CENTER);

iv.setLayoutParams(new GridView.LayoutParams(50,50));

} else {

iv = (ImageView) convertView;

}

ResolveInfo info = mApps.get(position);

iv.setImageDrawable(info.activityInfo.loadIcon(context.getPackageManager()));

return iv;

}

标签:自定义,new,高工,public,mApps,position,import,Android,android
来源: https://blog.csdn.net/m0_65145548/article/details/122042606