old
zlj 5 years ago
parent bf84559ef2
commit 2d6277e6e7
  1. 2
      README.md
  2. 17
      app/src/main/java/com/novel/read/activity/NovelSearchActivity.kt
  3. 183
      app/src/main/java/com/novel/read/adapter/SearchAdapter.java
  4. 168
      app/src/main/java/com/novel/read/adapter/SearchAdapter.kt

@ -9,7 +9,7 @@
- 适配了安卓9.0,使用AndroidX控件,对刘海屏等异形屏幕有了很好的支持
注: 之前项目采用Java编写,目前正在重构转KT代码。欢迎大家提问题。本人一定尽快修复
注: 之前项目采用Java编写,之后才转的Kotlin代码,可能有些替换不完善。欢迎大家提问题。本人一定尽快修复
## 使用技术
Kt、AndroidX、Retrofit、Okhttp3、Glide、LitePal等

@ -83,7 +83,6 @@ class NovelSearchActivity : NovelBaseActivity() {
} else {
if (loadSize >= COMMENT_SIZE) {
mSearchAdapter.isLoadingMore = true
mSearchList.add(SearchResp.BookBean())
mSearchAdapter.notifyDataSetChanged()
page++
getData()
@ -168,11 +167,14 @@ class NovelSearchActivity : NovelBaseActivity() {
}
})
mSearchAdapter.setOnItemClickListener { view, pos ->
mSearchAdapter.setHolderType(true)
tv_search.setText(mSearchList[pos].title)
saveKey()
}
mSearchAdapter.setOnItemClickListener(object :SearchAdapter.OnItemClickListener{
override fun onItemClick(view: View, pos: Int) {
mSearchAdapter.setHolderType(true)
tv_search.setText(mSearchList[pos].title)
saveKey()
}
})
head_history.setOnClickListener { view ->
DialogUtils.getInstance().showAlertDialog(
@ -237,10 +239,9 @@ class NovelSearchActivity : NovelBaseActivity() {
} else {
loadSize = event.result!!.book.size
if (mSearchAdapter.isLoadingMore) {
mSearchList.removeAt(mSearchList.size - 1)
mSearchAdapter.isLoadingMore = false
mSearchList.addAll(event.result!!.book)
mSearchAdapter.notifyDataSetChanged()
mSearchAdapter.isLoadingMore = false
} else {
mSearchList.clear()
mSearchList.addAll(event.result!!.book)

@ -1,183 +0,0 @@
package com.novel.read.adapter;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.novel.read.R;
import com.novel.read.activity.NovelBookDetailActivity;
import com.novel.read.adapter.holder.EmptyHolder;
import com.novel.read.adapter.holder.MoreHolder;
import com.novel.read.constants.Constant;
import com.novel.read.inter.OnLoadMoreListener;
import com.novel.read.model.protocol.SearchResp;
import com.novel.read.utlis.GlideImageLoader;
import java.util.List;
import static com.novel.read.constants.Constant.COMMENT_SIZE;
public class SearchAdapter extends RecyclerView.Adapter {
private List<SearchResp.BookBean> mList;
private Context mContext;
private final int VALUE_ITEM = 100; //正常item
private final int BOOK_ITEM = 102; //书本item
private final int EMPTY_ITEM = 101; //空白item
private boolean book = false;
private OnItemClickListener mClickListener;
private final int PROCESS_ITEM = 103; //加载更多
private boolean loadingMore;
private int lastVisibleItem, totalItemCount;
private int visibleThreshold = 1;
private OnLoadMoreListener mOnLoadMoreListener;
public SearchAdapter(List<SearchResp.BookBean> mList, RecyclerView recyclerView) {
this.mList = mList;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager llMangager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = llMangager.getItemCount();
lastVisibleItem = llMangager.findLastVisibleItemPosition();
if (!loadingMore && totalItemCount == (lastVisibleItem + visibleThreshold) && totalItemCount >= (COMMENT_SIZE)) {
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
}
}
});
}
}
public void setLoadingMore(boolean loadingMore) {
this.loadingMore = loadingMore;
}
public boolean isLoadingMore() {
return loadingMore;
}
public void setOnLoadMoreListener(OnLoadMoreListener listener) {
this.mOnLoadMoreListener = listener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (mContext == null) {
mContext = viewGroup.getContext();
}
View view;
if (i == VALUE_ITEM) {
view = LayoutInflater.from(mContext).inflate(R.layout.rlv_item_search, viewGroup, false);
return new ViewHolder(view);
} else if (i == BOOK_ITEM) {
view = LayoutInflater.from(mContext).inflate(R.layout.rlv_item_book_list_search, viewGroup, false);
return new BookHolder(view);
} else if (i == EMPTY_ITEM) {
view = LayoutInflater.from(mContext).inflate(R.layout.rlv_empty_view, viewGroup, false);
return new EmptyHolder(view);
} else if (i == PROCESS_ITEM) {
view = LayoutInflater.from(mContext).inflate(R.layout.load_more_layout, viewGroup, false);
return new MoreHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof ViewHolder) {
SearchResp.BookBean bookBean = mList.get(i);
((ViewHolder) viewHolder).mTvBookName.setText(bookBean.getTitle());
viewHolder.itemView.setOnClickListener(view -> mClickListener.onItemClick(view, i));
} else if (viewHolder instanceof BookHolder) {
SearchResp.BookBean bookBean = mList.get(i);
((BookHolder) viewHolder).tvBookName.setText(bookBean.getTitle());
((BookHolder) viewHolder).tvBookAuthor.setText(bookBean.getAuthor());
((BookHolder) viewHolder).tvBookDescription.setText(bookBean.getDescription());
GlideImageLoader.INSTANCE.displayCornerImage(mContext, bookBean.getCover(), ((BookHolder) viewHolder).ivBook);
viewHolder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, NovelBookDetailActivity.class);
intent.putExtra(Constant.Bundle.BookId, bookBean.getId());
mContext.startActivity(intent);
});
}
}
@Override
public int getItemCount() {
if (mList.size() == 0) {
return 1;
}
return mList.size();
}
@Override
public int getItemViewType(int position) {
if (mList == null || mList.size() == 0) {
return EMPTY_ITEM;
} else {
if (mList.get(position) == null) {
return PROCESS_ITEM;
}
if (book) {
return BOOK_ITEM;
} else {
return VALUE_ITEM;
}
}
}
public void setHolderType(boolean book) {
this.book = book;
notifyDataSetChanged();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTvBookName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mTvBookName = itemView.findViewById(R.id.tv_book_name);
}
}
static class BookHolder extends RecyclerView.ViewHolder {
ImageView ivBook;
TextView tvBookName;
TextView tvBookAuthor;
TextView tvBookDescription;
public BookHolder(@NonNull View itemView) {
super(itemView);
ivBook = itemView.findViewById(R.id.iv_book);
tvBookName = itemView.findViewById(R.id.tv_book_name);
tvBookAuthor = itemView.findViewById(R.id.tv_book_author);
tvBookDescription = itemView.findViewById(R.id.tv_book_description);
}
}
public void setOnItemClickListener(OnItemClickListener mListener) {
this.mClickListener = mListener;
}
public interface OnItemClickListener {
void onItemClick(View view, int pos);
}
}

