在开发launcher中通常有这个需求就是在主界面上预留空白控件,可以让用户从apps中自己选择apk放入。
1. 点击空白控件跳转到选择apk的布局中,所以我们需要写一个布局用来存放所有的apk,这里我使用的是activity,当然也可以用framlayout。
public class Test extends Activity implements AdapterView.OnItemClickListener{
private GridView gv;
private final static String TAG = "Test";
private final static String NAME = "item_name";
private final static String ICON = "item_icon";
private final static String SELECTION = "item_selection";
private final static String BACKGROUND = "item_background";
private final static String COMPONENT_NAME = "component name";
private final static int duration = 300;
private ImageView img_screen_shot = null;
private ImageView img_screen_shot_keep = null;
private ImageView img_dim = null;
private CustomView thisView = null;
private Context mContext = null;
private String[] list_custom_apps;
private String str_custom_apps;
private Drawable bitmap;
private String str_name;
private int transY = 0;
private int homeShortcutCount;
final static Object mLock = new Object[0];
private View mSource;
private int mMode = -1;
private int mPos = -1;
private int resultCode = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_apps);
mContext = Launcher.context;
mMode = 4;
gv =(GridView) findViewById(R.id.grid_add_apps);
gv.setOnItemClickListener(this);
displayView();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
Log.d(TAG,"onStop---------------onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.d(TAG,"onDestroy---------------onDestroy"+str_custom_apps);
super.onDestroy();
}
public boolean dispatchKeyEvent (KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
Intent intent = new Intent();
intent.putExtra("pkg",str_custom_apps);
intent.setAction("com.ff.pkg");
this.sendBroadcast(intent);
Log.d(TAG, "KeyEvent.KEYCODE_BACK............");
break;
}
}
return super.dispatchKeyEvent(event);
}
private List<ArrayMap<String, Object>> getAppList() {
List<ArrayMap<String, Object>> list = new ArrayList<ArrayMap<String, Object>>();
List<ArrayMap<String, Object>> list_all = ((Launcher)mContext).getAppDataLoader().getShortcutList(Launcher.MODE_APP);
List<ArrayMap<String, Object>> list_current = ((Launcher)mContext).getAppDataLoader().getShortcutList(mMode);
homeShortcutCount = 0;
for (int i = 0; i < list_all.size(); i++) {
ArrayMap<String, Object> map = new ArrayMap<String, Object>();
map.put(SELECTION, R.drawable.item_img_unsel);
for (int j = 0; j < list_current.size() - 1; j++) {
if (TextUtils.equals(list_all.get(i).get(AppDataLoader.COMPONENT_NAME).toString(),
list_current.get(j).get(AppDataLoader.COMPONENT_NAME).toString())) {
map.put(SELECTION, R.drawable.item_img_unsel);
if (mMode == Launcher.MODE_HOME) {
homeShortcutCount++;
}
break;
}
}
map.put(NAME, list_all.get(i).get(AppDataLoader.NAME));
map.put(ICON, list_all.get(i).get(AppDataLoader.ICON));
map.put(BACKGROUND, R.drawable.item_child_6);
map.put(COMPONENT_NAME, list_all.get(i).get(AppDataLoader.COMPONENT_NAME));
list.add(map);
}
return list;
}
private void displayView() {
LocalAdapter ad = new LocalAdapter(mContext,
getAppList(),
R.layout.add_apps_grid_item,
new String[] {ICON, NAME, SELECTION, BACKGROUND},
new int[] {R.id.item_type, R.id.item_name, R.id.item_sel, R.id.relative_layout});
gv.setAdapter(ad);
}
private void updateView() {
((BaseAdapter)gv.getAdapter()).notifyDataSetChanged();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ArrayMap<String, Object> it