diff --git a/app/src/main/java/xyz/fycz/myreader/base/adapter/BaseListAdapter.java b/app/src/main/java/xyz/fycz/myreader/base/adapter/BaseListAdapter.java index d550a1d..5eaa86a 100644 --- a/app/src/main/java/xyz/fycz/myreader/base/adapter/BaseListAdapter.java +++ b/app/src/main/java/xyz/fycz/myreader/base/adapter/BaseListAdapter.java @@ -1,11 +1,13 @@ package xyz.fycz.myreader.base.adapter; -import android.os.Handler; +import android.util.SparseArray; +import android.util.SparseIntArray; import android.view.View; import android.view.ViewGroup; import android.widget.Filter; import android.widget.Filterable; +import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; @@ -20,17 +22,34 @@ import java.util.List; public abstract class BaseListAdapter extends RecyclerView.Adapter implements Filterable { private static final String TAG = "BaseListAdapter"; + protected static final int TYPE_OTHER = Integer.MIN_VALUE; /*common statement*/ protected List mList = new ArrayList<>(); + protected SparseArray otherViews = new SparseArray<>(); + protected SparseIntArray otherViewPos = new SparseIntArray(); protected OnItemClickListener mClickListener; protected OnItemLongClickListener mLongClickListener; /************************abstract area************************/ protected abstract IViewHolder createViewHolder(int viewType); + /*************************rewrite logic area***************************************/ + + @Override + public int getItemViewType(int position) { + int key = otherViewPos.indexOfKey(position); + if (key >= 0) { + return otherViewPos.valueAt(key); + } + return super.getItemViewType(position); + } + @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + if (viewType < TYPE_OTHER + getOtherCount()) { + return new OtherViewHolder(otherViews.get(viewType)); + } IViewHolder viewHolder = createViewHolder(viewType); View view = viewHolder.createItemView(parent); @@ -41,6 +60,7 @@ public abstract class BaseListAdapter extends RecyclerView.Adapter extends RecyclerView.Adapter extends RecyclerView.Adapter values) { int oldSize = getItemSize(); if (mList.addAll(values)) { @@ -122,7 +153,7 @@ public abstract class BaseListAdapter extends RecyclerView.Adapter extends RecyclerView.Adapter= 0 && oldPos < size && newPos >=0 && newPos < size){ + if (oldPos >= 0 && oldPos < size && newPos >= 0 && newPos < size) { Collections.swap(mList, oldPos, newPos); notifyItemMoved(oldPos, newPos); } @@ -174,4 +205,11 @@ public abstract class BaseListAdapter extends RecyclerView.Adapter implements C //保存最近阅读时间 mBook.setLastReadTime(DateHelper.getLongDate()); init(); + + initAd(); + } + + private void initAd() { + AdUtils.checkHasAd().subscribe(new MySingleObserver() { + @Override + public void onSuccess(@NonNull Boolean aBoolean) { + AdBean adBean = AdUtils.getAdConfig().getRead(); + if (aBoolean && AdUtils.adTime("read", adBean)) { + if (adBean.getStatus() > 0) { + AdUtils.showInterAd(ReadActivity.this, "read"); + } + } + } + }); } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/fragment/FindBook2Fragment.java b/app/src/main/java/xyz/fycz/myreader/ui/fragment/FindBook2Fragment.java index 6f57a5d..304d679 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/fragment/FindBook2Fragment.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/fragment/FindBook2Fragment.java @@ -2,6 +2,7 @@ package xyz.fycz.myreader.ui.fragment; import android.content.Intent; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,17 +22,21 @@ import xyz.fycz.myreader.base.LazyFragment; import xyz.fycz.myreader.base.adapter.BaseListAdapter; import xyz.fycz.myreader.base.adapter.IViewHolder; import xyz.fycz.myreader.base.observer.MyObserver; +import xyz.fycz.myreader.base.observer.MySingleObserver; import xyz.fycz.myreader.common.APPCONST; import xyz.fycz.myreader.databinding.FragmentFindBook2Binding; import xyz.fycz.myreader.entity.FindKind; +import xyz.fycz.myreader.entity.ad.AdBean; import xyz.fycz.myreader.greendao.entity.Book; import xyz.fycz.myreader.greendao.service.BookService; import xyz.fycz.myreader.ui.activity.BookDetailedActivity; import xyz.fycz.myreader.ui.activity.BookstoreActivity; +import xyz.fycz.myreader.ui.activity.ReadActivity; import xyz.fycz.myreader.ui.adapter.holder.FindBookHolder; import xyz.fycz.myreader.ui.dialog.SourceExchangeDialog; import xyz.fycz.myreader.util.ToastUtils; import xyz.fycz.myreader.util.help.StringHelper; +import xyz.fycz.myreader.util.utils.AdUtils; import xyz.fycz.myreader.util.utils.RxUtils; import xyz.fycz.myreader.webapi.BookApi; import xyz.fycz.myreader.webapi.crawler.base.FindCrawler; @@ -51,6 +56,8 @@ public class FindBook2Fragment extends LazyFragment { private static final String KIND = "kind"; private static final String FIND_CRAWLER = "findCrawler"; + private AdBean adBean; + private View flow; public FindBook2Fragment() { } @@ -78,6 +85,7 @@ public class FindBook2Fragment extends LazyFragment { outState.putString(APPCONST.DATA_KEY, dataKey); super.onSaveInstanceState(outState); } + @Override public void lazyInit() { initData(); @@ -92,6 +100,8 @@ public class FindBook2Fragment extends LazyFragment { } protected void initData() { + adBean = AdUtils.getAdConfig().getFind(); + getFlow(); findBookAdapter = new BaseListAdapter() { @Override protected IViewHolder createViewHolder(int viewType) { @@ -104,6 +114,19 @@ public class FindBook2Fragment extends LazyFragment { loadBooks(); } + private void getFlow() { + AdUtils.checkHasAd().subscribe(new MySingleObserver() { + @Override + public void onSuccess(@NonNull Boolean aBoolean) { + if (aBoolean && AdUtils.adTime("find", adBean)) { + if (adBean.getStatus() > 0) { + AdUtils.getFlowAd(getActivity(), 1, view -> flow = view, "find"); + } + } + } + }); + } + protected void initWidget() { binding.loading.setOnReloadingListener(() -> { page = 1; @@ -156,6 +179,13 @@ public class FindBook2Fragment extends LazyFragment { } else { findBookAdapter.refreshItems(books); binding.srlFindBooks.finishRefresh(); + if (flow != null) { + int index = findBookAdapter.getItemCount() - books.size() + 2; + index = Math.min(findBookAdapter.getItemCount() - 1, index); + findBookAdapter.addOther(index, flow); + flow = null; + getFlow(); + } } } else { if (books.size() == 0) { @@ -163,6 +193,13 @@ public class FindBook2Fragment extends LazyFragment { } else { findBookAdapter.addItems(books); binding.srlFindBooks.finishLoadMore(); + if (flow != null) { + int index = findBookAdapter.getItemCount() - books.size() + 2; + index = Math.min(findBookAdapter.getItemCount() - 1, index); + findBookAdapter.addOther(index, flow); + flow = null; + getFlow(); + } } } page++; @@ -176,7 +213,7 @@ public class FindBook2Fragment extends LazyFragment { binding.loading.showError(); binding.srlFindBooks.finishRefresh(); } else { - if (e.getMessage()!= null && e.getMessage().contains("没有下一页")) { + if (e.getMessage() != null && e.getMessage().contains("没有下一页")) { binding.srlFindBooks.finishLoadMoreWithNoMoreData(); } else { ToastUtils.showError("数据加载失败\n" + e.getLocalizedMessage()); diff --git a/app/src/main/res/layout/activity_user_info.xml b/app/src/main/res/layout/activity_user_info.xml index 77892ff..058941a 100644 --- a/app/src/main/res/layout/activity_user_info.xml +++ b/app/src/main/res/layout/activity_user_info.xml @@ -46,7 +46,6 @@ android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" - android:text="@string/app_name" android:textColor="@color/textSecondary" android:textSize="@dimen/text_normal_size" /> @@ -74,7 +73,6 @@ android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" - android:text="@string/app_name" android:textColor="@color/textSecondary" android:textSize="@dimen/text_normal_size" /> @@ -102,7 +100,6 @@ android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" - android:text="@string/app_name" android:textColor="@color/textSecondary" android:textSize="@dimen/text_normal_size" /> @@ -179,7 +176,6 @@ android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" - android:text="@string/app_name" android:textColor="@color/textSecondary" android:textSize="@dimen/text_normal_size" /> @@ -207,7 +203,6 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" - android:text="@string/app_name" android:textColor="@color/textSecondary" android:textSize="@dimen/text_normal_size" android:layout_toStartOf="@+id/iv_no_ad_arrow"/>