@ -0,0 +1,168 @@
package com.novel.read.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.novel.read.R
import com.novel.read.activity.NovelBookDetailActivity
import com.novel.read.adapter.holder.EmptyHolder
import com.novel.read.adapter.holder.MoreHolder
import com.novel.read.constants.Constant
import com.novel.read.inter.OnLoadMoreListener
import com.novel.read.model.protocol.SearchResp
import com.novel.read.utlis.GlideImageLoader
import com.novel.read.constants.Constant.COMMENT_SIZE
class SearchAdapter(private val mList: List<SearchResp.BookBean>, recyclerView: RecyclerView) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var mContext: Context? = null
private var book = false
private var mClickListener: OnItemClickListener? = null
var isLoadingMore: Boolean = false
private var lastVisibleItem: Int = 0
private var totalItemCount: Int = 0
private val visibleThreshold = 1
private var mOnLoadMoreListener: OnLoadMoreListener? = null
init {
if (recyclerView.layoutManager is LinearLayoutManager) {
val llMangager = recyclerView.layoutManager as LinearLayoutManager?
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
totalItemCount = llMangager!!.itemCount
lastVisibleItem = llMangager.findLastVisibleItemPosition()
if (!isLoadingMore && totalItemCount == lastVisibleItem + visibleThreshold && totalItemCount >= COMMENT_SIZE) {
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener!!.onLoadMore()
}
}
}
})
}
}
fun setOnLoadMoreListener(listener: OnLoadMoreListener) {
this.mOnLoadMoreListener = listener
}
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): RecyclerView.ViewHolder {
if (mContext == null) {
mContext = viewGroup.context
}
val view: View
when (i) {
VALUE_ITEM -> {
view = LayoutInflater.from(mContext).inflate(R.layout.rlv_item_search, viewGroup, false)
return ViewHolder(view)
}
BOOK_ITEM -> {
view = LayoutInflater.from(mContext).inflate(R.layout.rlv_item_book_list_search, viewGroup, false)
return BookHolder(view)
}
EMPTY_ITEM -> {
view = LayoutInflater.from(mContext).inflate(R.layout.rlv_empty_view, viewGroup, false)
return EmptyHolder(view)
}
PROCESS_ITEM -> {
view = LayoutInflater.from(mContext).inflate(R.layout.load_more_layout, viewGroup, false)
return MoreHolder(view)
}
else -> throw IllegalArgumentException()
}
}
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, i: Int) {
when (viewHolder) {
is ViewHolder -> {
val bookBean = mList[i]
viewHolder.mTvBookName.text = bookBean.title
viewHolder.itemView.setOnClickListener { view -> mClickListener!!.onItemClick(view, i) }
}
is BookHolder -> {
val bookBean = mList[i]
viewHolder.tvBookName.text = bookBean.title
viewHolder.tvBookAuthor.text = bookBean.author
viewHolder.tvBookDescription.text = bookBean.description
GlideImageLoader.displayCornerImage(mContext!!, bookBean.cover!!, viewHolder.ivBook)
viewHolder.itemView.setOnClickListener { view ->
val intent = Intent(mContext, NovelBookDetailActivity::class.java)
intent.putExtra(Constant.Bundle.BookId, bookBean.id)
mContext!!.startActivity(intent)
}
}
is MoreHolder -> viewHolder.bindModule(isLoadingMore)
}
}
override fun getItemCount(): Int {
return if (mList.isEmpty()) {
1
} else mList.size+1
}
override fun getItemViewType(position: Int): Int {
if (position == itemCount - 1) {
return PROCESS_ITEM
}
return if (mList.isEmpty()) {
EMPTY_ITEM
} else {
if (book) {
BOOK_ITEM
} else {
VALUE_ITEM
}
}
}
fun setHolderType(book: Boolean) {
this.book = book
notifyDataSetChanged()
}
internal class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var mTvBookName: TextView = itemView.findViewById(R.id.tv_book_name)
}
internal class BookHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var ivBook: ImageView = itemView.findViewById(R.id.iv_book)
var tvBookName: TextView = itemView.findViewById(R.id.tv_book_name)
var tvBookAuthor: TextView = itemView.findViewById(R.id.tv_book_author)
var tvBookDescription: TextView = itemView.findViewById(R.id.tv_book_description)
}
fun setOnItemClickListener(mListener: OnItemClickListener) {
this.mClickListener = mListener
}
interface OnItemClickListener {
fun onItemClick(view: View, pos: Int)
}
companion object {
private const val VALUE_ITEM = 100 //正常item
private const val EMPTY_ITEM = 101 //空白item
private const val BOOK_ITEM = 102 //书本item
private const val PROCESS_ITEM = 103
}
}
Loading…
Cancel
Save