本文共 24717 字,大约阅读时间需要 82 分钟。
这节我们主要讨论收藏与历史记录页面的边边角角。
首先,看看他的最终的效果图了:
照例了,我们先看看他的布局文件:
1 26 11 2215 16 21
相应的布局文件说明如下:
别开这是一个布局文件其实涉及另外两个布局文件,相应布局文件是左右两边布局文件。他两个布局文件源代码又是怎么样了?一个是书签页面的布局文件,一个是历史记录的布局文件。但是布局文件共拥有一个布局文件。
相应源代码如下:
14 5 9 10 16 17
一个list控件加一个文本控件就ok了。
布局文件,一直都不是android工程中重头戏。java文件又是什么样子了?
我们来看看他所涉及的相应的java文件。
首先,我们看一下书签历史记录页面的java 源代码,他在这里的控制作用也只有一种抛砖引玉的作用了.
1 /** 2 * 这是一个historyactivity 3 */ 4 public class BookmarksHistoryActivity extends TabActivity { 5 6 /** 7 * 创建的方法 8 */ 9 @Override10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState);12 13 if (Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_SHOW_FULL_SCREEN, false)) { 14 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);15 }16 17 if (Controller.getInstance().getPreferences().getBoolean(Constants.PREFERENCES_GENERAL_HIDE_TITLE_BARS, true)) {18 requestWindowFeature(Window.FEATURE_NO_TITLE);19 }20 21 setContentView(R.layout.bookmarks_history_activity);22 23 setTitle(R.string.BookmarksListActivity_Title);24 25 Resources res = getResources();26 TabHost tabHost = getTabHost();27 TabHost.TabSpec spec;28 Intent intent;29 30 // Bookmarks31 intent = new Intent().setClass(this, BookmarksListActivity.class);32 33 spec = tabHost.newTabSpec("bookmarks").setIndicator(res.getString(R.string.Main_MenuShowBookmarks),34 res.getDrawable(R.drawable.ic_tab_bookmarks))35 .setContent(intent);36 tabHost.addTab(spec);37 38 // History39 intent = new Intent().setClass(this, HistoryListActivity.class);40 41 spec = tabHost.newTabSpec("history").setIndicator(res.getString(R.string.Main_MenuShowHistory),42 res.getDrawable(R.drawable.ic_tab_history))43 .setContent(intent);44 tabHost.addTab(spec);45 46 if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREFERENCE_USE_WEAVE, false)) {47 // Weave bookmarks48 intent = new Intent().setClass(this, WeaveBookmarksListActivity.class);49 50 spec = tabHost.newTabSpec("weave").setIndicator(res.getString(R.string.WeaveBookmarksListActivity_Title),51 res.getDrawable(R.drawable.ic_tab_weave))52 .setContent(intent);53 tabHost.addTab(spec);54 }55 56 tabHost.setCurrentTab(0);57 58 tabHost.setOnTabChangedListener(new OnTabChangeListener() { 59 @Override60 public void onTabChanged(String tabId) {61 if (tabId.equals("bookmarks")) {62 setTitle(R.string.BookmarksListActivity_Title);63 } else if (tabId.equals("history")) {64 setTitle(R.string.HistoryListActivity_Title);65 } else if (tabId.equals("weave")) {66 setTitle(R.string.WeaveBookmarksListActivity_Title);67 } else {68 setTitle(R.string.ApplicationName);69 }70 }71 });72 }73 74 /**75 * 配置文件改变的方法76 */77 @Override78 public void onConfigurationChanged(Configuration newConfig) {79 super.onConfigurationChanged(newConfig);80 }81 }
我们可以总结出来以下几个结果:
①在oncreate这个方法中,能否全频,能否有标题,这些设置的配置的参数就是存在与preference这个参数中的,可以sharedpreference这个对象保存配置参数,这也是android官方推荐的一种方式。
②一看这个布局的方式,这是一个典型tab布局,因此我们要实例化相应tabhost文件,并且为相应tabhost赋值事件,进入不同的intent,并且进入不同的界面后,赋值相应的标题。
③将相应的配置文件改变以后的,父类的配置文件也进行相应的改变。
由于要跳往不同的界面,所以就需要两个文件控制。
首先看一下 BookmarksListActivity中的控制文件,相应的源代码如下:
1 public class BookmarksListActivity extends Activity { 2 3 private static final int MENU_ADD_BOOKMARK = Menu.FIRST; 4 private static final int MENU_SORT_MODE = Menu.FIRST + 1; 5 6 private static final int MENU_OPEN_IN_TAB = Menu.FIRST + 10; 7 private static final int MENU_COPY_URL = Menu.FIRST + 11; 8 private static final int MENU_SHARE = Menu.FIRST + 12; 9 private static final int MENU_EDIT_BOOKMARK = Menu.FIRST + 13; 10 private static final int MENU_DELETE_BOOKMARK = Menu.FIRST + 14; 11 12 private static final int ACTIVITY_ADD_BOOKMARK = 0; 13 private static final int ACTIVITY_EDIT_BOOKMARK = 1; 14 15 private Cursor mCursor; 16 private BookmarksCursorAdapter mCursorAdapter; 17 18 private ListView mList; 19 20 @Override 21 public void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.bookmarks_list_activity); 24 25 setTitle(R.string.BookmarksListActivity_Title); 26 27 View emptyView = findViewById(R.id.BookmarksListActivity_EmptyTextView); 28 mList = (ListView) findViewById(R.id.BookmarksListActivity_List); 29 30 mList.setEmptyView(emptyView); 31 32 mList.setOnItemClickListener(new OnItemClickListener() { 33 34 @Override 35 public void onItemClick(AdapterView l, View v, int position, long id) { 36 Intent result = new Intent(); 37 result.putExtra(Constants.EXTRA_ID_NEW_TAB, false); 38 39 BookmarkItem item = BookmarksProviderWrapper.getStockBookmarkById(getContentResolver(), id); 40 if (item != null) { 41 result.putExtra(Constants.EXTRA_ID_URL, item.getUrl()); 42 } else { 43 result.putExtra(Constants.EXTRA_ID_URL, 44 PreferenceManager.getDefaultSharedPreferences(BookmarksListActivity.this).getString(Constants.PREFERENCES_GENERAL_HOME_PAGE, Constants.URL_ABOUT_START)); 45 } 46 47 if (getParent() != null) { 48 getParent().setResult(RESULT_OK, result); 49 } else { 50 setResult(RESULT_OK, result); 51 } 52 53 finish(); 54 } 55 }); 56 57 registerForContextMenu(mList); 58 59 fillData(); 60 } 61 62 @Override 63 protected void onDestroy() { 64 mCursor.close(); 65 super.onDestroy(); 66 } 67 68 /** 69 * Fill the bookmark to the list UI. 70 */ 71 private void fillData() { 72 mCursor = BookmarksProviderWrapper.getStockBookmarks(getContentResolver(), 73 PreferenceManager.getDefaultSharedPreferences(this).getInt(Constants.PREFERENCES_BOOKMARKS_SORT_MODE, 0)); 74 startManagingCursor(mCursor); 75 76 String[] from = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL}; 77 int[] to = new int[] {R.id.BookmarkRow_Title, R.id.BookmarkRow_Url}; 78 79 mCursorAdapter = new BookmarksCursorAdapter(this, 80 R.layout.bookmark_row, 81 mCursor, 82 from, 83 to, 84 ApplicationUtils.getFaviconSizeForBookmarks(this)); 85 86 mList.setAdapter(mCursorAdapter); 87 88 setAnimation(); 89 } 90 91 /** 92 * Set the list loading animation. 93 */ 94 private void setAnimation() { 95 AnimationSet set = new AnimationSet(true); 96 97 Animation animation = new AlphaAnimation(0.0f, 1.0f); 98 animation.setDuration(100); 99 set.addAnimation(animation);100 101 animation = new TranslateAnimation(102 Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,103 Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f104 );105 animation.setDuration(100);106 set.addAnimation(animation);107 108 LayoutAnimationController controller =109 new LayoutAnimationController(set, 0.5f);110 111 mList.setLayoutAnimation(controller);112 }113 114 /**115 * Display the add bookmark dialog.116 */117 private void openAddBookmarkDialog() {118 Intent i = new Intent(this, EditBookmarkActivity.class);119 120 i.putExtra(Constants.EXTRA_ID_BOOKMARK_ID, (long) -1);121 i.putExtra(Constants.EXTRA_ID_BOOKMARK_TITLE, "");122 i.putExtra(Constants.EXTRA_ID_BOOKMARK_URL, "");123 124 startActivityForResult(i, ACTIVITY_ADD_BOOKMARK);125 }126 127 @Override128 public boolean onCreateOptionsMenu(Menu menu) {129 super.onCreateOptionsMenu(menu);130 131 MenuItem item;132 item = menu.add(0, MENU_ADD_BOOKMARK, 0, R.string.BookmarksListActivity_MenuAddBookmark);133 item.setIcon(R.drawable.ic_menu_add_bookmark);134 135 item = menu.add(0, MENU_SORT_MODE, 0, R.string.BookmarksListActivity_MenuSortMode);136 item.setIcon(R.drawable.ic_menu_sort); 137 138 return true;139 }140 141 @Override142 public boolean onMenuItemSelected(int featureId, MenuItem item) {143 144 switch(item.getItemId()) {145 case MENU_SORT_MODE:146 changeSortMode();147 return true;148 149 case MENU_ADD_BOOKMARK: 150 openAddBookmarkDialog();151 return true;152 153 default: return super.onMenuItemSelected(featureId, item);154 }155 }156 157 @Override158 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {159 super.onCreateContextMenu(menu, v, menuInfo);160 161 long id = ((AdapterContextMenuInfo) menuInfo).id;162 if (id != -1) {163 BookmarkItem item = BookmarksProviderWrapper.getStockBookmarkById(getContentResolver(), id);164 if (item != null) {165 menu.setHeaderTitle(item.getTitle());166 }167 }168 169 menu.add(0, MENU_OPEN_IN_TAB, 0, R.string.BookmarksListActivity_MenuOpenInTab); 170 menu.add(0, MENU_COPY_URL, 0, R.string.BookmarksHistoryActivity_MenuCopyLinkUrl);171 menu.add(0, MENU_SHARE, 0, R.string.Main_MenuShareLinkUrl);172 menu.add(0, MENU_EDIT_BOOKMARK, 0, R.string.BookmarksListActivity_MenuEditBookmark);173 menu.add(0, MENU_DELETE_BOOKMARK, 0, R.string.BookmarksListActivity_MenuDeleteBookmark);174 }175 176 @Override177 public boolean onContextItemSelected(MenuItem item) {178 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();179 180 Intent i;181 BookmarkItem bookmarkItem = BookmarksProviderWrapper.getStockBookmarkById(getContentResolver(), info.id);182 183 switch (item.getItemId()) {184 case MENU_OPEN_IN_TAB: 185 i = new Intent();186 i.putExtra(Constants.EXTRA_ID_NEW_TAB, true);187 188 if (bookmarkItem != null) {189 i.putExtra(Constants.EXTRA_ID_URL, bookmarkItem.getUrl());190 } else {191 i.putExtra(Constants.EXTRA_ID_URL,192 PreferenceManager.getDefaultSharedPreferences(BookmarksListActivity.this).getString(Constants.PREFERENCES_GENERAL_HOME_PAGE, Constants.URL_ABOUT_START));193 }194 195 if (getParent() != null) {196 getParent().setResult(RESULT_OK, i);197 } else {198 setResult(RESULT_OK, i); 199 }200 201 finish();202 return true;203 204 case MENU_EDIT_BOOKMARK: 205 if (bookmarkItem != null) {206 i = new Intent(this, EditBookmarkActivity.class);207 i.putExtra(Constants.EXTRA_ID_BOOKMARK_ID, info.id);208 i.putExtra(Constants.EXTRA_ID_BOOKMARK_TITLE, bookmarkItem.getTitle());209 i.putExtra(Constants.EXTRA_ID_BOOKMARK_URL, bookmarkItem.getUrl());210 211 startActivityForResult(i, ACTIVITY_EDIT_BOOKMARK);212 }213 return true;214 215 case MENU_COPY_URL:216 if (bookmarkItem != null) {217 ApplicationUtils.copyTextToClipboard(this, bookmarkItem.getUrl(), getString(R.string.Commons_UrlCopyToastMessage));218 }219 return true;220 221 case MENU_SHARE:222 if (bookmarkItem != null) {223 ApplicationUtils.sharePage(this, bookmarkItem.getTitle(), bookmarkItem.getUrl());224 }225 return true;226 227 case MENU_DELETE_BOOKMARK:228 //mDbAdapter.deleteBookmark(info.id);229 BookmarksProviderWrapper.deleteStockBookmark(getContentResolver(), info.id);230 fillData();231 return true;232 default: return super.onContextItemSelected(item);233 }234 }235 236 /**237 * Change list sort mode. Update list.238 * @param sortMode The new sort mode.239 */240 private void doChangeSortMode(int sortMode) {241 Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();242 editor.putInt(Constants.PREFERENCES_BOOKMARKS_SORT_MODE, sortMode);243 editor.commit();244 245 fillData();246 }247 248 /**249 * Show a dialog for choosing the sort mode.250 * Perform the change if required.251 */252 private void changeSortMode() {253 254 int currentSort = PreferenceManager.getDefaultSharedPreferences(this).getInt(Constants.PREFERENCES_BOOKMARKS_SORT_MODE, 0);255 256 AlertDialog.Builder builder = new AlertDialog.Builder(this);257 builder.setInverseBackgroundForced(true);258 builder.setIcon(android.R.drawable.ic_dialog_info);259 builder.setTitle(getResources().getString(R.string.BookmarksListActivity_MenuSortMode));260 builder.setSingleChoiceItems(new String[] {getResources().getString(R.string.BookmarksListActivity_MostUsedSortMode),261 getResources().getString(R.string.BookmarksListActivity_AlphaSortMode),262 getResources().getString(R.string.BookmarksListActivity_RecentSortMode) },263 currentSort,264 new OnClickListener() {265 @Override266 public void onClick(DialogInterface dialog, int which) {267 doChangeSortMode(which);268 dialog.dismiss(); 269 } 270 }); 271 builder.setCancelable(true);272 builder.setNegativeButton(R.string.Commons_Cancel, null);273 274 AlertDialog alert = builder.create();275 alert.show();276 }277 278 @Override279 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {280 super.onActivityResult(requestCode, resultCode, intent);281 282 switch (requestCode) {283 case ACTIVITY_EDIT_BOOKMARK:284 if (resultCode == RESULT_OK) {285 fillData();286 }287 break;288 case ACTIVITY_ADD_BOOKMARK:289 if (resultCode == RESULT_OK) {290 fillData();291 }292 break;293 294 default:295 break;296 }297 }298 299 }
①同理在oncreate方法,实例化相应的listview,把从ContentResolver取得的网址信息,添加到相应的listview中去,并且为listview赋值与每项目的点击事件,每项的点击跳转到相应的界面上去。同时结束当前的界面。
②在方法中实现数据的填充,同时还为列表绑定上下文菜单。
③程序也是一环扣一环的,那么这个fillData方法是加载数据的方法,又是如何实现的了,把数据库存标签的数据按照一定方式(分页的方式)读取出来,并且加载到相应的列表上去。这个加载数据的过程中有一个淡入淡出动画的效果。
④你要添加这个标签对话框也是在此界面上完成这个功能的,我们用了回调结果intent来跳转到相应编辑标签的界面,编辑标签以后,在到回调结果方法中重新进行相应数据的重新初始化。
⑤这个文件大量的篇幅用于一些上下文菜单的处理,分别对相应在tab打开网页菜单,编辑标签的菜单,复制URL的菜单,分享的菜单,删除菜单的事件做一个处理。
接下来,是一个HistoryListActivity源代码控制文件的解析。源代码如下:
/** * history list activity. */public class HistoryListActivity extends ExpandableListActivity { private static final int MENU_CLEAR_HISTORY = Menu.FIRST; private static final int MENU_OPEN_IN_TAB = Menu.FIRST + 10; private static final int MENU_COPY_URL = Menu.FIRST + 11; private static final int MENU_SHARE = Menu.FIRST + 12; private static final int MENU_DELETE_FROM_HISTORY = Menu.FIRST + 13; private ExpandableListAdapter mAdapter; private ProgressDialog mProgressDialog; private OnCheckedChangeListener mBookmarkStarChangeListener; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle(R.string.HistoryListActivity_Title); registerForContextMenu(getExpandableListView()); mBookmarkStarChangeListener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { long id = (Long) buttonView.getTag(); BookmarksProviderWrapper.toggleBookmark(getContentResolver(), id, isChecked); if (isChecked) { Toast.makeText(HistoryListActivity.this, R.string.HistoryListActivity_BookmarkAdded, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(HistoryListActivity.this, R.string.HistoryListActivity_BookmarkRemoved, Toast.LENGTH_SHORT).show(); } } }; fillData(); } /** * Fill the history list. */ private void fillData() { Cursor c = BookmarksProviderWrapper.getStockHistory(getContentResolver()); mAdapter = new HistoryExpandableListAdapter( this, mBookmarkStarChangeListener, c, c.getColumnIndex(Browser.BookmarkColumns.DATE), ApplicationUtils.getFaviconSizeForBookmarks(this)); setListAdapter(mAdapter); if (getExpandableListAdapter().getGroupCount() > 0) { getExpandableListView().expandGroup(0); } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo; int type = ExpandableListView.getPackedPositionType(info.packedPosition); int group = ExpandableListView.getPackedPositionGroup(info.packedPosition); int child = ExpandableListView.getPackedPositionChild(info.packedPosition); if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { HistoryItem item = (HistoryItem) getExpandableListAdapter().getChild(group, child); menu.setHeaderTitle(item.getTitle()); menu.add(0, MENU_OPEN_IN_TAB, 0, R.string.HistoryListActivity_MenuOpenInTab); menu.add(0, MENU_COPY_URL, 0, R.string.BookmarksHistoryActivity_MenuCopyLinkUrl); menu.add(0, MENU_SHARE, 0, R.string.Main_MenuShareLinkUrl); menu.add(0, MENU_DELETE_FROM_HISTORY, 0, R.string.HistoryListActivity_MenuDelete); } } @Override public boolean onContextItemSelected(MenuItem menuItem) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuItem.getMenuInfo(); int type = ExpandableListView.getPackedPositionType(info.packedPosition); if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { int group = ExpandableListView.getPackedPositionGroup(info.packedPosition); int child = ExpandableListView.getPackedPositionChild(info.packedPosition); HistoryItem item = (HistoryItem) getExpandableListAdapter().getChild(group, child); switch (menuItem.getItemId()) { case MENU_OPEN_IN_TAB: doNavigateToUrl(item.getUrl(), true); break; case MENU_COPY_URL: ApplicationUtils.copyTextToClipboard(this, item.getUrl(), getString(R.string.Commons_UrlCopyToastMessage)); break; case MENU_SHARE: ApplicationUtils.sharePage(this, item.getTitle(), item.getUrl()); break; case MENU_DELETE_FROM_HISTORY: BookmarksProviderWrapper.deleteHistoryRecord(getContentResolver(), item.getId()); fillData(); break; default: break; } } return super.onContextItemSelected(menuItem); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuItem item; item = menu.add(0, MENU_CLEAR_HISTORY, 0, R.string.Commons_ClearHistory); item.setIcon(R.drawable.ic_menu_delete); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch(item.getItemId()) { case MENU_CLEAR_HISTORY: clearHistory(); return true; default: return super.onMenuItemSelected(featureId, item); } } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { HistoryItem item = (HistoryItem) getExpandableListAdapter().getChild(groupPosition, childPosition); doNavigateToUrl(item.getUrl(), false); return super.onChildClick(parent, v, groupPosition, childPosition, id); } /** * Load the given url. * @param url The url. * @param newTab If True, will open a new tab. If False, the current tab is used. */ private void doNavigateToUrl(String url, boolean newTab) { Intent result = new Intent(); result.putExtra(Constants.EXTRA_ID_NEW_TAB, newTab); result.putExtra(Constants.EXTRA_ID_URL, url); if (getParent() != null) { getParent().setResult(RESULT_OK, result); } else { setResult(RESULT_OK, result); } finish(); } /** * Clear history. */ private void doClearHistory() { mProgressDialog = ProgressDialog.show(this, this.getResources().getString(R.string.Commons_PleaseWait), this.getResources().getString(R.string.Commons_ClearingHistory)); new HistoryClearer(); } /** * Display confirmation and clear history. */ private void clearHistory() { ApplicationUtils.showYesNoDialog(this, android.R.drawable.ic_dialog_alert, R.string.Commons_ClearHistory, R.string.Commons_NoUndoMessage, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); doClearHistory(); } }); } /** * Runnable to clear history. */ private class HistoryClearer implements Runnable { /** * Constructor. */ public HistoryClearer() { new Thread(this).start(); } @Override public void run() { BookmarksProviderWrapper.clearHistoryAndOrBookmarks(getContentResolver(), true, false); for (CustomWebView webView : Controller.getInstance().getWebViewList()) { webView.clearHistory(); } handler.sendEmptyMessage(0); } private Handler handler = new Handler() { public void handleMessage(Message msg) { mProgressDialog.dismiss(); fillData(); } }; } }
观察相应的源代码了,我们可想而知其大体与标签列表界面的大体一样,上面说过方法就不做过多的赘述了,这里我们只需要阐述了为什么要HistoryClearer这个开启线程来清空历史记录。 首先原因有二:
①由于打开网页是很多很多,这里面数据还是很多很多。
②正视由于数据量大,这是一个耗时操作,如果放在一个ui线程中势必会造成页面假使,极大的影响了用户体验。开启一个线程,使其在回调后,更新界面,符合提升用户体验的原则。
我们再这里可以得出来这样android开发的原则:凡是耗时操作,应该开启多线程,再相应线程回调完成后再更新界面。
这就是我对标签记录页面总结,和一点点总结。
转载地址:http://mvqoo.baihongyu.com/