210 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			210 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| package com.novelbook.android.adapter;
 | |
| 
 | |
| import android.content.Context;
 | |
| import android.support.v7.widget.RecyclerView;
 | |
| import android.text.TextUtils;
 | |
| import android.view.LayoutInflater;
 | |
| import android.view.View;
 | |
| import android.view.ViewGroup;
 | |
| import android.widget.ImageView;
 | |
| import android.widget.LinearLayout;
 | |
| import android.widget.TextView;
 | |
| 
 | |
| import com.novelbook.android.R;
 | |
| import com.novelbook.android.bean.NovelBlock;
 | |
| import com.novelbook.android.db.Novel;
 | |
| import com.novelbook.android.utils.ImageUtil;
 | |
| import com.novelbook.android.utils.OnItemClickListener;
 | |
| 
 | |
| import java.util.ArrayList;
 | |
| import java.util.List;
 | |
| 
 | |
| import butterknife.BindView;
 | |
| import butterknife.ButterKnife;
 | |
| 
 | |
| public class BandanAdapterNew extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
 | |
|    private final int EMPTY_VIEW = 1;
 | |
|    private final int PROGRESS_VIEW = 2;
 | |
|    private final int IMAGE_VIEW = 3;
 | |
| 
 | |
|    private Context context;
 | |
|    private List<NovelBlock> mDatas = new ArrayList<NovelBlock>();
 | |
|    private OnItemClickListener mOnItemClickLitener;
 | |
|    private int listItemID;
 | |
|     private boolean showFootView =false;
 | |
| 
 | |
|     public void setShowFootView(boolean showFootView) {
 | |
|         this.showFootView = showFootView;
 | |
|     }
 | |
|    public BandanAdapterNew(Context context, List<NovelBlock> mDatas, int listItemID, OnItemClickListener clickLitener) {
 | |
|        this.context = context;
 | |
|        this.mDatas = mDatas;
 | |
|        this.mOnItemClickLitener = clickLitener;
 | |
|        this.listItemID = listItemID;
 | |
|    }
 | |
|    public BandanAdapterNew(Context context, OnItemClickListener 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  RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
 | |
|    {
 | |
|        if(viewType == EMPTY_VIEW){
 | |
|            EmptyViewHolder  holder = new EmptyViewHolder (LayoutInflater.from(context).inflate(R.layout.recycle_list_empty_item, parent, false));
 | |
|            return holder;
 | |
|        }
 | |
| 
 | |
|        MyViewHolder holder = new  MyViewHolder(LayoutInflater.from(context).inflate(listItemID, parent,
 | |
|                         false));
 | |
|         return holder;
 | |
|    }
 | |
| 
 | |
| 
 | |
| 
 | |
|    public void setParameters(List<NovelBlock> mDatas,int listItemID ) {
 | |
|        this.mDatas = mDatas;
 | |
|        this.listItemID = listItemID;
 | |
|    }
 | |
| 
 | |
|    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener)
 | |
|    {
 | |
|        this.mOnItemClickLitener = mOnItemClickLitener;
 | |
|    }
 | |
| 
 | |
|    @Override
 | |
|    public void onBindViewHolder(RecyclerView.ViewHolder hd, int position)
 | |
