314 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
		
		
			
		
	
	
			314 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
|  | package com.novelbook.android.Fragments; | ||
|  | 
 | ||
|  | 
 | ||
|  | import android.content.Context; | ||
|  | import android.content.Intent; | ||
|  | import android.os.Bundle; | ||
|  | import android.support.v4.app.Fragment; | ||
|  | import android.support.v7.widget.GridLayoutManager; | ||
|  | import android.support.v7.widget.RecyclerView; | ||
|  | import android.util.Log; | ||
|  | import android.view.LayoutInflater; | ||
|  | import android.view.View; | ||
|  | import android.view.ViewGroup; | ||
|  | import android.widget.ImageView; | ||
|  | import android.widget.TextView; | ||
|  | 
 | ||
|  | import com.google.gson.JsonArray; | ||
|  | import com.google.gson.JsonElement; | ||
|  | import com.google.gson.JsonObject; | ||
|  | import com.google.gson.JsonParser; | ||
|  | import com.novelbook.android.Activity_cate_books; | ||
|  | import com.novelbook.android.R; | ||
|  | import com.novelbook.android.activity_cates; | ||
|  | import com.novelbook.android.bean.Cataloge; | ||
|  | 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.ImageUtil; | ||
|  | import com.umeng.analytics.MobclickAgent; | ||
|  | 
 | ||
|  | import org.json.JSONObject; | ||
|  | 
 | ||
|  | import java.util.ArrayList; | ||
|  | import java.util.Iterator; | ||
|  | import java.util.List; | ||
|  | 
 | ||
|  | import butterknife.BindView; | ||
|  | import butterknife.ButterKnife; | ||
|  | 
 | ||
|  | 
 | ||
|  | public class FragmentCates  extends BasicFragment { | ||
|  |  public static final String TAG =FragmentCates.class.getSimpleName(); | ||
|  |    @BindView(R.id.mRecyclerView) | ||
|  |    RecyclerView mRecyclerView; | ||
|  |    private BookListAdapter mAdapter; | ||
|  |    private List<Cataloge> mCataloges; | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected int getLayoutRes() { | ||
|  |         return R.layout.fragment_cates; | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected void initData() { | ||
|  | 
 | ||
|  |        showProgressDialog(true, "正在加载"); | ||
|  |        BookSubscribe.getCates(Constants.SEX,new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() { | ||
|  |           @Override | ||
|  |           public void onSuccess(String result) { | ||
|  |               Log.d(TAG, String.format("init data HttpMethods successed on init, trycnt %s   " ,error_try)); | ||
|  |               error_try =0; | ||
|  |              // mFirstPage= gson.fromJson(result, FirstPage.class); | ||
|  |              try { | ||
|  |                 JSONObject jsonObject = new JSONObject(result); | ||
|  |                 String resultstr = jsonObject.getString("nts"); | ||
|  |                 mCataloges = parserJsonArray(resultstr); | ||
|  |                 handler.sendEmptyMessage(1); | ||
|  | 
 | ||
|  |              } 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 )); | ||
|  |               retryErorr(2); | ||
|  |           } | ||
|  |        }, getActivity())); | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected void fillData() { | ||
|  |        List<Cataloge> cates = new ArrayList<>(); | ||
|  |        for(Cataloge cate:mCataloges){ | ||
|  |           if(cate!=null && cate.getNovelCount()>0){ | ||
|  |              cates.add(cate); | ||
|  |           } | ||
|  |        } | ||
|  | 
 | ||
|  |        mAdapter = new  BookListAdapter(getActivity(), cates, R.layout.recycle_list_item_cates, new  OnItemClickLitener() { | ||
|  | 
 | ||
|  |           @Override | ||
|  |           public void onItemClick(View view, int position) { | ||
|  |              Cataloge cl = cates.get(position); | ||
|  |              showCateList(cl.getName()); | ||
|  | 
 | ||
|  |           } | ||
|  |        }); | ||
|  |        initReceyleView(); | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected void initViews() { | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  |     public FragmentCates() { | ||
|  |         // Required empty public constructor | ||
|  |     } | ||
|  | 
 | ||
|  |    void initReceyleView() { | ||
|  | 
 | ||
|  |       //  mRecyclerView.setLayoutManager(new LinearLayoutManager(this.activity)); | ||
|  |       mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3)); | ||
|  |       mRecyclerView.setAdapter(mAdapter ); | ||
|  | 
 | ||
|  |    } | ||
|  | 
 | ||
