430 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
		
		
			
		
	
	
			430 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
|  | package com.novelbook.android.Fragments; | |||
|  | 
 | |||
|  | import android.content.Context; | |||
|  | import android.os.Bundle; | |||
|  | import android.os.Handler; | |||
|  | import android.os.Message; | |||
|  | import android.support.design.widget.AppBarLayout; | |||
|  | import android.support.v4.widget.NestedScrollView; | |||
|  | import android.support.v7.widget.LinearLayoutManager; | |||
|  | import android.support.v7.widget.RecyclerView; | |||
|  | import android.util.Log; | |||
|  | import android.view.View; | |||
|  | import android.widget.TextView; | |||
|  | 
 | |||
|  | import com.ixiaow.multilayout.MultiLayout; | |||
|  | import com.novelbook.android.R; | |||
|  | 
 | |||
|  | 
 | |||
|  | import com.novelbook.android.bean.ProgressType; | |||
|  | import com.novelbook.android.db.Novel; | |||
|  | import com.novelbook.android.netapi.RandomHost; | |||
|  | import com.novelbook.android.netsubscribe.BookSubscribe; | |||
|  | import com.novelbook.android.netutils.NetUtil; | |||
|  | import com.novelbook.android.netutils.OnSuccessAndFaultListener; | |||
|  | import com.novelbook.android.netutils.OnSuccessAndFaultSub; | |||
|  | import com.novelbook.android.utils.CommonUtil; | |||
|  | 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.flyco.tablayout.CommonTabLayout; | |||
|  | import com.flyco.tablayout.listener.CustomTabEntity; | |||
|  | import com.umeng.analytics.MobclickAgent; | |||
|  | 
 | |||
|  | import org.json.JSONArray; | |||
|  | import org.json.JSONObject; | |||
|  | import org.litepal.util.Const; | |||
|  | 
 | |||
|  | import java.util.ArrayList; | |||
|  | import java.util.Arrays; | |||
|  | import java.util.List; | |||
|  | 
 | |||
|  | import butterknife.BindView; | |||
|  | 
 | |||
|  | import static com.novelbook.android.utils.Constants.lstNt; | |||
|  | import static com.novelbook.android.utils.Constants.lstProgress; | |||
|  | import static com.novelbook.android.utils.Constants.lstSex; | |||
|  | 
 | |||
