最近编辑于2018年4月30日
参考http://blog.csdn.net/mapdigit/article/details/7555404
这个首页的写法对于我来说比较新奇,它非常适合这种讲解例子的添加,只需要在注册表中添加一个对应label的activity就能添加一个对应子目录下的示例。
一、
入口activity就应该是ApiDemos了,app首页应该是一个列表,打开ApiDemos看看他是怎么写的。
getListView().setTextFilterEnabled(true);的作用是软键盘输入关键字后筛选item,将有该关键字的item展示出来,参考http://blog.csdn.net/blue_bamboo/article/details/45789285
ListActivity页面上默认有一个listview,一般不需要setContentView,它也只适用于一个没有数据时显示空页面(可以丰富)有数据时只展示一个listview的页面。查看源码:
从源码中可以看出,可以setContentView(layout.xml),但这个layout.xml只能有一个id为"@id/android:list"的listview和一个id为"@id/android:empty"的布局(或控件)。当有数据时展示list,没有数据时展示empty。
simapleAdapter,它是数据源getData(path)与UI ListActivity之间的桥梁,我们来看看它的参数。
第一个参数content。
第二个参数是List<? extends Map<String,?>>,数据源,是一个list,list中每一个子元素都是map,map的每一个子元素都有一个key(必须为String)和一个value。
第三个参数是@LayoutRes,listview的item布局。
第四个参数是字符串数组,对应着map中的key(map中可能还有其他key)。
第五个参数是@IdRes,对应着listview的item中控件的id。
最后会以第四个参数作为key从第二个参数中子元素 map中取出对应key的value值,将这个value值赋给item对应id的控件(第一个key(第四个参数中的第一个子元素)对应的value赋给item中第一个id(第五个参数中的第一个子元素)的控件,第二个key(第四个参数中的第二个子元素)对应的value赋给item中第二个id(第五个参数中的第二个子元素)的控件,。。。)。
二、
再来看看getData(Path)如何获取数据的。因为返回值需要是list,所以必然有list.add(),我们就是要研究它什么时候add,add的具体内容。
遍历取出每一个activity的信息,我们首页好像不是把每个activity的信息展示出来啊?那打开app展示的哪些activity的信息呢?它展示的是不是activity的信息呢?它应该有个筛选。
list中有两种item,再看看app,应该是一个是目录item,一个含有具体demoactivity信息的item。第一个页面完全是目录item,点击item进去应该还是一个listview页面。点击展示demoactivity信息的item,应该跳转到demoactivity。
这个参数就是我们在onCreate中看到的path,也就是传给getData()的参数,那么它应该是携带的目录信息,决定了这个item点击进去后我们会看到哪些目录信息item和demoactivity信息item。
可以看出他通过activity的label信息来把每个activity分来目录。
三、最后页面上的item排了一下序。
四、
这是一个列表页面的复用,如果需要做文件管理页面,也是各种目录套目录,也可以使用这种(ApiDemos跳转到ApiDemos)复用,如果是fragment跳转fragment的复用需要做好事务的处理。
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
最后,附上ApiDemos的代码,以及我下载的ApiDemos的码云地址(方便注释做笔记,以及修改后回退版本)
ApiDemos:
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApiDemos extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String path = intent.getStringExtra("com.example.android.apis.Path");
if (path == null) {
path = "";
}
setListAdapter(new SimpleAdapter(this, getData(path),
android.R.layout.simple_list_item_1, new String[] { "title" },
new int[] { android.R.id.text1 }));
getListView().setTextFilterEnabled(true);
}
protected List<Map<String, Object>> getData(String prefix) {
List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
if (null == list)
return myData;
String[] prefixPath;
String prefixWithSlash = prefix;
if (prefix.equals("")) {
prefixPath = null;
} else {
prefixPath = prefix.split("/");
prefixWithSlash = prefix + "/";
}
int len = list.size();
Map<String, Boolean> entries = new HashMap<String, Boolean>();
for (int i = 0; i < len; i++) {
ResolveInfo info = list.get(i);
CharSequence labelSeq = info.loadLabel(pm);
String label = labelSeq != null
? labelSeq.toString()
: info.activityInfo.name;
if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
String[] labelPath = label.split("/");
String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
addItem(myData, nextLabel, activityIntent(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name));
} else {
if (entries.get(nextLabel) == null) {
addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
entries.put(nextLabel, true);
}
}
}
}
Collections.sort(myData, sDisplayNameComparator);
return myData;
}
private final static Comparator<Map<String, Object>> sDisplayNameComparator =
new Comparator<Map<String, Object>>() {
private final Collator collator = Collator.getInstance();
public int compare(Map<String, Object> map1, Map<String, Object> map2) {
return collator.compare(map1.get("title"), map2.get("title"));
}
};
protected Intent activityIntent(String pkg, String componentName) {
Intent result = new Intent();
result.setClassName(pkg, componentName);
return result;
}
protected Intent browseIntent(String path) {
Intent result = new Intent();
result.setClass(this, ApiDemos.class);
result.putExtra("com.example.android.apis.Path", path);
return result;
}
protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
Map<String, Object> temp = new HashMap<String, Object>();
temp.put("title", name);
temp.put("intent", intent);
data.add(temp);
}
@Override
@SuppressWarnings("unchecked")
protected void onListItemClick(ListView l, View v, int position, long id) {
Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
Intent intent = new Intent((Intent) map.get("intent"));
intent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
startActivity(intent);
}
}
我的ApiDemos码云地址:https://gitee.com/xydzjnq/ApiDemos