|    {
 | |
|        if (hd instanceof EmptyViewHolder) {
 | |
|            EmptyViewHolder   holder = (EmptyViewHolder)hd;
 | |
|            holder.tvEmpty.setVisibility(View.VISIBLE);
 | |
|            holder.tvEmpty.setText(R.string.noRecord);
 | |
|            return;
 | |
|        }
 | |
| 
 | |
| 
 | |
|        MyViewHolder   holder = (MyViewHolder)hd;
 | |
|        NovelBlock nb = mDatas.get(position);
 | |
|        holder.tvCateName.setText(nb.getName());
 | |
|        if(nb==null){return;}
 | |
|        String nvs = "";
 | |
|        String pic ="";
 | |
|        for(Novel nv : nb.getNs()) {
 | |
|            if (nv != null) {
 | |
|                nvs +=TextUtils.isEmpty(nvs)?nv.getName() : "  " + nv.getName();
 | |
|               if(TextUtils.isEmpty(pic)) {
 | |
|                   pic = nv.getCover();
 | |
|               }
 | |
|            }
 | |
|        }
 | |
| 
 | |
|        holder.tvNoves.setText(nvs);
 | |
| 
 | |
|       ImageUtil.loadImage(context,pic,holder.imageView);
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|        // 如果设置了回调,则设置点击事件
 | |
|        if (mOnItemClickLitener != null)
 | |
|        {
 | |
|            holder.itemView.setOnClickListener(new View.OnClickListener() //show more cate paihang
 | |
|            {
 | |
|                @Override
 | |
|                public void onClick(View v)
 | |
|                {
 | |
|                    int pos = holder.getLayoutPosition();
 | |
|                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
 | |
|                }
 | |
|            });
 | |
| 
 | |
|            holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
 | |
|            {
 | |
|                @Override
 | |
|                public boolean onLongClick(View v)
 | |
|                {
 | |
|                    int pos = holder.getLayoutPosition();
 | |
|                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
 | |
|                    return false;
 | |
|                }
 | |
|            });
 | |
| 
 | |
| 
 | |
|            holder.ll1.setOnClickListener(new View.OnClickListener() { //show bookdetail
 | |
|                @Override
 | |
|                public void onClick(View v)
 | |
|                {
 | |
|                    int pos = position;
 | |
|                    mOnItemClickLitener.onLinearOutClick(holder.itemView, pos,0);
 | |
|                }
 | |
|            });
 | |
| 
 | |
| 
 | |
|        }
 | |
|    }
 | |
| 
 | |
|    @Override
 | |
|    public int getItemCount()
 | |
|    {
 | |
|        return mDatas.size();
 | |
|    }
 | |
|    public void addData(int position) {
 | |
|       // mDatas.add(position, "Insert One");
 | |
|        notifyItemInserted(position);
 | |
|    }
 | |
|     public void setData(List<NovelBlock> items) {
 | |
|         // mDatas.add(position, "Insert One");
 | |
|         mDatas = items;
 | |
|         notifyDataSetChanged();
 | |
|     }
 | |
|    public void removeData(int position) {
 | |
|        mDatas.remove(position);
 | |
|        notifyItemRemoved(position);
 | |
|    }
 | |
| 
 | |
|     public void AddFooterItem(List<NovelBlock> items) {
 | |
|         mDatas.addAll(items);
 | |
|         notifyDataSetChanged();
 | |
|     }
 | |
|     class EmptyViewHolder extends RecyclerView.ViewHolder {
 | |
|         @BindView(R.id.tvLoadText)
 | |
|         TextView tvEmpty;
 | |
|         public EmptyViewHolder(View itemView) {
 | |
|             super(itemView);
 | |
|             ButterKnife.bind(this,itemView);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|    class MyViewHolder extends RecyclerView.ViewHolder
 | |
|    {
 | |
|       @BindView(R.id.ll1)
 | |
|       LinearLayout ll1;
 | |
|        @BindView(R.id.tvCateName)
 | |
|        TextView tvCateName;
 | |
|        @BindView(R.id.imageView)
 | |
|        ImageView imageView;
 | |
|        @BindView(R.id.tvNovels)
 | |
|        TextView tvNoves;
 | |
|        public MyViewHolder(View view)
 | |
|        {
 | |
|            super(view);
 | |
|            ButterKnife.bind(this, view);
 | |
|            //tvTitle = (TextView) view.findViewById(R.id.title);
 | |
|            // tvAuthor = (TextView) view.findViewById(R.id.author);
 | |
| 
 | |
|        }
 | |
|    }
 | |
| } |