|  | 
 | ||
|  |     // TODO: Rename and change types and number of parameters | ||
|  |     public static FragmentCates newInstance() { | ||
|  |         FragmentCates fragment = new FragmentCates(); | ||
|  | 
 | ||
|  |         return fragment; | ||
|  |     } | ||
|  | 
 | ||
|  |    private void showCateList(String s) { | ||
|  |       Intent intent = new Intent(getActivity(), Activity_cate_books.class); | ||
|  |       intent.putExtra(Activity_cate_books.EXTRNAME,s); | ||
|  |       startActivity(intent); | ||
|  | 
 | ||
|  |    } | ||
|  | 
 | ||
|  | 
 | ||
|  |     @Override | ||
|  |     protected void processArguments() { | ||
|  | 
 | ||
|  |     } | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     @Override | ||
|  |     public void setFTag() { | ||
|  | 
 | ||
|  |     } | ||
|  |    class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.MyViewHolder> { | ||
|  |       private final int EMPTY_VIEW = 1; | ||
|  |       private final int PROGRESS_VIEW = 2; | ||
|  |       private final int IMAGE_VIEW = 3; | ||
|  | 
 | ||
|  |       private Context context; | ||
|  |       private List<Cataloge> mDatas = new ArrayList<Cataloge>(); | ||
|  |       private OnItemClickLitener mOnItemClickLitener; | ||
|  |       private int listItemID; | ||
|  |       public BookListAdapter(Context context, List<Cataloge> mDatas, int listItemID, OnItemClickLitener clickLitener) { | ||
|  |          this.context = context; | ||
|  |          this.mDatas = mDatas; | ||
|  |          this.mOnItemClickLitener = clickLitener; | ||
|  |          this.listItemID = listItemID; | ||
|  |       } | ||
|  |       public BookListAdapter(Context context, OnItemClickLitener clickLitener) { | ||
|  |          this.context = context; | ||
|  |          this.mOnItemClickLitener = clickLitener; | ||
|  | 
 | ||
|  |       } | ||
|  | 
 | ||
|  |       @Override | ||
|  |       public int getItemViewType(int position) { | ||
|  |          if(mDatas.size() == 0){ | ||
|  |             return EMPTY_VIEW; | ||
|  |          } else if(mDatas.get(position) == null){ | ||
|  |             return PROGRESS_VIEW; | ||
|  |          } else { | ||
|  |             return super.getItemViewType(position); | ||
|  |          } | ||
|  |       } | ||
|  | 
 | ||
|  |       @Override | ||
|  |       public  BookListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) | ||
|  |       { | ||
|  |          BookListAdapter.MyViewHolder holder = new BookListAdapter.MyViewHolder(LayoutInflater.from( | ||
|  |                  context).inflate(listItemID, parent, | ||
|  |                  false)); | ||
|  |          return holder; | ||
|  |       } | ||
|  | 
 | ||
|  | 
 | ||
|  | 
 | ||
|  |       public void setParameters(List<Cataloge> mDatas,int listItemID ) { | ||
|  |          this.mDatas = mDatas; | ||
|  |          this.listItemID = listItemID; | ||
|  |       } | ||
|  | 
 | ||
|  |       public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) | ||
|  |       { | ||
|  |          this.mOnItemClickLitener = mOnItemClickLitener; | ||
|  |       } | ||
|  | 
 | ||
