403 lines
13 KiB
Plaintext
403 lines
13 KiB
Plaintext
|
package com.novelbook.android.Fragments;
|
|||
|
|
|||
|
import android.annotation.SuppressLint;
|
|||
|
|
|||
|
import android.os.Bundle;
|
|||
|
import android.support.v7.widget.LinearLayoutManager;
|
|||
|
import android.support.v7.widget.RecyclerView;
|
|||
|
import android.support.v7.widget.Toolbar;
|
|||
|
import android.text.TextUtils;
|
|||
|
import android.util.Log;
|
|||
|
import android.view.MenuItem;
|
|||
|
import android.view.View;
|
|||
|
import android.widget.Toast;
|
|||
|
|
|||
|
import com.novelbook.android.R;
|
|||
|
|
|||
|
import com.novelbook.android.db.Novel;
|
|||
|
import com.novelbook.android.netsubscribe.BookSubscribe;
|
|||
|
import com.novelbook.android.netutils.OnSuccessAndFaultListener;
|
|||
|
import com.novelbook.android.netutils.OnSuccessAndFaultSub;
|
|||
|
import com.novelbook.android.utils.Constants;
|
|||
|
import com.novelbook.android.utils.GsonUtil;
|
|||
|
import com.novelbook.android.utils.OnItemClickListener;
|
|||
|
import com.novelbook.android.adapter.BookListAdapter;
|
|||
|
import com.umeng.analytics.MobclickAgent;
|
|||
|
|
|||
|
import org.json.JSONObject;
|
|||
|
|
|||
|
import java.util.ArrayList;
|
|||
|
|
|||
|
import java.util.List;
|
|||
|
|
|||
|
import butterknife.BindView;
|
|||
|
|
|||
|
|
|||
|
public class Fragment_booklist extends BasicFragment {
|
|||
|
public static final String TAG = Fragment_booklist.class.getSimpleName();
|
|||
|
private static final String EXTR_CATE ="cate" ;
|
|||
|
private static final String EXTR_PROGRESS ="progress" ;
|
|||
|
private static final String EXTR_SEARCH ="search";
|
|||
|
private static final String EXTR_FN ="fn" ;
|
|||
|
private static final String EXTR_BANGDAN ="bangdan" ;
|
|||
|
private static final String EXTR_HISTORY ="history" ;
|
|||
|
private static final String EXTR_CID ="cid" ;
|
|||
|
private BookListAdapter mAdapter;
|
|||
|
// private BookListAdapter mAdapter;
|
|||
|
private List mData;;
|
|||
|
private List mMoreData;
|
|||
|
private String cate;
|
|||
|
private int progress,cid;
|
|||
|
private String keyWord ,fn,bangdan,history;
|
|||
|
private int listItem =R.layout.recycle_list_item_horizon;
|
|||
|
//private int pageNo=1;
|
|||
|
private int totalCount;
|
|||
|
// private int pageCount;
|
|||
|
@BindView(R.id.rvBooklist)
|
|||
|
RecyclerView mRecyclerView;
|
|||
|
|
|||
|
@SuppressLint("ValidFragment")
|
|||
|
public Fragment_booklist(String cate, int progress) {
|
|||
|
this.cate =cate;
|
|||
|
this.progress = progress;
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public Fragment_booklist() {
|
|||
|
// Required empty public constructor
|
|||
|
}
|
|||
|
public static Fragment_booklist newInstance(int cid,String bangdan) {
|
|||
|
Fragment_booklist fragment = new Fragment_booklist();
|
|||
|
Bundle args = new Bundle();
|
|||
|
args.putInt(EXTR_CID,cid);
|
|||
|
args.putString(EXTR_BANGDAN,bangdan);
|
|||
|
fragment.setArguments(args);
|
|||
|
return fragment;
|
|||
|
}
|
|||
|
public static Fragment_booklist newInstance(String fn,String bangdan) {
|
|||
|
Fragment_booklist fragment = new Fragment_booklist();
|
|||
|
Bundle args = new Bundle();
|
|||
|
args.putString(EXTR_FN,fn);
|
|||
|
args.putString(EXTR_BANGDAN,bangdan);
|
|||
|
fragment.setArguments(args);
|
|||
|
return fragment;
|
|||
|
}
|
|||
|
public static Fragment_booklist newInstance(String noveType,int progress) {
|
|||
|
Fragment_booklist fragment = new Fragment_booklist();
|
|||
|
Bundle args = new Bundle();
|
|||
|
args.putString(EXTR_CATE,noveType);
|
|||
|
args.putInt(EXTR_PROGRESS, progress);
|
|||
|
fragment.setArguments(args);
|
|||
|
return fragment;
|
|||
|
}
|
|||
|
public static Fragment_booklist search(String keyWord) {
|
|||
|
Fragment_booklist fragment = new Fragment_booklist();
|
|||
|
Bundle args = new Bundle();
|
|||
|
args.putString(EXTR_SEARCH,keyWord);
|
|||
|
|
|||
|
fragment.setArguments(args);
|
|||
|
return fragment;
|
|||
|
}
|
|||
|
public static Fragment_booklist history() {
|
|||
|
Fragment_booklist fragment = new Fragment_booklist();
|
|||
|
Bundle args = new Bundle();
|
|||
|
args.putString(EXTR_HISTORY,EXTR_HISTORY);
|
|||
|
|
|||
|
fragment.setArguments(args);
|
|||
|
return fragment;
|
|||
|
}
|
|||
|
protected void processArguments(){
|
|||
|
if (getArguments() != null) {
|
|||
|
Bundle bundle = getArguments() ;
|
|||
|
bangdan =bundle.getString(EXTR_BANGDAN);
|
|||
|
fn= bundle.getString(EXTR_FN);
|
|||
|
cid =bundle.getInt(EXTR_CID);
|
|||
|
if(!TextUtils.isEmpty((fn))){
|
|||
|
return;
|
|||
|
}
|
|||
|
cate = bundle.getString(EXTR_CATE);
|
|||
|
progress = bundle.getInt(EXTR_PROGRESS);
|
|||
|
keyWord =bundle.getString(EXTR_SEARCH);
|
|||
|
history =bundle.getString(EXTR_HISTORY);
|
|||
|
if(!TextUtils.isEmpty(history)){
|
|||
|
listItem =R.layout.recycle_list_item_history;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int lastPageNo =0;
|
|||
|
@Override
|
|||
|
protected void fillData() {
|
|||
|
|
|||
|
if(mRecyclerView.getAdapter()==null) {
|
|||
|
mRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
|
|||
|
mRecyclerView.setAdapter(mAdapter);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
if(mMoreData!=null) {
|
|||
|
if(mMoreData.size()>0) {
|
|||
|
pageNo++;
|
|||
|
}
|
|||
|
int status =getPageCount()>= pageNo ? BookListAdapter.PULLUP_LOAD_MORE : BookListAdapter.NO_LOAD_MORE;
|
|||
|
// mAdapter.setPercent(pageNo/pageCount);
|
|||
|
mAdapter.AddFooterItem(mMoreData);
|
|||
|
mAdapter.changeMoreStatus(status);
|
|||
|
mMoreData.clear();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
protected int getLayoutRes() {
|
|||
|
return R.layout.fragment_fragment_booklist;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
@Override
|
|||
|
public void initData() {
|
|||
|
if(pageNo>1){
|
|||
|
if(lastPageNo ==pageNo){
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
lastPageNo = pageNo;
|
|||
|
// int pn = pageNo;
|
|||
|
|
|||
|
if(pageNo==1) {
|
|||
|
|
|||
|
if (mData != null) { //下拉刷新
|
|||
|
mData = new ArrayList<Novel>();
|
|||
|
mAdapter.setData(mData);
|
|||
|
// loadListAd(mAdapter,1,false);
|
|||
|
|
|||
|
} else {
|
|||
|
|
|||
|
initialDataAdapter();
|
|||
|
}
|
|||
|
}
|
|||
|
/* if(mAdapter!=null){
|
|||
|
mAdapter.changeMoreStatus(BookListAdapter.LOADING_MORE);
|
|||
|
}*/
|
|||
|
mMoreData =null;
|
|||
|
OnSuccessAndFaultListener successAndFaultListener = new OnSuccessAndFaultListener() {
|
|||
|
@Override
|
|||
|
public void onSuccess(String result) {
|
|||
|
|
|||
|
// mFirstPage= gson.fromJson(result, FirstPage.class);
|
|||
|
try {
|
|||
|
JSONObject jsonObject = new JSONObject(result);
|
|||
|
setPageCount(jsonObject.getInt("pageCount")); ;
|
|||
|
mMoreData = GsonUtil. parserJsonArray(jsonObject,Constants.BLOCK_TITLE_NOVELS);
|
|||
|
if(mMoreData.size()>0){
|
|||
|
loadListAd(mAdapter,1,mData.size()>0);
|
|||
|
}
|
|||
|
} catch (Exception e) {
|
|||
|
e.printStackTrace(); Log.e(TAG, "onSuccess: 解析失败",e );
|
|||
|
}
|
|||
|
handler.sendEmptyMessage(1);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onFault(String errorMsg) {
|
|||
|
//失败
|
|||
|
Log.d(TAG, "error on get firstpage: " + errorMsg);
|
|||
|
handler.sendEmptyMessage(1);
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
showProgressDialog(false,"正在加载...");
|
|||
|
|
|||
|
Log.d(TAG,String.format("fn is %s",fn) );
|
|||
|
|
|||
|
if(!TextUtils.isEmpty(bangdan) && (cid >0 || !TextUtils.isEmpty(fn))){ //榜单
|
|||
|
// showProgressDialog(true, "正在加载榜单");
|
|||
|
mAdapter.setShowTop(true);
|
|||
|
BookSubscribe.getPaihangBangList(Constants.SEX,pageNo,cid,new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
|
|||
|
@Override
|
|||
|
public void onSuccess(String result) {
|
|||
|
|
|||
|
try {
|
|||
|
JSONObject jsonObject = new JSONObject(result);
|
|||
|
String resultstr = jsonObject.getString("rank") ;
|
|||
|
setPageCount(jsonObject.getJSONObject("rank").getInt("pageCount")); ;
|
|||
|
mMoreData = GsonUtil. parserJsonArray(resultstr,Constants.BLOCK_TITLE_NOVELS);
|
|||
|
Log.d(TAG, "排行榜详细 onSuccess: pageCount " + getPageCount());
|
|||
|
if(mMoreData.size()>0){
|
|||
|
loadListAd(mAdapter,1,mData.size()>0);
|
|||
|
}
|
|||
|
} catch (Exception e) {
|
|||
|
e.printStackTrace();
|
|||
|
Log.e(TAG, "onSuccess: 解析失败",e );
|
|||
|
}
|
|||
|
Log.d(TAG, "progress: progress to hide");
|
|||
|
handler.sendEmptyMessage(1);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onFault(String errorMsg) {
|
|||
|
//失败
|
|||
|
Log.d(TAG, "error on get firstpage: " + errorMsg);
|
|||
|
handler.sendEmptyMessage(1);
|
|||
|
}
|
|||
|
}, getActivity()));
|
|||
|
|
|||
|
|
|||
|
}else if(!TextUtils.isEmpty(fn)){ //首页 更多
|
|||
|
mAdapter.setShowTop(true);
|
|||
|
// showProgressDialog(true, "正在加载更多");
|
|||
|
BookSubscribe.getNovelPaihang(fn,pageNo,Constants.SEX,new OnSuccessAndFaultSub(successAndFaultListener, getActivity()));
|
|||
|
|
|||
|
}
|
|||
|
else if(!TextUtils.isEmpty((cate))) { //分类
|
|||
|
mAdapter.setShowTop(true);
|
|||
|
// showProgressDialog(true, "正在加载分类");
|
|||
|
BookSubscribe.getCateNovelList(cate, pageNo, Constants.SEX, progress, new OnSuccessAndFaultSub(successAndFaultListener, getActivity()));
|
|||
|
|
|||
|
}else if(!TextUtils.isEmpty(keyWord)){ //搜索
|
|||
|
// showProgressDialog(true, "正在加载搜索");
|
|||
|
// mAdapter.setShowTop(true);
|
|||
|
BookSubscribe.getSearchNovelList( keyWord,pageNo, Constants.SEX, new OnSuccessAndFaultSub(successAndFaultListener, getActivity()));
|
|||
|
}else if(!TextUtils.isEmpty(history)){
|
|||
|
loadHistory();
|
|||
|
}else{
|
|||
|
handler.sendEmptyMessage(1);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void initialDataAdapter() {
|
|||
|
mData = new ArrayList<Novel>();
|
|||
|
mAdapter = new BookListAdapter(activity, mData, listItem, new OnItemClickListener() {
|
|||
|
|
|||
|
@Override
|
|||
|
public void onItemClick(View view, int position) {
|
|||
|
if(mData.get(position) instanceof Novel)
|
|||
|
showBookDetail((Novel)mData.get(position));
|
|||
|
// openBook(mData.get(position),mAdapter);
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onItemLongClick(View view, int position) {
|
|||
|
// initDialog(position);
|
|||
|
// mAdapter.removeData(position);
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onLinearOutClick(View view, int position, int llId) {
|
|||
|
Toast.makeText(activity, "book " + position + " clicked",
|
|||
|
Toast.LENGTH_SHORT).show();
|
|||
|
}
|
|||
|
});
|
|||
|
mAdapter.setShowFootView(true);
|
|||
|
mRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
|
|||
|
mRecyclerView.setAdapter(mAdapter);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void initViews(){
|
|||
|
//mRecyclerView
|
|||
|
|
|||
|
//lv_catalogue.setFastScrollStyle(R.style.FastScrollTheme); //不起作用
|
|||
|
initLoadMoreListener();
|
|||
|
}
|
|||
|
|
|||
|
//----------------绑定列表
|
|||
|
|
|||
|
void initialBookList() {
|
|||
|
|
|||
|
}
|
|||
|
@Override
|
|||
|
public void setFTag() {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void initLoadMoreListener() {
|
|||
|
|
|||
|
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|||
|
int lastVisibleItem ;
|
|||
|
@Override
|
|||
|
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
|||
|
super.onScrollStateChanged(recyclerView, newState);
|
|||
|
Log.d(TAG, String.format("onScrollStateChanged , state %s ,lastVisibleitem %s ,getItemCount %s,pageno %s ,getPageCount() %s",
|
|||
|
newState==RecyclerView.SCROLL_STATE_IDLE,lastVisibleItem,mAdapter.getItemCount(),pageNo,getPageCount()));
|
|||
|
//判断RecyclerView的状态 是空闲时,同时,是最后一个可见的ITEM时才加载
|
|||
|
if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==mAdapter.getItemCount()){
|
|||
|
|
|||
|
if(pageNo <= getPageCount()) {
|
|||
|
//设置正在加载更多
|
|||
|
mAdapter.changeMoreStatus(mAdapter.LOADING_MORE);
|
|||
|
|
|||
|
//改为网络请求
|
|||
|
initData();
|
|||
|
}else{
|
|||
|
mAdapter.changeMoreStatus(mAdapter.NO_LOAD_MORE);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|||
|
super.onScrolled(recyclerView, dx, dy);
|
|||
|
|
|||
|
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
|||
|
//最后一个可见的ITEM
|
|||
|
lastVisibleItem=layoutManager.findLastVisibleItemPosition();
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
}
|
|||
|
void loadHistory(){
|
|||
|
initialDataAdapter();
|
|||
|
new Thread() {
|
|||
|
@Override
|
|||
|
public void run() {
|
|||
|
super.run();
|
|||
|
|
|||
|
mMoreData = Novel.getNovelsHistory();
|
|||
|
setPageCount(1);
|
|||
|
//
|
|||
|
handler.sendEmptyMessage(1);
|
|||
|
if(mMoreData!=null && mMoreData.size()>5) {
|
|||
|
loadListAd(mAdapter, 1, mData.size() > 0);
|
|||
|
}
|
|||
|
}
|
|||
|
}.start();
|
|||
|
}
|
|||
|
boolean isFirstLoad =true;
|
|||
|
@Override
|
|||
|
public void onResume(){
|
|||
|
super.onResume();
|
|||
|
// pageNo=1;
|
|||
|
|
|||
|
if(!TextUtils.isEmpty(history)){
|
|||
|
if(isFirstLoad) {
|
|||
|
isFirstLoad=false;
|
|||
|
}else{
|
|||
|
initData();
|
|||
|
}
|
|||
|
}
|
|||
|
MobclickAgent.onPageStart(TAG);
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onPause(){
|
|||
|
super.onPause();
|
|||
|
MobclickAgent.onPageEnd(TAG);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|