315 lines
9.5 KiB
Plaintext
315 lines
9.5 KiB
Plaintext
|
package com.novelbook.android;
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.content.Intent;
|
||
|
import android.os.Bundle;
|
||
|
import android.os.Handler;
|
||
|
import android.os.Message;
|
||
|
import android.support.annotation.NonNull;
|
||
|
import android.support.v7.app.AppCompatActivity;
|
||
|
import android.support.v7.widget.GridLayoutManager;
|
||
|
import android.support.v7.widget.RecyclerView;
|
||
|
import android.support.v7.widget.Toolbar;
|
||
|
import android.util.Log;
|
||
|
import android.view.LayoutInflater;
|
||
|
import android.view.Menu;
|
||
|
import android.view.MenuItem;
|
||
|
import android.view.View;
|
||
|
import android.view.ViewGroup;
|
||
|
import android.view.Window;
|
||
|
import android.widget.ImageView;
|
||
|
import android.widget.TextView;
|
||
|
import android.widget.Toast;
|
||
|
|
||
|
|
||
|
import com.google.gson.JsonArray;
|
||
|
import com.google.gson.JsonElement;
|
||
|
import com.google.gson.JsonObject;
|
||
|
import com.google.gson.JsonParser;
|
||
|
import com.novelbook.android.bean.Cataloge;
|
||
|
import com.novelbook.android.bean.NovelBlock;
|
||
|
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.ImageUtil;
|
||
|
|
||
|
import org.json.JSONObject;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.List;
|
||
|
|
||
|
import butterknife.BindView;
|
||
|
import butterknife.ButterKnife;
|
||
|
|
||
|
public class activity_cates extends Activity_base {
|
||
|
public final static String TAG = activity_cates.class.getSimpleName();
|
||
|
@BindView(R.id.recycleViewCateList)
|
||
|
RecyclerView mRecyclerView;
|
||
|
private BookListAdapter mAdapter;
|
||
|
private List<Cataloge> mCataloges;
|
||
|
|
||
|
@Override
|
||
|
public int getLayoutRes() {
|
||
|
return R.layout.activity_cates;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void initViews() {
|
||
|
creatToolbar();
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public void fillData() {
|
||
|
List<Cataloge> cates = new ArrayList<>();
|
||
|
for(Cataloge cate:mCataloges){
|
||
|
if(cate!=null && cate.getNovelCount()>0){
|
||
|
cates.add(cate);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mAdapter = new BookListAdapter(this, 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 setTitle() {
|
||
|
|
||
|
}
|
||
|
|
||
|
interface OnItemClickLitener
|
||
|
{
|
||
|
void onItemClick(View view, int position);
|
||
|
|
||
|
}
|
||
|
|
||
|
private void showCateList(String s) {
|
||
|
Intent intent = new Intent(activity_cates.this, Activity_cate_books.class);
|
||
|
intent.putExtra(Activity_cate_books.EXTRNAME,s);
|
||
|
startActivity(intent);
|
||
|
|
||
|
}
|
||
|
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
|
||
|
protected void initData() {
|
||
|
|
||
|
|
||
|
showProgressDialog(true, "正在加载");
|
||
|
BookSubscribe.getCates(Constants.SEX,new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
|
||
|
@Override
|
||
|
public void onSuccess(String result) {
|
||
|
|
||
|
// 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, "error on get firstpage: " + errorMsg);
|
||
|
handler.sendEmptyMessage(2);
|
||
|
}
|
||
|
}, this));
|
||
|
|
||
|
|
||
|
}
|
||
|
@Override
|
||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||
|
// Inflate the menu; this adds items to the action bar if it is present.
|
||
|
getMenuInflater().inflate(R.menu.booksearch, menu);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
private void creatToolbar() {
|
||
|
|
||
|
setSupportActionBar(toolbar);
|
||
|
|
||
|
|
||
|
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
|
||
|
@Override
|
||
|
public boolean onMenuItemClick(MenuItem item) {
|
||
|
int menuItemId = item.getItemId();
|
||
|
if(menuItemId==R.id.menuSearch){
|
||
|
Intent intent = new Intent(activity_cates.this, Activity_Search.class);
|
||
|
startActivity(intent);
|
||
|
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void initReceyleView() {
|
||
|
|
||
|
// mRecyclerView.setLayoutManager(new LinearLayoutManager(this.activity));
|
||
|
mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
|
||
|
mRecyclerView.setAdapter(mAdapter );
|
||
|
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|