Switching to recycler view
This commit is contained in:
parent
66b4ea16f4
commit
510e7fd7a0
@ -11,6 +11,8 @@ import android.support.annotation.NonNull;
|
|||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.view.ViewCompat;
|
import android.support.v4.view.ViewCompat;
|
||||||
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@ -18,13 +20,8 @@ import android.view.animation.AccelerateInterpolator;
|
|||||||
import android.view.animation.Animation;
|
import android.view.animation.Animation;
|
||||||
import android.view.animation.DecelerateInterpolator;
|
import android.view.animation.DecelerateInterpolator;
|
||||||
import android.view.animation.Transformation;
|
import android.view.animation.Transformation;
|
||||||
import android.widget.AdapterView;
|
|
||||||
import android.widget.AdapterView.OnItemClickListener;
|
|
||||||
import android.widget.AdapterView.OnItemLongClickListener;
|
|
||||||
import android.widget.ArrayAdapter;
|
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.ListView;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.anthonycr.bonsai.Schedulers;
|
import com.anthonycr.bonsai.Schedulers;
|
||||||
@ -52,6 +49,7 @@ import acr.browser.lightning.database.HistoryItem;
|
|||||||
import acr.browser.lightning.dialog.LightningDialogBuilder;
|
import acr.browser.lightning.dialog.LightningDialogBuilder;
|
||||||
import acr.browser.lightning.favicon.FaviconModel;
|
import acr.browser.lightning.favicon.FaviconModel;
|
||||||
import acr.browser.lightning.preference.PreferenceManager;
|
import acr.browser.lightning.preference.PreferenceManager;
|
||||||
|
import acr.browser.lightning.utils.Preconditions;
|
||||||
import acr.browser.lightning.utils.ThemeUtils;
|
import acr.browser.lightning.utils.ThemeUtils;
|
||||||
import acr.browser.lightning.view.LightningView;
|
import acr.browser.lightning.view.LightningView;
|
||||||
import butterknife.BindView;
|
import butterknife.BindView;
|
||||||
@ -89,7 +87,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
private UIController mUiController;
|
private UIController mUiController;
|
||||||
|
|
||||||
// Adapter
|
// Adapter
|
||||||
private BookmarkViewAdapter mBookmarkAdapter;
|
private BookmarkListAdapter mBookmarkAdapter;
|
||||||
|
|
||||||
// Preloaded images
|
// Preloaded images
|
||||||
private Bitmap mWebpageBitmap, mFolderBitmap;
|
private Bitmap mWebpageBitmap, mFolderBitmap;
|
||||||
@ -98,7 +96,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
private final List<HistoryItem> mBookmarks = new ArrayList<>();
|
private final List<HistoryItem> mBookmarks = new ArrayList<>();
|
||||||
|
|
||||||
// Views
|
// Views
|
||||||
@BindView(R.id.right_drawer_list) ListView mBookmarksListView;
|
@BindView(R.id.right_drawer_list) RecyclerView mBookmarksListView;
|
||||||
@BindView(R.id.starIcon) ImageView mBookmarkTitleImage;
|
@BindView(R.id.starIcon) ImageView mBookmarkTitleImage;
|
||||||
@BindView(R.id.icon_star) ImageView mBookmarkImage;
|
@BindView(R.id.icon_star) ImageView mBookmarkImage;
|
||||||
|
|
||||||
@ -113,17 +111,13 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
@Nullable
|
@Nullable
|
||||||
private Subscription mBookmarksSubscription;
|
private Subscription mBookmarksSubscription;
|
||||||
|
|
||||||
private Single<BookmarkViewAdapter> initBookmarkManager() {
|
private static Single<List<HistoryItem>> initBookmarks(@NonNull final BookmarkManager bookmarkManager) {
|
||||||
return Single.create(new SingleAction<BookmarkViewAdapter>() {
|
return Single.create(new SingleAction<List<HistoryItem>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSubscribe(@NonNull SingleSubscriber<BookmarkViewAdapter> subscriber) {
|
public void onSubscribe(@NonNull SingleSubscriber<List<HistoryItem>> subscriber) {
|
||||||
Context context = getContext();
|
subscriber.onItem(bookmarkManager.getBookmarksFromFolder(null, true));
|
||||||
if (context != null) {
|
|
||||||
mBookmarkAdapter = new BookmarkViewAdapter(context, mBookmarks);
|
|
||||||
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false);
|
|
||||||
subscriber.onItem(mBookmarkAdapter);
|
|
||||||
}
|
|
||||||
subscriber.onComplete();
|
subscriber.onComplete();
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -154,10 +148,10 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
// Handle bookmark click
|
// Handle bookmark click
|
||||||
private final OnItemClickListener mItemClickListener = new OnItemClickListener() {
|
private final OnItemClickListener mItemClickListener = new OnItemClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemClick(int position) {
|
||||||
final HistoryItem item = mBookmarks.get(position);
|
final HistoryItem item = mBookmarks.get(position);
|
||||||
if (item.isFolder()) {
|
if (item.isFolder()) {
|
||||||
mScrollIndex = mBookmarksListView.getFirstVisiblePosition();
|
mScrollIndex = ((LinearLayoutManager) mBookmarksListView.getLayoutManager()).findFirstVisibleItemPosition();
|
||||||
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true), true);
|
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(item.getTitle(), true), true);
|
||||||
} else {
|
} else {
|
||||||
mUiController.bookmarkItemClicked(item);
|
mUiController.bookmarkItemClicked(item);
|
||||||
@ -167,7 +161,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
|
|
||||||
private final OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {
|
private final OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
|
public boolean onItemLongClick(int position) {
|
||||||
final HistoryItem item = mBookmarks.get(position);
|
final HistoryItem item = mBookmarks.get(position);
|
||||||
handleLongPress(item);
|
handleLongPress(item);
|
||||||
return true;
|
return true;
|
||||||
@ -187,8 +181,6 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
final View view = inflater.inflate(R.layout.bookmark_drawer, container, false);
|
final View view = inflater.inflate(R.layout.bookmark_drawer, container, false);
|
||||||
mUnbinder = ButterKnife.bind(this, view);
|
mUnbinder = ButterKnife.bind(this, view);
|
||||||
mBookmarksListView.setOnItemClickListener(mItemClickListener);
|
|
||||||
mBookmarksListView.setOnItemLongClickListener(mItemLongClickListener);
|
|
||||||
mBookmarkTitleImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN);
|
mBookmarkTitleImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN);
|
||||||
final View backView = view.findViewById(R.id.bookmark_back_button);
|
final View backView = view.findViewById(R.id.bookmark_back_button);
|
||||||
backView.setOnClickListener(new View.OnClickListener() {
|
backView.setOnClickListener(new View.OnClickListener() {
|
||||||
@ -197,7 +189,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
if (mBookmarkManager == null) return;
|
if (mBookmarkManager == null) return;
|
||||||
if (!mBookmarkManager.isRootFolder()) {
|
if (!mBookmarkManager.isRootFolder()) {
|
||||||
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true);
|
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true);
|
||||||
mBookmarksListView.setSelection(mScrollIndex);
|
mBookmarksListView.getLayoutManager().scrollToPosition(mScrollIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -205,15 +197,24 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
setupNavigationButton(view, R.id.action_reading, R.id.icon_reading);
|
setupNavigationButton(view, R.id.action_reading, R.id.icon_reading);
|
||||||
setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop);
|
setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop);
|
||||||
|
|
||||||
mBookmarksSubscription = initBookmarkManager().subscribeOn(Schedulers.io())
|
mBookmarkAdapter = new BookmarkListAdapter(mBookmarks, mFaviconModel, mFolderBitmap, mWebpageBitmap);
|
||||||
|
mBookmarkAdapter.setOnItemClickListener(mItemClickListener);
|
||||||
|
mBookmarkAdapter.setOnItemLongClickListener(mItemLongClickListener);
|
||||||
|
mBookmarksListView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
|
mBookmarksListView.setAdapter(mBookmarkAdapter);
|
||||||
|
|
||||||
|
mBookmarksSubscription = initBookmarks(mBookmarkManager)
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(Schedulers.main())
|
.observeOn(Schedulers.main())
|
||||||
.subscribe(new SingleOnSubscribe<BookmarkViewAdapter>() {
|
.subscribe(new SingleOnSubscribe<List<HistoryItem>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onItem(@Nullable BookmarkViewAdapter item) {
|
public void onItem(@Nullable List<HistoryItem> item) {
|
||||||
mBookmarksSubscription = null;
|
mBookmarksSubscription = null;
|
||||||
mBookmarksListView.setAdapter(mBookmarkAdapter);
|
Preconditions.checkNonNull(item);
|
||||||
|
setBookmarkDataSet(item, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -385,7 +386,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
mUiController.closeBookmarksDrawer();
|
mUiController.closeBookmarksDrawer();
|
||||||
} else {
|
} else {
|
||||||
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true);
|
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), true);
|
||||||
mBookmarksListView.setSelection(mScrollIndex);
|
mBookmarksListView.getLayoutManager().scrollToPosition(mScrollIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,34 +397,87 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(folder, true), false);
|
setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(folder, true), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BookmarkViewAdapter extends ArrayAdapter<HistoryItem> {
|
static class BookmarkViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
||||||
|
@BindView(R.id.textBookmark) TextView txtTitle;
|
||||||
|
@BindView(R.id.faviconBookmark) ImageView favicon;
|
||||||
|
|
||||||
final Context context;
|
@Nullable private final OnItemLongClickListener onItemLongClickListener;
|
||||||
|
@Nullable private final OnItemClickListener onItemClickListener;
|
||||||
|
|
||||||
BookmarkViewAdapter(Context context, @NonNull List<HistoryItem> data) {
|
BookmarkViewHolder(@NonNull View itemView,
|
||||||
super(context, R.layout.bookmark_list_item, data);
|
@Nullable OnItemLongClickListener onItemLongClickListener,
|
||||||
this.context = context;
|
@Nullable OnItemClickListener onItemClickListener) {
|
||||||
|
super(itemView);
|
||||||
|
ButterKnife.bind(this, itemView);
|
||||||
|
|
||||||
|
this.onItemClickListener = onItemClickListener;
|
||||||
|
this.onItemLongClickListener = onItemLongClickListener;
|
||||||
|
|
||||||
|
itemView.setOnLongClickListener(this);
|
||||||
|
itemView.setOnClickListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
@Override
|
||||||
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
|
public void onClick(View v) {
|
||||||
View row = convertView;
|
if (onItemClickListener != null) {
|
||||||
BookmarkViewHolder holder;
|
onItemClickListener.onItemClick(getAdapterPosition());
|
||||||
|
|
||||||
if (row == null) {
|
|
||||||
LayoutInflater inflater = LayoutInflater.from(context);
|
|
||||||
row = inflater.inflate(R.layout.bookmark_list_item, parent, false);
|
|
||||||
|
|
||||||
holder = new BookmarkViewHolder();
|
|
||||||
holder.txtTitle = (TextView) row.findViewById(R.id.textBookmark);
|
|
||||||
holder.favicon = (ImageView) row.findViewById(R.id.faviconBookmark);
|
|
||||||
row.setTag(holder);
|
|
||||||
} else {
|
|
||||||
holder = (BookmarkViewHolder) row.getTag();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ViewCompat.jumpDrawablesToCurrentState(row);
|
@Override
|
||||||
|
public boolean onLongClick(View v) {
|
||||||
|
return onItemLongClickListener != null &&
|
||||||
|
onItemLongClickListener.onItemLongClick(getAdapterPosition());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OnItemLongClickListener {
|
||||||
|
boolean onItemLongClick(int position);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OnItemClickListener {
|
||||||
|
void onItemClick(int position);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class BookmarkListAdapter extends RecyclerView.Adapter<BookmarkViewHolder> {
|
||||||
|
|
||||||
|
@NonNull private final List<HistoryItem> mBookmarks;
|
||||||
|
@NonNull private final FaviconModel mFaviconModel;
|
||||||
|
@NonNull private final Bitmap mFolderBitmap;
|
||||||
|
@NonNull private final Bitmap mWebpageBitmap;
|
||||||
|
|
||||||
|
@Nullable private OnItemLongClickListener mOnItemLongCLickListener;
|
||||||
|
@Nullable private OnItemClickListener mOnItemClickListener;
|
||||||
|
|
||||||
|
BookmarkListAdapter(@NonNull List<HistoryItem> bookmarks,
|
||||||
|
@NonNull FaviconModel faviconModel,
|
||||||
|
@NonNull Bitmap folderBitmap,
|
||||||
|
@NonNull Bitmap webpageBitmap) {
|
||||||
|
mBookmarks = bookmarks;
|
||||||
|
mFaviconModel = faviconModel;
|
||||||
|
mFolderBitmap = folderBitmap;
|
||||||
|
mWebpageBitmap = webpageBitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setOnItemLongClickListener(@Nullable OnItemLongClickListener listener) {
|
||||||
|
mOnItemLongCLickListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setOnItemClickListener(@Nullable OnItemClickListener listener) {
|
||||||
|
mOnItemClickListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BookmarkViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||||
|
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
|
||||||
|
View itemView = inflater.inflate(R.layout.bookmark_list_item, parent, false);
|
||||||
|
|
||||||
|
return new BookmarkViewHolder(itemView, mOnItemLongCLickListener, mOnItemClickListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(BookmarkViewHolder holder, int position) {
|
||||||
|
ViewCompat.jumpDrawablesToCurrentState(holder.itemView);
|
||||||
|
|
||||||
final HistoryItem web = mBookmarks.get(position);
|
final HistoryItem web = mBookmarks.get(position);
|
||||||
holder.txtTitle.setText(web.getTitle());
|
holder.txtTitle.setText(web.getTitle());
|
||||||
@ -454,12 +508,12 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
|||||||
} else {
|
} else {
|
||||||
holder.favicon.setImageBitmap(web.getBitmap());
|
holder.favicon.setImageBitmap(web.getBitmap());
|
||||||
}
|
}
|
||||||
return row;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class BookmarkViewHolder {
|
@Override
|
||||||
TextView txtTitle;
|
public int getItemCount() {
|
||||||
ImageView favicon;
|
return mBookmarks.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@
|
|||||||
android:textAppearance="?android:attr/textAppearanceLarge"/>
|
android:textAppearance="?android:attr/textAppearanceLarge"/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<ListView
|
<android.support.v7.widget.RecyclerView
|
||||||
android:id="@+id/right_drawer_list"
|
android:id="@+id/right_drawer_list"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user