|  |       @Override | ||
|  |       public void onBindViewHolder(BookListAdapter.MyViewHolder holder, int position) | ||
|  |       { | ||
|  |          holder.tvTitle.setText(mDatas.get(position).getName()); | ||
|  |          holder.tvNum.setText(mDatas.get(position).getNovelCount()+""); | ||
|  |          if( mDatas.get(position).getNovel()!=null) { | ||
|  |             ImageUtil.loadImage(context, mDatas.get(position).getNovel().getCover(), holder.imageView); | ||
|  |          } | ||
|  | 
 | ||
|  | 
 | ||
|  |          // 如果设置了回调,则设置点击事件 | ||
|  |          if (mOnItemClickLitener != null) | ||
|  |          { | ||
|  |             holder.itemView.setOnClickListener(new View.OnClickListener() | ||
|  |             { | ||
|  |                @Override | ||
|  |                public void onClick(View v) | ||
|  |                { | ||
|  |                   int pos = holder.getLayoutPosition(); | ||
|  |                   mOnItemClickLitener.onItemClick(holder.itemView, pos); | ||
|  |                } | ||
|  |             }); | ||
|  |          } | ||
|  |       } | ||
|  | 
 | ||
|  |       @Override | ||
|  |       public int getItemCount() | ||
|  |       { | ||
|  |          return mDatas.size(); | ||
|  |       } | ||
|  |       public void addData(int position) { | ||
|  |          //   mDatas.add(position, "Insert One"); | ||
|  |          //    notifyItemInserted(position); | ||
|  |       } | ||
|  | 
 | ||
|  |       public void removeData(int position) { | ||
|  |          mDatas.remove(position); | ||
|  |          notifyItemRemoved(position); | ||
|  |       } | ||
|  |       class MyViewHolder extends RecyclerView.ViewHolder | ||
|  |       { | ||
|  |          @BindView(R.id.tvCateName) | ||
|  |          TextView tvTitle; | ||
|  |          @BindView(R.id.tvCateNums) | ||
|  |          TextView tvNum; | ||
|  |          @BindView(R.id.imageViewCate) | ||
|  |          ImageView imageView; | ||
|  | 
 | ||
|  |          public MyViewHolder(View view) | ||
|  |          { | ||
|  |             super(view); | ||
|  |             ButterKnife.bind(this, view); | ||
|  |             //tvTitle = (TextView) view.findViewById(R.id.title); | ||
|  |             // tvAuthor = (TextView) view.findViewById(R.id.author); | ||
|  | 
 | ||
|  |          } | ||
|  |       } | ||
|  |    } | ||
|  |    interface OnItemClickLitener | ||
|  |    { | ||
|  |       void onItemClick(View view, int position); | ||
|  | 
 | ||
|  |    } | ||
|  |    public List<Cataloge> parserJsonArray(String strJson) { | ||
|  | 
 | ||
|  |       List<Cataloge> list = new ArrayList<Cataloge>(); | ||
|  |       //创建一个Gson对象 | ||
|  |       // Gson gson = new Gson(); | ||
|  |       //创建一个JsonParser | ||
|  |       JsonParser parser = new JsonParser(); | ||
|  |       //通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象 | ||
|  |       JsonElement el = parser.parse(strJson); | ||
|  | 
 | ||
|  |       //把JsonElement对象转换成JsonObject | ||
|  |       JsonObject jsonObj = null; | ||
|  |       if (el.isJsonObject()) { | ||
|  |          jsonObj = el.getAsJsonObject(); | ||
|  |       } | ||
|  | 
 | ||
|  | 
 | ||
|  |       //把JsonElement对象转换成JsonArray | ||
|  |       JsonArray jsonArray = null; | ||
|  |       if (el.isJsonArray()) { | ||
|  |          jsonArray = el.getAsJsonArray(); | ||
|  |       } | ||
|  | 
 | ||
|  |       //遍历JsonArray对象 | ||
|  |       Iterator it = jsonArray.iterator(); | ||
|  |       while (it.hasNext()) { | ||
|  |          JsonElement e = (JsonElement) it.next(); | ||
|  |          //JsonElement转换为JavaBean对象 | ||
|  |          list.add((Cataloge) gson.fromJson(e, Cataloge.class)); | ||
|  |       } | ||
|  |       return list; | ||
|  |    } | ||
|  | 
 | ||
|  |    @Override | ||
|  |     public void onResume() { | ||
|  |         super.onResume(); | ||
|  | 
 | ||
|  |         MobclickAgent.onPageStart(TAG); | ||
|  |     } | ||
|  | 
 | ||
|  |     @Override | ||
|  |     public void onPause(){ | ||
|  |         super.onPause(); | ||
|  |         MobclickAgent.onPageEnd(TAG); | ||
|  |     } | ||
|  | } |