设置的主界面的可以通过修改xml中的dashboard_categaries.xml 文件实现,在DashboardSummary.java 文件中的rebuildUI()方法中将xml对应的实体类转换成对应的view,具体细节可以看设置源码。
一,dashboard_categaries中定义节点的样式:
<!-- Wifi -->
<dashboard-tile
android:id="@+id/wifi_settings"
android:fragment="com.android.settings.wifi.WifiSettings"
android:icon="@drawable/sunmi_wifi"
android:title="@string/wifi_settings_title" />
<!-- 移动网络 -->
<dashboard-tile
android:id="@+id/mobile_net_settings"
android:icon="@drawable/sunmi_network"
android:title="@string/network_settings_title" >
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.android.phone.MobileNetworkSettings"
android:targetPackage="com.android.phone" />
</dashboard-tile>
这是设置中的wifi,和移动网络选项,一个是添加fragment ,另一个是添加intent
解析这个xml是在SettingActivity中的loadCategoriesFromResource(R.xml.dashboard_categories, categories);方法中,
二,DashboardSummary.java 文件中的rebuildUI()方法
private void rebuildUI(Context context) {
if (!isAdded()) {
return;
}
final Resources res = getResources();
mDashboard.removeAllViews();
List<DashboardCategory> categories = ((SettingsActivity) context).getDashboardCategories(true);
final int count = categories.size();
for (int n = 0; n < count; n++) {
DashboardCategory category = categories.get(n);
View categoryView = mLayoutInflater.inflate(R.layout.dashboard_category, mDashboard, false);
TextView categoryLabel = (TextView) categoryView.findViewById(R.id.category_title);
categoryLabel.setText(category.getTitle(res));
ViewGroup categoryContent = (ViewGroup) categoryView.findViewById(R.id.category_content);
final int tilesCount = category.getTilesCount();
for (int i = 0; i < tilesCount; i++) {
DashboardTile tile = category.getTile(i);
DashboardTileView tileView = new DashboardTileView(context);
updateTileView(context, res, tile, tileView.getImageView(), tileView.getTitleTextView(),
tileView.getStatusTextView());
tileView.setTile(tile);
categoryContent.addView(tileView);
}
// Add the category
mDashboard.addView(categoryView);
}
}
分析源码可知rebuildui()是将xml中解析的实体类,构建成对应的view(categoryView,DashboardTileView)在这并没有看到添加点击事件,所以猜测应该写到DashboardTileView中了
三,DashboardTileView的点击事件
public class DashboardTileView extends FrameLayout implements View.OnClickListener
看到这里就知道是在这里实现点击事件处理的
@Override
public void onClick(View v) {
if (mTile.fragment != null) {
Utils.startWithFragment(getContext(), mTile.fragment, mTile.fragmentArguments, null, 0,
mTile.titleRes, mTile.getTitle(getResources()));
} else if (mTile.intent != null) {
getContext().startActivity(mTile.intent);
}
}
看到这里一目了然啦,可以知道fragment 优先级>intent
再来看fragment的跳转
四,fragment的跳转细节
public static void startWithFragment(Context context, String fragmentName, Bundle args,
Fragment resultTo, int resultRequestCode, int titleResId,
CharSequence title) {
startWithFragment(context, fragmentName, args, resultTo, resultRequestCode,
null /* titleResPackageName */, titleResId, title, false /* not a shortcut */);
}
public static void startWithFragment(Context context, String fragmentName, Bundle args,
Fragment resultTo, int resultRequestCode, String titleResPackageName, int titleResId,
CharSequence title, boolean isShortcut) {
Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, titleResPackageName,
titleResId, title, isShortcut);
if (resultTo == null) {
context.startActivity(intent);
} else {
resultTo.startActivityForResult(intent, resultRequestCode);
}
}
public static Intent onBuildStartFragmentIntent(Context context, String fragmentName,
Bundle args, String titleResPackageName, int titleResId, CharSequence title,
boolean isShortcut) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(context, SubSettings.class);
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, fragmentName);
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RES_PACKAGE_NAME,
titleResPackageName);
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, titleResId);
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE, title);
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, isShortcut);
return intent;
}
可以知道是通过构建一个带fragmentName参数的intent来启动SubSettings.class
而SubSettings.class中并没有实现具体添加fragment,在父类SettingsActivity中oncrreate()中获取具体参数,添加对应fragment
点击Setting 之dashboard 点击跳转流程就是这样啦