Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Nov 12, 2018
1 parent aba0599 commit 3d2a42a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.monke.monkeybook.view.adapter;

import com.monke.monkeybook.bean.BookShelfBean;
import com.monke.monkeybook.help.MyItemTouchHelpCallback;
import com.monke.monkeybook.view.adapter.base.OnItemClickListenerTwo;

import java.util.List;

public interface BookShelfAdapter {
MyItemTouchHelpCallback.OnItemTouchCallbackListener getItemTouchCallbackListener();

List<BookShelfBean> getBooks();

void replaceAll(List<BookShelfBean> newDataS, String bookshelfPx);

void refreshBook(String noteUrl);

void setItemClickListener(OnItemClickListenerTwo itemClickListener);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import me.grantland.widget.AutofitTextView;

public class BookShelfGridAdapter extends RecyclerView.Adapter<BookShelfGridAdapter.MyViewHolder> {
public class BookShelfGridAdapter extends RecyclerView.Adapter<BookShelfGridAdapter.MyViewHolder> implements BookShelfAdapter {

private List<BookShelfBean> books;
private OnItemClickListenerTwo itemClickListener;
Expand Down Expand Up @@ -66,10 +66,12 @@ public BookShelfGridAdapter(Activity activity) {
books = new ArrayList<>();
}

@Override
public MyItemTouchHelpCallback.OnItemTouchCallbackListener getItemTouchCallbackListener() {
return itemTouchCallbackListener;
}

@Override
public void refreshBook(String noteUrl) {
for (int i = 0; i < books.size(); i++) {
if (Objects.equals(books.get(i).getNoteUrl(), noteUrl)) {
Expand Down Expand Up @@ -149,10 +151,12 @@ public void run() {
}
}

@Override
public void setItemClickListener(OnItemClickListenerTwo itemClickListener) {
this.itemClickListener = itemClickListener;
}

@Override
public synchronized void replaceAll(List<BookShelfBean> newDataS, String bookshelfPx) {
this.bookshelfPx = bookshelfPx;
if (null != newDataS && newDataS.size() > 0) {
Expand All @@ -164,6 +168,7 @@ public synchronized void replaceAll(List<BookShelfBean> newDataS, String bookshe
notifyDataSetChanged();
}

@Override
public List<BookShelfBean> getBooks() {
return books;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@

import me.grantland.widget.AutofitTextView;

public class BookShelfListAdapter extends RecyclerView.Adapter<BookShelfListAdapter.MyViewHolder> {
public class BookShelfListAdapter extends RecyclerView.Adapter<BookShelfListAdapter.MyViewHolder> implements BookShelfAdapter {

private Activity activity;
private List<BookShelfBean> books;
private Boolean needAnim;
private OnItemClickListenerTwo itemClickListener;
private String bookshelfPx;

Expand All @@ -57,16 +56,17 @@ public boolean onMove(int srcPosition, int targetPosition) {
}
};

@Override
public MyItemTouchHelpCallback.OnItemTouchCallbackListener getItemTouchCallbackListener() {
return itemTouchCallbackListener;
}

public BookShelfListAdapter(Activity activity, boolean needAnim) {
public BookShelfListAdapter(Activity activity) {
this.activity = activity;
this.needAnim = needAnim;
books = new ArrayList<>();
}

@Override
public void refreshBook(String noteUrl) {
for (int i = 0; i < books.size(); i++) {
if (Objects.equals(books.get(i).getNoteUrl(), noteUrl)) {
Expand Down Expand Up @@ -158,10 +158,12 @@ public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("Recycl
}
}

@Override
public void setItemClickListener(OnItemClickListenerTwo itemClickListener) {
this.itemClickListener = itemClickListener;
}

@Override
public synchronized void replaceAll(List<BookShelfBean> newDataS, String bookshelfPx) {
this.bookshelfPx = bookshelfPx;
if (null != newDataS && newDataS.size() > 0) {
Expand All @@ -173,6 +175,7 @@ public synchronized void replaceAll(List<BookShelfBean> newDataS, String bookshe
notifyDataSetChanged();
}

@Override
public List<BookShelfBean> getBooks() {
return books;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.monke.monkeybook.presenter.contract.BookListContract;
import com.monke.monkeybook.view.activity.BookDetailActivity;
import com.monke.monkeybook.view.activity.ReadBookActivity;
import com.monke.monkeybook.view.adapter.BookShelfAdapter;
import com.monke.monkeybook.view.adapter.BookShelfGridAdapter;
import com.monke.monkeybook.view.adapter.BookShelfListAdapter;
import com.monke.monkeybook.view.adapter.base.OnItemClickListenerTwo;
Expand All @@ -51,14 +52,12 @@ public class BookListFragment extends MBaseFragment<BookListContract.Presenter>
RelativeLayout rlEmptyView;

private Unbinder unbinder;
private boolean viewIsList;
private String bookPx;
private boolean resumed = false;
private boolean isRecreate;
private int group;

private BookShelfGridAdapter bookShelfGridAdapter;
private BookShelfListAdapter bookShelfListAdapter;
private BookShelfAdapter bookShelfAdapter;

private CallBackValue callBackValue;

Expand Down Expand Up @@ -86,21 +85,27 @@ public void onAttach(Activity activity) {
callBackValue = (CallBackValue) getActivity();
}

@Override
protected void initData() {
bookPx = preferences.getString(getString(R.string.pk_bookshelf_px), "0");
isRecreate = callBackValue != null && callBackValue.isRecreate();
}

@Override
protected void bindView() {
super.bindView();
unbinder = ButterKnife.bind(this, view);
setUpAdapter();
if (preferences.getBoolean("bookshelfIsList", true)) {
rvBookshelf.setLayoutManager(new LinearLayoutManager(getContext()));
bookShelfAdapter = new BookShelfListAdapter(getActivity());
} else {
rvBookshelf.setLayoutManager(new GridLayoutManager(getContext(), 3));
bookShelfAdapter = new BookShelfGridAdapter(getActivity());
}
rvBookshelf.setAdapter((RecyclerView.Adapter) bookShelfAdapter);
refreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
}

@Override
protected void initData() {
viewIsList = preferences.getBoolean("bookshelfIsList", true);
bookPx = preferences.getString(getString(R.string.pk_bookshelf_px), "0");
isRecreate = callBackValue != null && callBackValue.isRecreate();
}

@Override
protected void firstRequest() {
group = preferences.getInt("bookshelfGroup", 0);
Expand Down Expand Up @@ -132,45 +137,15 @@ protected void bindEvent() {
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelpCallback);
itemTouchHelper.attachToRecyclerView(rvBookshelf);
}
if (viewIsList) {
bookShelfListAdapter.setItemClickListener(getAdapterListener());
itemTouchHelpCallback.setOnItemTouchCallbackListener(bookShelfListAdapter.getItemTouchCallbackListener());
} else {
bookShelfGridAdapter.setItemClickListener(getAdapterListener());
itemTouchHelpCallback.setOnItemTouchCallbackListener(bookShelfGridAdapter.getItemTouchCallbackListener());
}
}


private void setUpAdapter() {
if (viewIsList) {
bookShelfListAdapter = new BookShelfListAdapter(getActivity(), getNeedAnim());
rvBookshelf.setAdapter(bookShelfListAdapter);
rvBookshelf.setLayoutManager(new LinearLayoutManager(getContext()));
} else {
bookShelfGridAdapter = new BookShelfGridAdapter(getActivity());
rvBookshelf.setAdapter(bookShelfGridAdapter);
rvBookshelf.setLayoutManager(new GridLayoutManager(getContext(), 3));
}
}

private boolean getNeedAnim() {
return preferences.getBoolean(getString(R.string.pk_bookshelf_anim), false);
}

private List<BookShelfBean> getBookshelfList() {
if (viewIsList) {
return bookShelfListAdapter.getBooks();
} else {
return bookShelfGridAdapter.getBooks();
}
bookShelfAdapter.setItemClickListener(getAdapterListener());
itemTouchHelpCallback.setOnItemTouchCallbackListener(bookShelfAdapter.getItemTouchCallbackListener());
}

private OnItemClickListenerTwo getAdapterListener() {
return new OnItemClickListenerTwo() {
@Override
public void onClick(View view, int index) {
BookShelfBean bookShelfBean = getBookshelfList().get(index);
BookShelfBean bookShelfBean = bookShelfAdapter.getBooks().get(index);
bookShelfBean.setHasUpdate(false);
DbHelper.getInstance().getmDaoSession().getBookShelfBeanDao().insertOrReplace(bookShelfBean);
Intent intent = new Intent(getActivity(), ReadBookActivity.class);
Expand All @@ -192,7 +167,7 @@ public void onLongClick(View view, int index) {
intent.putExtra("openFrom", BookDetailPresenter.FROM_BOOKSHELF);
String key = String.valueOf(System.currentTimeMillis());
intent.putExtra("data_key", key);
BitIntentDataManager.getInstance().putData(key, getBookshelfList().get(index));
BitIntentDataManager.getInstance().putData(key, bookShelfAdapter.getBooks().get(index));

startActivityByAnim(intent, android.R.anim.fade_in, android.R.anim.fade_out);

Expand All @@ -216,9 +191,8 @@ public void onPause() {
}

private void stopBookShelfRefreshAnim() {
List<BookShelfBean> bookShelfBeans = getBookshelfList();
if (bookShelfBeans != null && bookShelfBeans.size() > 0) {
for (BookShelfBean bookShelfBean : bookShelfBeans) {
if (bookShelfAdapter.getBooks() != null && bookShelfAdapter.getBooks().size() > 0) {
for (BookShelfBean bookShelfBean : bookShelfAdapter.getBooks()) {
if (bookShelfBean.isLoading()) {
bookShelfBean.setLoading(false);
refreshBook(bookShelfBean.getNoteUrl());
Expand All @@ -229,11 +203,7 @@ private void stopBookShelfRefreshAnim() {

@Override
public void refreshBookShelf(List<BookShelfBean> bookShelfBeanList) {
if (viewIsList) {
bookShelfListAdapter.replaceAll(bookShelfBeanList, bookPx);
} else {
bookShelfGridAdapter.replaceAll(bookShelfBeanList, bookPx);
}
bookShelfAdapter.replaceAll(bookShelfBeanList, bookPx);
if (rlEmptyView == null) return;
if (bookShelfBeanList.size() > 0) {
rlEmptyView.setVisibility(View.GONE);
Expand All @@ -245,11 +215,7 @@ public void refreshBookShelf(List<BookShelfBean> bookShelfBeanList) {

@Override
public void refreshBook(String noteUrl) {
if (viewIsList) {
bookShelfListAdapter.refreshBook(noteUrl);
} else {
bookShelfGridAdapter.refreshBook(noteUrl);
}
bookShelfAdapter.refreshBook(noteUrl);
}

@Override
Expand Down

0 comments on commit 3d2a42a

Please sign in to comment.