|  | public class Fragment_paihang extends BasicFragment { | |||
|  | public static final String TAG = Fragment_paihang.class.getSimpleName(); | |||
|  | 
 | |||
|  | 
 | |||
|  |     private ArrayList<CustomTabEntity> mData1,mData2,mData3  ; | |||
|  |     private int tab1Pos,tab2Pos,tab3Pos; | |||
|  |     private BookListAdapter mAdapter; | |||
|  |     // private BookListAdapter mAdapter; | |||
|  |     private List<Novel> mData;; | |||
|  |     private List<Novel> mMoreData; | |||
|  |     @BindView(R.id.rvPaihang) | |||
|  |     RecyclerView rvPaihang; | |||
|  |   /*  @BindView(R.id.tab_layout1) | |||
|  |     CommonTabLayout tabLayout1; | |||
|  |     @BindView(R.id.tab_layout2) | |||
|  |     CommonTabLayout tabLayout2; | |||
|  |     @BindView(R.id.tab_layout3)*/ | |||
|  |     CommonTabLayout tabLayout3; | |||
|  |     @BindView(R.id.topic_layout) | |||
|  |     MultiLayout topic_layout; | |||
|  |     @BindView(R.id.topic_layout1) | |||
|  |     MultiLayout topic_layout1; | |||
|  |     @BindView(R.id.topic_layout2) | |||
|  |     MultiLayout topic_layout2; | |||
|  |     @BindView(R.id.app_bar) | |||
|  |     AppBarLayout appBarLayout; | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     public Fragment_paihang() { | |||
|  |         // Required empty public constructor | |||
|  |     } | |||
|  |     protected   void processArguments(){ | |||
|  |         if (getArguments() != null) { | |||
|  |             Bundle bundle = getArguments() ; | |||
|  |         } | |||
|  | 
 | |||
|  |     } | |||
|  |     @Override | |||
|  |     protected  void fillData() { | |||
|  |         if(rvPaihang.getAdapter()==null) { | |||
|  |             rvPaihang.setLayoutManager(new LinearLayoutManager(activity)); | |||
|  |             rvPaihang.setAdapter(mAdapter); | |||
|  |         } | |||
|  |         if(mMoreData!=null) { | |||
|  |             if(mMoreData.size()>0) { | |||
|  |                 pageNo++; | |||
|  |             } | |||
|  |             int status = getPageCount() >= pageNo ? BookListAdapter.LOADING_MORE : BookListAdapter.NO_LOAD_MORE; | |||
|  |             //  mAdapter.setPercent(pageNo/pageCount); | |||
|  |             mAdapter.AddFooterItem(mMoreData); | |||
|  |             mAdapter.changeMoreStatus(status); | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         mAdapter.notifyDataSetChanged(); | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     public static Fragment_paihang newInstance(String param1, String param2) { | |||
|  |         Fragment_paihang fragment = new Fragment_paihang(); | |||
|  |         Bundle args = new Bundle(); | |||
|  | 
 | |||
|  |         fragment.setArguments(args); | |||
|  |         return fragment; | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void onCreate(Bundle savedInstanceState) { | |||
|  |         super.onCreate(savedInstanceState); | |||
|  |         if (getArguments() != null) { | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     protected int getLayoutRes() { | |||
|  |         return R.layout.fragment_fragment_paihang; | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void initData() { | |||
|  |       loadSearchData(); | |||
|  |     } | |||
|  |       private void loadSearchData(){ | |||
|  |         int pn = pageNo; | |||
|  |         if(lstSex==null){ | |||
|  |            // getSearchTabTtitle(); | |||
|  |            // initTabs(); | |||
|  |             appBarLayout.setVisibility(View.GONE); | |||
|  |             return; | |||
|  |         } | |||
|  |           appBarLayout.setVisibility(View.VISIBLE); | |||
|  |         String sex = lstSex.get(tab1Pos); | |||
|  |         String cate = lstNt.get(tab2Pos); | |||
|  |         if(pageNo==1) { | |||
|  | 
 | |||
|  |             if (mData != null) {  //下拉刷新 | |||
|  |                 mData = new ArrayList<Novel>(); | |||
|  |                 mAdapter.setData(mData); | |||
|  | 
 | |||
|  |             } else { | |||
|  | 
 | |||
|  |                 mData = new ArrayList<Novel>(); | |||
|  |                 mAdapter = new BookListAdapter(activity, mData, R.layout.recycle_list_item_horizon, new OnItemClickListener() { | |||
|  | 
 | |||
|  |                     @Override | |||
|  |                     public void onItemClick(View view, int position) { | |||
|  |                         showBookDetail(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) { | |||
|  | 
 | |||
|  |                     } | |||
|  |                 }); | |||
|  |                 mAdapter.setShowTop(true); | |||
|  |                 mAdapter.setShowFootView(true); | |||
|  |                 rvPaihang.setLayoutManager(new LinearLayoutManager(activity)); | |||
|  |                 rvPaihang.setAdapter(mAdapter); | |||
|  | 
 | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |         mMoreData =null; | |||
|  |         OnSuccessAndFaultListener successAndFaultListener =   new OnSuccessAndFaultListener() { | |||
|  |             @Override | |||
|  |             public void onSuccess(String result) { | |||
|  |                 error_try=0; | |||
|  |                 Log.d(TAG, String.format("init data HttpMethods successed on init, trycnt %s   " ,error_try)); | |||
|  |                 // 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(); | |||
|  |                 } | |||
|  |                 handler.sendEmptyMessage(1); | |||
|  | 
 | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onFault(String errorMsg) { | |||
|  |                 //失败 | |||
|  | 
 | |||
|  |             //    Log.d(TAG, String.format("init data HttpMethods error on init, trycnt %s  " ,error_try )); | |||
|  |                 //  getSearchTabTtitle(); | |||
|  |                 retryErorr(0); | |||
|  |             } | |||
|  |         }; | |||
|  |           showProgressDialog(false, "正在加载..."); | |||
|  |         BookSubscribe.getCateNovelList(cate, pageNo, tab1Pos+1,  Constants.lstProgressType.get(tab3Pos).getId(), new OnSuccessAndFaultSub(successAndFaultListener, getActivity())); | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  |     @Override | |||
|  |     public void initViews(){ | |||
|  |         appBarLayout.setVisibility(View.GONE); | |||
|  |         if(lstNt==null) { | |||
|  |             CommonUtil.getSearchTabTtitle(getActivity()); | |||
|  |             showProgressDialog(false,"正在加载..."); | |||
|  |             new Thread(){ | |||
|  |                 @Override | |||
|  |                 public void run() { | |||
|  |                     Log.d(TAG, "prepare book to load options for search"  ); | |||
|  |                     while(lstNt==null){ | |||
|  |                         try { | |||
|  |                             Thread.sleep(100); | |||
|  |                         } catch (InterruptedException e) { | |||
|  |                             e.printStackTrace(); | |||
|  |                         } | |||
|  |                     } | |||
|  | 
 | |||
|  |                     handler.sendEmptyMessage(MSG_INIT_TAB); | |||
|  |                 } | |||
|  |             }.start(); | |||
|  | 
 | |||
|  |         } | |||
|  |         else { | |||
|  |             initTabs(); | |||
|  |         } | |||
|  | 
 | |||
|  |         initLoadMoreListener(); | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |      void initTabs() { | |||
|  |         tmp=0; | |||
|  |      /* | |||
|  |         while(lstNt==null){ | |||
|  |             try { | |||
|  |                 Thread.sleep(10); | |||
|  |             } catch (InterruptedException e) { | |||
|  |                 e.printStackTrace(); | |||
|  |             } | |||
|  |         }*/ | |||
|  | 
 | |||
|  | 
 | |||
|  |           List<String> lstType = new ArrayList<String>()  ;//Arrays.asList(lstProgress); | |||
|  |           for(ProgressType progressType : Constants.lstProgressType){ | |||
|  |               lstType.add(progressType.getName()); | |||
|  |           } | |||
|  |          topic_layout.initTabNames(lstNt) ; | |||
|  |          topic_layout1.initTabNames( lstSex) ; | |||
|  |          topic_layout2.initTabNames(lstType) ; | |||
|  |        // topic_layout.initTabNames(lstNt); | |||
|  |         appBarLayout.setVisibility(View.VISIBLE); | |||
|  |         topic_layout.setOnTabSelectListener(new MultiLayout.OnTabSelectListener() { | |||
|  |             @Override | |||
|  |             public void select(TextView textView, int i, int i1) { | |||
|  |                 Log.d(TAG, String.format("select: text %s,i %s i1 %s ",textView.getText(),i,i1)); | |||
|  |                 tab2Pos = i1; | |||
|  |                tabChanged(); | |||
|  |             } | |||
|  | 
 | |||
|  | 
 | |||
|  |         }); | |||
|  |         topic_layout1.setOnTabSelectListener(new MultiLayout.OnTabSelectListener() { | |||
|  |             @Override | |||
|  |             public void select(TextView textView, int i, int i1) { | |||
|  |                 Log.d(TAG, String.format("select: text %s,i %s i1 %s ",textView.getText(),i,i1)); | |||
|  |                 tab1Pos = i1; | |||
|  |                 tabChanged(); | |||
|  |             } | |||
|  | 
 | |||
|  |         }); | |||
|  |         topic_layout2.setOnTabSelectListener(new MultiLayout.OnTabSelectListener() { | |||
|  |             @Override | |||
|  |             public void select(TextView textView, int i, int i1) { | |||
|  |                 Log.d(TAG, String.format("select: text %s,i %s i1 %s ",textView.getText(),i,i1)); | |||
|  |                 tab3Pos = i1; | |||
|  |                 tabChanged(); | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  |      // tabChanged(); | |||
|  | 
 | |||
|  |     } | |||
|  |     int tmp=0; | |||
|  |     void tabChanged() { | |||
|  | 
 | |||
|  |         Log.d(TAG, String.format("tabChanged: to load data tmp %s" , tmp )); | |||
|  |         tmp++; | |||
|  | 
 | |||
|  |        /* Toast.makeText(activity,   String.format( "TAB1 :{0}, TAB2 :{1}, TAB3 :{3}", | |||
|  |                 mData1.get(tab1Pos).getTabTitle(), | |||
|  |                 mData2.get(tab2Pos).getTabTitle(), | |||
|  |                 mData3.get(tab3Pos).getTabTitle()), | |||
|  |                 Toast.LENGTH_SHORT).show(); | |||
|  |  String selectedKey = mData1.get(tab1Pos).getTabTitle() +" " + | |||
|  |                mData2.get(tab2Pos).getTabTitle() +" " + | |||
|  |                mData3.get(tab3Pos).getTabTitle() ; | |||
|  |         */ | |||
|  |        if(lstSex==null || lstSex.size()==0) return; | |||
|  | 
 | |||
|  |        pageNo=1; | |||
|  | 
 | |||
|  |         String sex = lstSex.get(tab1Pos); | |||
|  |         String cate = lstNt.get(tab2Pos); | |||
|  |         String selectedKey = lstSex.get(tab1Pos) +" " + | |||
|  |                 cate +" " +  Constants.lstProgressType.get(tab3Pos).getName(); | |||
|  |               //  lstProgress[tab3Pos]  ; | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |       //  Toast.makeText(activity,  selectedKey, Toast.LENGTH_SHORT).show(); | |||
|  |         tvHint.setText(selectedKey); | |||
|  |         Log.d(TAG, String.format("tabChanged: to load data %s,pageno %s, tmp %s" , selectedKey,pageNo ,tmp)); | |||
|  | 
 | |||
|  |         if( tmp<lstProgress.length) return; | |||
|  | 
 | |||
|  |         loadSearchData(); | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  |     private void initLoadMoreListener() { | |||
|  | 
 | |||
|  |         rvPaihang.addOnScrollListener(new RecyclerView.OnScrollListener() { | |||
|  |             int lastVisibleItem ,visibleItemCount,totalItemCount,pastVisiblesItems; | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onScrollStateChanged(RecyclerView recyclerView, int newState) { | |||
|  |                 super.onScrollStateChanged(recyclerView, newState); | |||
|  | 
 | |||
|  |                 //判断RecyclerView的状态 是空闲时,同时,是最后一个可见的ITEM时才加载 | |||
|  | 
 | |||
|  |                 Log.d(TAG, String.format("onScrollStateChanged , state %s ,lastVisibleitem %s ,getItemCount %s,pageno %s ,pageCount %s", | |||
|  |                         newState==RecyclerView.SCROLL_STATE_IDLE,lastVisibleItem,mAdapter.getItemCount(),pageNo,getPageCount())); | |||
|  |                 if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==mAdapter.getItemCount()){ | |||
|  | 
 | |||
|  |                     if(pageNo <= getPageCount()) { | |||
|  |                         //设置正在加载更多 | |||
|  |                         mAdapter.changeMoreStatus(mAdapter.LOADING_MORE); | |||
|  | 
 | |||
|  |                         //改为网络请求 | |||
|  |                       loadSearchData(); | |||
|  |                     }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(); | |||
|  | 
 | |||
|  |                // if (dy > 0) { | |||
|  |                     visibleItemCount = layoutManager.getChildCount(); | |||
|  |                     totalItemCount = layoutManager.getItemCount(); | |||
|  |                     pastVisiblesItems = layoutManager.findFirstVisibleItemPosition(); | |||
|  | 
 | |||
|  | 
 | |||
|  |              //   } | |||
|  |                 Log.d(TAG, String.format("onScrolled:dy %s,visibleItemCount %s,totalItemCount %s,pastVisiblesItems %s ", | |||
|  |                       dy,  visibleItemCount,totalItemCount,pastVisiblesItems)); | |||
|  |                     //最后一个可见的ITEM | |||
|  |                 lastVisibleItem=layoutManager.findLastVisibleItemPosition(); | |||
|  |                 lastVisibleItem=layoutManager.findLastCompletelyVisibleItemPosition(); | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  | //----------------绑定列表 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     void initialBookList() { | |||
|  |      //   rvPaihang.setLayoutManager(new LinearLayoutManager(activity)); | |||
|  |     //    rvPaihang.setAdapter(mAdapter); | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     @BindView(R.id.tvHint) | |||
|  |     TextView tvHint; | |||
|  |     //------------滑动监听 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void setFTag() { | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void onResume() { | |||
|  |         super.onResume(); | |||
|  |         MobclickAgent.onPageStart(TAG); | |||
|  |     } | |||
|  |     @Override | |||
|  |     public void onPause(){ | |||
|  |         super.onPause(); | |||
|  |         MobclickAgent.onPageEnd(TAG); | |||
|  |     } | |||
|  | } |