Bug 902038: MultiTypeCursorAdapter for history, bookmarks and browser search adapters. [r=lucasr]

This commit is contained in:
Sriram Ramasubramanian 2013-08-07 08:50:52 -07:00
Родитель 9bd725980c
Коммит 399e36faf6
3 изменённых файлов: 54 добавлений и 151 удалений

Просмотреть файл

@ -11,22 +11,20 @@ import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.LinkedList;
/**
* Adapter to back the BookmarksListView with a list of bookmarks.
*/
class BookmarksListAdapter extends CursorAdapter {
class BookmarksListAdapter extends MultiTypeCursorAdapter {
private static final int VIEW_TYPE_ITEM = 0;
private static final int VIEW_TYPE_FOLDER = 1;
private static final int VIEW_TYPE_COUNT = 2;
private static final int[] VIEW_TYPES = new int[] { VIEW_TYPE_ITEM, VIEW_TYPE_FOLDER };
private static final int[] LAYOUT_TYPES = new int[] { R.layout.home_item_row, R.layout.bookmark_folder_row };
// A listener that knows how to refresh the list for a given folder id.
// This is usually implemented by the enclosing fragment/activity.
@ -44,7 +42,7 @@ class BookmarksListAdapter extends CursorAdapter {
public BookmarksListAdapter(Context context, Cursor cursor) {
// Initializing with a null cursor.
super(context, cursor);
super(context, cursor, VIEW_TYPES, LAYOUT_TYPES);
mParentStack = new LinkedList<Pair<Integer, String>>();
@ -104,23 +102,8 @@ class BookmarksListAdapter extends CursorAdapter {
position--;
}
Cursor c = getCursor();
if (!c.moveToPosition(position)) {
throw new IllegalStateException("Couldn't move cursor to position " + position);
}
return getItemViewType(c);
}
/**
* Returns the type of the item at the given position in the cursor.
*
* @param cursor A cursor moved to the required position.
* @return The type of the item.
*/
public int getItemViewType(Cursor cursor) {
if (cursor.getInt(cursor.getColumnIndexOrThrow(Bookmarks.TYPE)) == Bookmarks.TYPE_FOLDER) {
final Cursor c = getCursor(position);
if (c.getInt(c.getColumnIndexOrThrow(Bookmarks.TYPE)) == Bookmarks.TYPE_FOLDER) {
return VIEW_TYPE_FOLDER;
}
@ -128,11 +111,6 @@ class BookmarksListAdapter extends CursorAdapter {
return VIEW_TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return VIEW_TYPE_COUNT;
}
/**
* Get the title of the folder given a cursor moved to the position.
*
@ -179,48 +157,34 @@ class BookmarksListAdapter extends CursorAdapter {
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// The position also reflects the opened child folder row.
public void bindView(View view, Context context, int position) {
final int viewType = getItemViewType(position);
final Cursor cursor;
if (isShowingChildFolder()) {
if (position == 0) {
BookmarkFolderView folder = (BookmarkFolderView) LayoutInflater.from(parent.getContext()).inflate(R.layout.bookmark_folder_row, null);
folder.setText(mParentStack.peek().second);
folder.open();
return folder;
cursor = null;
} else {
// Accounting for the folder view.
position--;
cursor = getCursor(position);
}
// Accounting for the folder view.
position--;
} else {
cursor = getCursor(position);
}
return super.getView(position, convertView, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final int viewType = getItemViewType(cursor);
if (viewType == VIEW_TYPE_ITEM) {
TwoLinePageRow row = (TwoLinePageRow) view;
final TwoLinePageRow row = (TwoLinePageRow) view;
row.updateFromCursor(cursor);
} else {
BookmarkFolderView row = (BookmarkFolderView) view;
row.setText(getFolderTitle(context, cursor));
row.close();
final BookmarkFolderView row = (BookmarkFolderView) view;
if (cursor == null) {
row.setText(mParentStack.peek().second);
row.open();
} else {
row.setText(getFolderTitle(context, cursor));
row.close();
}
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final int viewType = getItemViewType(cursor);
final int resId;
if (viewType == VIEW_TYPE_ITEM) {
resId = R.layout.home_item_row;
} else {
resId = R.layout.bookmark_folder_row;
}
return LayoutInflater.from(parent.getContext()).inflate(resId, null);
}
}

Просмотреть файл

@ -34,7 +34,6 @@ import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
@ -229,12 +228,7 @@ public class BrowserSearch extends HomeFragment
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Account for the search engines
position -= getSuggestEngineCount();
final Cursor c = mAdapter.getCursor();
if (c == null || !c.moveToPosition(position)) {
return;
}
final Cursor c = mAdapter.getCursor(position);
final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
mUrlOpenListener.onUrlOpen(url);
}
@ -630,15 +624,18 @@ public class BrowserSearch extends HomeFragment
}
}
private class SearchAdapter extends SimpleCursorAdapter {
private class SearchAdapter extends MultiTypeCursorAdapter {
private static final int ROW_SEARCH = 0;
private static final int ROW_STANDARD = 1;
private static final int ROW_SUGGEST = 2;
private static final int ROW_TYPE_COUNT = 3;
public SearchAdapter(Context context) {
super(context, -1, null, new String[] {}, new int[] {});
super(context, null, new int[] { ROW_STANDARD,
ROW_SEARCH,
ROW_SUGGEST },
new int[] { R.layout.home_item_row,
R.layout.home_search_item_row,
R.layout.home_search_item_row });
}
@Override
@ -658,13 +655,6 @@ public class BrowserSearch extends HomeFragment
return ROW_SEARCH;
}
@Override
public int getViewTypeCount() {
// view can be either a standard awesomebar row, a search engine
// row, or a suggestion row
return ROW_TYPE_COUNT;
}
@Override
public boolean isEnabled(int position) {
// If we're using a gamepad or keyboard, allow the row to be
@ -699,20 +689,14 @@ public class BrowserSearch extends HomeFragment
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
public void bindView(View view, Context context, int position) {
final int type = getItemViewType(position);
if (type == ROW_SEARCH || type == ROW_SUGGEST) {
final SearchEngineRow row;
if (convertView == null) {
row = (SearchEngineRow) mInflater.inflate(R.layout.home_search_item_row, mList, false);
row.setOnUrlOpenListener(mUrlOpenListener);
row.setOnSearchListener(mSearchListener);
row.setOnEditSuggestionListener(mEditSuggestionListener);
} else {
row = (SearchEngineRow) convertView;
}
final SearchEngineRow row = (SearchEngineRow) view;
row.setOnUrlOpenListener(mUrlOpenListener);
row.setOnSearchListener(mSearchListener);
row.setOnEditSuggestionListener(mEditSuggestionListener);
row.setSearchTerm(mSearchTerm);
final SearchEngine engine = mSearchEngines.get(getEngineIndex(position));
@ -722,27 +706,13 @@ public class BrowserSearch extends HomeFragment
// Only animate suggestions the first time they are shown
mAnimateSuggestions = false;
}
return row;
} else {
final TwoLinePageRow row;
if (convertView == null) {
row = (TwoLinePageRow) mInflater.inflate(R.layout.home_item_row, mList, false);
} else {
row = (TwoLinePageRow) convertView;
}
// Account for the search engines
position -= getSuggestEngineCount();
final Cursor c = getCursor();
if (!c.moveToPosition(position)) {
throw new IllegalStateException("Couldn't move cursor to position " + position);
}
final Cursor c = getCursor(position);
final TwoLinePageRow row = (TwoLinePageRow) view;
row.updateFromCursor(c);
return row;
}
}
@ -872,4 +842,4 @@ public class BrowserSearch extends HomeFragment
}
}
}
}
}

Просмотреть файл

@ -18,11 +18,10 @@ import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
@ -93,12 +92,7 @@ public class MostRecentPage extends HomeFragment {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
position -= mAdapter.getMostRecentSectionsCountBefore(position);
final Cursor c = mAdapter.getCursor();
if (c == null || !c.moveToPosition(position)) {
return;
}
final Cursor c = mAdapter.getCursor(position);
final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
mUrlOpenListener.onUrlOpen(url);
}
@ -154,11 +148,12 @@ public class MostRecentPage extends HomeFragment {
}
}
private static class MostRecentAdapter extends SimpleCursorAdapter {
private static class MostRecentAdapter extends MultiTypeCursorAdapter {
private static final int ROW_HEADER = 0;
private static final int ROW_STANDARD = 1;
private static final int ROW_TYPE_COUNT = 2;
private static final int[] VIEW_TYPES = new int[] { ROW_STANDARD, ROW_HEADER };
private static final int[] LAYOUT_TYPES = new int[] { R.layout.home_item_row, R.layout.home_header_row };
// For the time sections in history
private static final long MS_PER_DAY = 86400000;
@ -178,7 +173,8 @@ public class MostRecentPage extends HomeFragment {
private final SparseArray<MostRecentSection> mMostRecentSections;
public MostRecentAdapter(Context context) {
super(context, -1, null, new String[] {}, new int[] {});
super(context, null, VIEW_TYPES, LAYOUT_TYPES);
mContext = context;
// Initialize map of history sections
@ -206,12 +202,6 @@ public class MostRecentPage extends HomeFragment {
return ROW_STANDARD;
}
@Override
public int getViewTypeCount() {
// view can be either a standard page row, or a header row
return ROW_TYPE_COUNT;
}
@Override
public boolean isEnabled(int position) {
return (getItemViewType(position) == ROW_STANDARD);
@ -231,40 +221,19 @@ public class MostRecentPage extends HomeFragment {
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
public void bindView(View view, Context context, int position) {
final int type = getItemViewType(position);
if (type == ROW_HEADER) {
final TextView row;
if (convertView == null) {
row = (TextView) LayoutInflater.from(mContext).inflate(R.layout.home_header_row, parent, false);
} else {
row = (TextView) convertView;
}
final MostRecentSection section = mMostRecentSections.get(position);
final TextView row = (TextView) view;
row.setText(getMostRecentSectionTitle(section));
return row;
} else {
final TwoLinePageRow row;
if (convertView == null) {
row = (TwoLinePageRow) LayoutInflater.from(mContext).inflate(R.layout.home_item_row, parent, false);
} else {
row = (TwoLinePageRow) convertView;
}
// Account for the search engines
// Account for the most recent section headers
position -= getMostRecentSectionsCountBefore(position);
final Cursor c = getCursor();
if (!c.moveToPosition(position)) {
throw new IllegalStateException("Couldn't move cursor to position " + position);
}
final Cursor c = getCursor(position);
final TwoLinePageRow row = (TwoLinePageRow) view;
row.updateFromCursor(c);
return row;
}
}