847 lines
		
	
	
		
			34 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
		
		
			
		
	
	
			847 lines
		
	
	
		
			34 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
|  | package com.novelbook.android.adapter; | |||
|  | 
 | |||
|  | import android.annotation.SuppressLint; | |||
|  | import android.app.Activity; | |||
|  | import android.content.Context; | |||
|  | import android.support.annotation.NonNull; | |||
|  | import android.support.v7.widget.GridLayoutManager; | |||
|  | import android.support.v7.widget.RecyclerView; | |||
|  | import android.support.v7.widget.StaggeredGridLayoutManager; | |||
|  | import android.text.TextUtils; | |||
|  | import android.util.Log; | |||
|  | import android.view.LayoutInflater; | |||
|  | import android.view.View; | |||
|  | import android.view.ViewGroup; | |||
|  | import android.widget.Button; | |||
|  | import android.widget.FrameLayout; | |||
|  | import android.widget.ImageView; | |||
|  | import android.widget.LinearLayout; | |||
|  | import android.widget.ProgressBar; | |||
|  | import android.widget.TextView; | |||
|  | 
 | |||
|  | import com.androidquery.callback.AQuery2; | |||
|  | import com.androidquery.callback.ImageOptions; | |||
|  | import com.bytedance.sdk.openadsdk.DownloadStatusController; | |||
|  | import com.bytedance.sdk.openadsdk.TTAdConstant; | |||
|  | import com.bytedance.sdk.openadsdk.TTAppDownloadListener; | |||
|  | import com.bytedance.sdk.openadsdk.TTFeedAd; | |||
|  | import com.bytedance.sdk.openadsdk.TTImage; | |||
|  | import com.bytedance.sdk.openadsdk.TTNativeAd; | |||
|  | import com.novelbook.android.AD.toutiao.TToast; | |||
|  | import com.novelbook.android.BookActivity; | |||
|  | import com.novelbook.android.R; | |||
|  | import com.novelbook.android.db.Novel; | |||
|  | 
 | |||
|  | import com.novelbook.android.netutils.NetUtil; | |||
|  | import com.novelbook.android.utils.CommonUtil; | |||
|  | import com.novelbook.android.utils.ImageUtil; | |||
|  | import com.novelbook.android.utils.MyImageLoader; | |||
|  | import com.novelbook.android.utils.OnItemClickListener; | |||
|  | import com.qq.e.ads.nativ.NativeExpressADView; | |||
|  | 
 | |||
|  | import java.util.ArrayList; | |||
|  | import java.util.List; | |||
|  | import java.util.Map; | |||
|  | import java.util.WeakHashMap; | |||
|  | 
 | |||
|  | import butterknife.BindView; | |||
|  | import butterknife.ButterKnife; | |||
|  | import okhttp3.internal.Util; | |||
|  | 
 | |||
|  | import static com.novelbook.android.utils.ImageUtil.loader; | |||
|  | 
 | |||
|  | public  class BookListAdapter extends RecyclerView.Adapter< RecyclerView.ViewHolder> { | |||
|  |     private final static String TAG= BookListAdapter.class.getSimpleName(); | |||
|  |     private final int EMPTY_VIEW = 1; | |||
|  | 
 | |||
|  |     private static final int TYPE_ITEM   = 2; | |||
|  |     private static final int TYPE_FOOTER = 3; | |||
|  |     private static final int NO_FOOTER = 4; | |||
|  |     private static final int ITEM_VIEW_TYPE_GROUP_PIC_AD =5; | |||
|  |     private static final int ITEM_VIEW_TYPE_SMALL_PIC_AD = 6; | |||
|  |     private static final int ITEM_VIEW_TYPE_LARGE_PIC_AD = 7; | |||
|  |     private static final int ITEM_VIEW_TYPE_VIDEO = 8; | |||
|  |     private static final int ITEM_VIEW_TYPE_QQ_NATIVE_EXPRESS = 9; | |||
|  | 
 | |||
|  | 
 | |||
|  |    // private MyImageLoader loader = new MyImageLoader(); | |||
|  |     private Context mContext; | |||
|  |     private List mDatas  ; | |||
|  | 
 | |||
|  |     private OnItemClickListener mOnItemClickListener; | |||
|  |     private int listItemID; | |||
|  |     //private List<Novel> mBooks; | |||
|  |     private LayoutInflater mInflater; | |||
|  | 
 | |||
|  |     //上拉加载更多 | |||
|  |     public static final int PULLUP_LOAD_MORE = 0; | |||
|  |     //正在加载中 | |||
|  |     public static final int LOADING_MORE     = 1; | |||
|  |     //没有加载更多 隐藏 | |||
|  |     public static final int NO_LOAD_MORE     = 2; | |||
|  | 
 | |||
|  |     //上拉加载更多状态-默认为0 | |||
|  |     private int mLoadMoreStatus = 2; | |||
|  |     private boolean showFootView =false; | |||
|  | 
 | |||
|  |     private float percent =0; | |||
|  |     private int norecord =R.string.noRecord; | |||
|  | 
 | |||
|  |     private boolean isShowTop=false; | |||
|  | 
 | |||
|  |     public boolean isShowTop() { | |||
|  |         return isShowTop; | |||
|  |     } | |||
|  | 
 | |||
|  |     public void setShowTop(boolean showTop) { | |||
|  |         isShowTop = showTop; | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     public void setNorecord(int norecord) { | |||
|  |         this.norecord = norecord; | |||
|  |     } | |||
|  | 
 | |||
|  |     public void setPercent(float percent) { | |||
|  |         this.percent = percent; | |||
|  |     } | |||
|  | 
 | |||
|  |     private AQuery2 mAQuery; | |||
|  |     private Map<AdViewHolder, TTAppDownloadListener> mTTAppDownloadListenerMap = new WeakHashMap<>(); | |||
|  |     public void setShowFootView(boolean showFootView) { | |||
|  |         this.showFootView = showFootView; | |||
|  |     } | |||
|  | 
 | |||
|  |     public BookListAdapter(Context context, List<Novel> datas) { | |||
|  |         mContext = context; | |||
|  |         mInflater = LayoutInflater.from(mContext); | |||
|  |         mDatas = datas; | |||
|  |         this.mAQuery = new AQuery2(context); | |||
|  |     } | |||
|  | 
 | |||
|  |     public BookListAdapter(Context context, List<Novel> mBooks, int listItemID, OnItemClickListener clickLitener) { | |||
|  |         this.mContext =context; | |||
|  |         this.mDatas = mBooks; | |||
|  |         this.mOnItemClickListener = clickLitener; | |||
|  |         this.listItemID = listItemID; | |||
|  |         mInflater = LayoutInflater.from(mContext); | |||
|  |         this.mAQuery = new AQuery2(context); | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     public BookListAdapter(Context context, OnItemClickListener clickLitener) { | |||
|  |         this.mContext = context; | |||
|  |         this.mOnItemClickListener = clickLitener; | |||
|  |         this.mAQuery = new AQuery2(context); | |||
|  |     } | |||
|  | 
 | |||
|  |     public void setListener(OnItemClickListener clickLitener){ | |||
|  |         this.mOnItemClickListener = clickLitener; | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public int getItemViewType(int position) { | |||
|  |         if (mDatas.size() == 0) { | |||
|  |             return EMPTY_VIEW; | |||
|  |         }else   if (  position < mDatas.size()) { | |||
|  |             //最后一个item设置为footerView | |||
|  |             if (mDatas.get(position) instanceof Novel) { | |||
|  |                 return TYPE_ITEM; | |||
|  |             } else if (mDatas.get(position) instanceof TTFeedAd) { | |||
|  |                 TTFeedAd ad = (TTFeedAd) mDatas.get(position); | |||
|  |                 Log.d(TAG, "getItemViewType: ad.getImageMode()= " +ad.getImageMode()); | |||
|  |                 if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_SMALL_IMG) { | |||
|  |                     return ITEM_VIEW_TYPE_SMALL_PIC_AD; | |||
|  |                 } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_LARGE_IMG) { | |||
|  |                     return ITEM_VIEW_TYPE_LARGE_PIC_AD; | |||
|  |                 } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_GROUP_IMG) { | |||
|  |                     return ITEM_VIEW_TYPE_GROUP_PIC_AD; | |||
|  |                 } else if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO) { | |||
|  |                     return ITEM_VIEW_TYPE_VIDEO; | |||
|  |                 } | |||
|  |                 return ITEM_VIEW_TYPE_SMALL_PIC_AD; | |||
|  |             }else if(mDatas.get(position) instanceof NativeExpressADView  ){ | |||
|  |                 return ITEM_VIEW_TYPE_QQ_NATIVE_EXPRESS; | |||
|  |             } | |||
|  |             Log.d(TAG, String.format("getItemViewType exception 1: position %s, mDatas.get(position) %s ,mDatas.size %s",position, mDatas.get(position),mDatas.size() )); | |||
|  |         } | |||
|  |         else   if (  position  == mDatas.size()) { | |||
|  |             //最后一个item设置为footerView | |||
|  |             if(!showFootView){ | |||
|  |                 return NO_FOOTER; | |||
|  |             } | |||
|  |             return TYPE_FOOTER; | |||
|  |         } else { | |||
|  |             return TYPE_ITEM; | |||
|  |         } | |||
|  |         Log.d(TAG, String.format("getItemViewType exception 2: position %s, mDatas.get(position) %s ,mDatas.size %s",position, mDatas.get(position),mDatas.size() )); | |||
|  |         return EMPTY_VIEW; | |||
|  | 
 | |||
|  |            // return super.getItemViewType(position); | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public  RecyclerView.ViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) { | |||
|  | 
 | |||
|  |         switch (viewType) { | |||
|  |             case TYPE_FOOTER: | |||
|  |                 return new FooterViewHolder(mInflater.inflate(R.layout.load_more_footview_layout, parent, false)); | |||
|  |             case EMPTY_VIEW: | |||
|  |                 return new EmptyViewHolder (mInflater.inflate(R.layout.recycle_list_empty_item, parent, false)); | |||
|  |             case NO_FOOTER: | |||
|  |                 return new NoShowViewHolder (mInflater.inflate(R.layout.recycle_list_noshow_item, parent, false)); | |||
|  |             case ITEM_VIEW_TYPE_SMALL_PIC_AD: | |||
|  |                 return new SmallAdViewHolder(mInflater.inflate(R.layout.listitem_ad_small_pic, parent, false)); | |||
|  |             case ITEM_VIEW_TYPE_LARGE_PIC_AD: | |||
|  |                 return new LargeAdViewHolder(mInflater.inflate(R.layout.listitem_ad_large_pic, parent, false)); | |||
|  |             case ITEM_VIEW_TYPE_GROUP_PIC_AD: | |||
|  |                 return new GroupAdViewHolder(mInflater.inflate(R.layout.listitem_ad_group_pic, parent, false)); | |||
|  |             case ITEM_VIEW_TYPE_VIDEO: | |||
|  |                 return new VideoAdViewHolder(mInflater.inflate(R.layout.listitem_ad_large_video, parent, false)); | |||
|  |             case ITEM_VIEW_TYPE_QQ_NATIVE_EXPRESS: | |||
|  | 
 | |||
|  |                 return new QQ_ExpressViewHolder(mInflater.inflate(R.layout.listitem_ad_qq_item_express, parent, false)); | |||
|  | 
 | |||
|  |             default: | |||
|  |                 return new MyViewHolder( mInflater.inflate(listItemID, parent, false)); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     public void setBookList(List<Novel> bookLists){ | |||
|  |         this.mDatas = bookLists; | |||
|  |         notifyDataSetChanged(); | |||
|  |     } | |||
|  |     public void setParameters(List<Novel> mDatas, int listItemID) { | |||
|  |         this.mDatas = mDatas; | |||
|  |         this.listItemID = listItemID; | |||
|  |     } | |||
|  | 
 | |||
|  |     public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) { | |||
|  |         this.mOnItemClickListener = mOnItemClickLitener; | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void onBindViewHolder(RecyclerView.ViewHolder hd, int position) { | |||
|  | 
 | |||
|  |         TTFeedAd ttFeedAd; | |||
|  |         if   (hd instanceof FooterViewHolder) { | |||
|  | 
 | |||
|  | 
 | |||
|  |             FooterViewHolder footerViewHolder = (FooterViewHolder) hd; | |||
|  | 
 | |||
|  | 
 | |||
|  |             switch (mLoadMoreStatus) { | |||
|  |                 case PULLUP_LOAD_MORE: | |||
|  |                     footerViewHolder.mTvLoadText.setText(String.format("上拉加载更多..")); | |||
|  |                     break; | |||
|  |                 case LOADING_MORE: | |||
|  |                     footerViewHolder.mTvLoadText.setText("正加载更多..."); | |||
|  |                     break; | |||
|  |                 case NO_LOAD_MORE: | |||
|  |                     if(!showFootView){ | |||
|  |                         footerViewHolder.mLoadLayout.setVisibility(View.GONE); | |||
|  |                         footerViewHolder.itemView.setVisibility(View.GONE); | |||
|  |                        return; | |||
|  |                     } | |||
|  |                     //隐藏加载更多 | |||
|  |                     footerViewHolder.mTvLoadText.setText("到底了"); | |||
|  | 
 | |||
|  |                     break; | |||
|  | 
 | |||
|  |             } | |||
|  |         }else if   (hd instanceof EmptyViewHolder) { | |||
|  |             EmptyViewHolder holder = (EmptyViewHolder) hd; | |||
|  |             holder.tvEmpty.setVisibility(View.GONE); | |||
|  |             if (mDatas == null || mDatas != null && mDatas.size() == 0) { | |||
|  |                 holder.tvEmpty.setVisibility(View.VISIBLE); | |||
|  |                 holder.tvEmpty.setText(norecord); | |||
|  |             } | |||
|  |         }else if   (hd instanceof QQ_ExpressViewHolder) { | |||
|  |             QQ_ExpressViewHolder   holder = (QQ_ExpressViewHolder)hd; | |||
|  |          //   holder.title.setVisibility(View.VISIBLE); | |||
|  |             final NativeExpressADView adView = (NativeExpressADView) mDatas.get(position); | |||
|  |            // mAdViewPositionMap.put(adView, position); // 广告在列表中的位置是可以被更新的 | |||
|  |             if (holder.container.getChildCount() > 0 | |||
|  |                     && holder.container.getChildAt(0) == adView) { | |||
|  |                 return; | |||
|  |             } | |||
|  | 
 | |||
|  |             if (holder.container.getChildCount() > 0) { | |||
|  |                 holder.container.removeAllViews(); | |||
|  |             } | |||
|  | 
 | |||
|  |             if (adView.getParent() != null) { | |||
|  |                 ((ViewGroup) adView.getParent()).removeView(adView); | |||
|  |             } | |||
|  | 
 | |||
|  |             holder.container.addView(adView); | |||
|  |             adView.render(); // 调用render方法后sdk才会开始展示广告 | |||
|  |         } | |||
|  | 
 | |||
|  |         else if   (hd instanceof MyViewHolder && mDatas.get(position) instanceof  Novel) { | |||
|  | 
 | |||
|  |             Novel novel =(Novel) mDatas.get(position); | |||
|  |             MyViewHolder   holder = (MyViewHolder)hd; | |||
|  |             holder.tvTitle.setText(novel.getName()); | |||
|  |             if (holder.tvAuthor != null) holder.tvAuthor.setText(novel.getAuthor()); | |||
|  |             if (holder.tvCate != null) holder.tvCate.setText(novel.getNovelType()); | |||
|  |             if (holder.tvDesc != null) holder.tvDesc.setText(novel.getDesc()); | |||
|  | 
 | |||
|  |             if (holder.tvLastRead != null) holder.tvLastRead.setText(CommonUtil.getDateString( novel.getLastVisit() )); | |||
|  |           //  if (holder.tvProgress != null) holder.tvProgress.setText( String.format("上次看到第 %s 章,共%s章",mDatas.get(position).getLastReadChapt(),mDatas.get(position).getChaptCnt())       ); | |||
|  |             if (holder.tvProgress != null) holder.tvProgress.setText( String.format("上次看到第 %s 章",novel.getLastReadChapt(),novel.getChaptCnt())       ); | |||
|  |             if (holder.tvReadtime != null) holder.tvReadtime.setText( "总计阅读了 "+CommonUtil.getTimeCnt4Read( novel.getReadtime() ,true) ); | |||
|  | 
 | |||
|  | 
 | |||
|  |             if (holder.tvStatus != null) holder.tvStatus.setText(novel.getProgress()); | |||
|  |             if (holder.imageView != null/* && !TextUtils.isEmpty(mDatas.get(position).getCover())*/) { | |||
|  |               //  ImageUtil.loadImage(mContext, mDatas.get(position).getCover(), holder.imageView); | |||
|  |                 loader.displayImage(mContext, NetUtil.getCoverUrl(novel.getCover()) ,holder.imageView); | |||
|  |             } | |||
|  |             if (holder.imageTop != null) { | |||
|  |                 if( novel.isTop()){ | |||
|  |                     holder.imageTop.setVisibility(View.VISIBLE); | |||
|  |                 }else{ | |||
|  |                     holder.imageTop.setVisibility(View.GONE); | |||
|  |                 } | |||
|  | 
 | |||
|  |             } | |||
|  |             if(isShowTop) { | |||
|  |                 if (holder.imageTop1 != null) { | |||
|  |                     if (position == 0) { | |||
|  |                         holder.imageTop1.setVisibility(View.VISIBLE); | |||
|  |                     } else { | |||
|  |                         holder.imageTop1.setVisibility(View.GONE); | |||
|  |                     } | |||
|  |                 } | |||
|  |                 if (holder.imageTop2 != null) { | |||
|  |                     if (position == 1) { | |||
|  |                         holder.imageTop2.setVisibility(View.VISIBLE); | |||
|  |                     } else { | |||
|  |                         holder.imageTop2.setVisibility(View.GONE); | |||
|  |                     } | |||
|  |                 } | |||
|  |                 if (holder.imageTop3 != null) { | |||
|  |                     if (position == 2) { | |||
|  |                         holder.imageTop3.setVisibility(View.VISIBLE); | |||
|  |                     } else { | |||
|  |                         holder.imageTop3.setVisibility(View.GONE); | |||
|  |                     } | |||
|  |                 } | |||
|  |             } | |||
|  | 
 | |||
|  |             if (holder.imageUpdate != null) { | |||
|  |                 if( novel.isUpdated()){ | |||
|  |                     holder.imageUpdate.setVisibility(View.VISIBLE); | |||
|  |                 }else{ | |||
|  |                     holder.imageUpdate.setVisibility(View.GONE); | |||
|  |                 } | |||
|  | 
 | |||
|  |             } | |||
|  |             if (holder.tvNum != null) holder.tvNum.setText("999"); | |||
|  |             // 如果设置了回调,则设置点击事件 | |||
|  |             if (mOnItemClickListener != null) { | |||
|  |                 holder.itemView.setOnClickListener(new View.OnClickListener() { | |||
|  |                     @Override | |||
|  |                     public void onClick(View v) { | |||
|  |                         int pos = holder.getLayoutPosition(); | |||
|  |                         mOnItemClickListener.onItemClick(holder.itemView, pos); | |||
|  |                     } | |||
|  |                 }); | |||
|  | 
 | |||
|  |                 holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { | |||
|  |                     @Override | |||
|  |                     public boolean onLongClick(View v) { | |||
|  |                         int pos = holder.getLayoutPosition(); | |||
|  |                         mOnItemClickListener.onItemLongClick(holder.itemView, pos); | |||
|  |                         return false; | |||
|  |                     } | |||
|  |                 }); | |||
|  |             } | |||
|  |         }else if (hd instanceof SmallAdViewHolder) { | |||
|  |             ttFeedAd =(TTFeedAd) mDatas.get(position); | |||
|  |             SmallAdViewHolder smallAdViewHolder = (SmallAdViewHolder) hd; | |||
|  |             bindData(smallAdViewHolder, ttFeedAd); | |||
|  |             if (ttFeedAd.getImageList() != null && !ttFeedAd.getImageList().isEmpty()) { | |||
|  |                 TTImage image = ttFeedAd.getImageList().get(0); | |||
|  |                 if (image != null && image.isValid()) { | |||
|  |                     mAQuery.id(smallAdViewHolder.mSmallImage).image(image.getImageUrl()); | |||
|  |                 } | |||
|  |             } | |||
|  | 
 | |||
|  |         } else if (hd instanceof LargeAdViewHolder) { | |||
|  |             ttFeedAd =(TTFeedAd) mDatas.get(position); | |||
|  |             LargeAdViewHolder largeAdViewHolder = (LargeAdViewHolder) hd; | |||
|  |             bindData(largeAdViewHolder, ttFeedAd); | |||
|  |             if (ttFeedAd.getImageList() != null && !ttFeedAd.getImageList().isEmpty()) { | |||
|  |                 TTImage image = ttFeedAd.getImageList().get(0); | |||
|  |                 if (image != null && image.isValid()) { | |||
|  |                     mAQuery.id(largeAdViewHolder.mLargeImage).image(image.getImageUrl()); | |||
|  |                 } | |||
|  |             } | |||
|  | 
 | |||
|  |         } else if (hd instanceof GroupAdViewHolder) { | |||
|  |             ttFeedAd =(TTFeedAd) mDatas.get(position); | |||
|  |             GroupAdViewHolder groupAdViewHolder = (GroupAdViewHolder) hd; | |||
|  |             bindData(groupAdViewHolder, ttFeedAd); | |||
|  |             if (ttFeedAd.getImageList() != null && ttFeedAd.getImageList().size() >= 3) { | |||
|  |                 TTImage image1 = ttFeedAd.getImageList().get(0); | |||
|  |                 TTImage image2 = ttFeedAd.getImageList().get(1); | |||
|  |                 TTImage image3 = ttFeedAd.getImageList().get(2); | |||
|  |                 if (image1 != null && image1.isValid()) { | |||
|  |                     mAQuery.id(groupAdViewHolder.mGroupImage1).image(image1.getImageUrl()); | |||
|  |                 } | |||
|  |                 if (image2 != null && image2.isValid()) { | |||
|  |                     mAQuery.id(groupAdViewHolder.mGroupImage2).image(image2.getImageUrl()); | |||
|  |                 } | |||
|  |                 if (image3 != null && image3.isValid()) { | |||
|  |                     mAQuery.id(groupAdViewHolder.mGroupImage3).image(image3.getImageUrl()); | |||
|  |                 } | |||
|  |             } | |||
|  | 
 | |||
|  |         } else if (hd instanceof VideoAdViewHolder) { | |||
|  |             ttFeedAd = (TTFeedAd)mDatas.get(position); | |||
|  |             VideoAdViewHolder videoAdViewHolder = (VideoAdViewHolder) hd; | |||
|  |             bindData(videoAdViewHolder, ttFeedAd); | |||
|  |             ttFeedAd.setVideoAdListener(new TTFeedAd.VideoAdListener() { | |||
|  |                 @Override | |||
|  |                 public void onVideoLoad(TTFeedAd ad) { | |||
|  | 
 | |||
|  |                 } | |||
|  | 
 | |||
|  |                 @Override | |||
|  |                 public void onVideoError(int errorCode, int extraCode) { | |||
|  | 
 | |||
|  |                 } | |||
|  | 
 | |||
|  |                 @Override | |||
|  |                 public void onVideoAdStartPlay(TTFeedAd ad) { | |||
|  | 
 | |||
|  |                 } | |||
|  | 
 | |||
|  |                 @Override | |||
|  |                 public void onVideoAdPaused(TTFeedAd ad) { | |||
|  | 
 | |||
|  |                 } | |||
|  | 
 | |||
|  |                 @Override | |||
|  |                 public void onVideoAdContinuePlay(TTFeedAd ad) { | |||
|  | 
 | |||
|  |                 } | |||
|  |             }); | |||
|  |             if (videoAdViewHolder.videoView != null) { | |||
|  |                 View video = ttFeedAd.getAdView(); | |||
|  |                 if (video != null) { | |||
|  |                     if (video.getParent() == null) { | |||
|  |                         videoAdViewHolder.videoView.removeAllViews(); | |||
|  |                         videoAdViewHolder.videoView.addView(video); | |||
|  |                     } | |||
|  |                 } | |||
|  |             } | |||
|  | 
 | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public int getItemCount() { | |||
|  |         return mDatas.size()+1; | |||
|  |     } | |||
|  | 
 | |||
|  |     public void addData(int position) { | |||
|  |      //   mDatas.add(position, "Insert One"); | |||
|  |         notifyItemInserted(position); | |||
|  |     } | |||
|  | 
 | |||
|  |     public void removeData(int position) { | |||
|  |         mDatas.remove(position); | |||
|  |         notifyItemRemoved(position); | |||
|  |     } | |||
|  | 
 | |||
|  |     //to set booklist updated, used in shelf | |||
|  |     public void setUpdate(List<Novel> lstUpdate) { | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     public class FooterViewHolder extends RecyclerView.ViewHolder { | |||
|  |         @BindView(R.id.pbLoad) | |||
|  |         ProgressBar mPbLoad; | |||
|  |         @BindView(R.id.tvLoadText) | |||
|  |         TextView mTvLoadText; | |||
|  |         @BindView(R.id.loadLayout) | |||
|  |         LinearLayout mLoadLayout; | |||
|  |         public FooterViewHolder(View itemView) { | |||
|  |             super(itemView); | |||
|  |             ButterKnife.bind(this,itemView); | |||
|  |         } | |||
|  |     } | |||
|  |       class EmptyViewHolder extends RecyclerView.ViewHolder { | |||
|  |         @BindView(R.id.tvLoadText) | |||
|  |         TextView tvEmpty; | |||
|  |         public EmptyViewHolder(View itemView) { | |||
|  |             super(itemView); | |||
|  |             ButterKnife.bind(this,itemView); | |||
|  |         } | |||
|  |     } | |||
|  |     class NoShowViewHolder extends RecyclerView.ViewHolder { | |||
|  | 
 | |||
|  |         public NoShowViewHolder(View itemView) { | |||
|  |             super(itemView); | |||
|  | 
 | |||
|  |         } | |||
|  |     } | |||
|  |     public void AddHeaderItem(List items) { | |||
|  |         if(mDatas.size()>3 &&( mDatas.get(3) instanceof TTFeedAd) ){ | |||
|  |             return; | |||
|  |         } | |||
|  |         int p = mDatas.size()>3?3:0; | |||
|  |         mDatas.addAll(p, items); | |||
|  |         notifyDataSetChanged(); | |||
|  |     } | |||
|  |     public void setData(List<Novel> items) { | |||
|  |         // mDatas.add(position, "Insert One"); | |||
|  |         mDatas = items; | |||
|  |         notifyDataSetChanged(); | |||
|  |     } | |||
|  |     public void AddFooterItem(List items) { | |||
|  |         mDatas.addAll(items); | |||
|  |         notifyDataSetChanged(); | |||
|  |     } | |||
|  |     /** | |||
|  |      * 更新加载更多状态 | |||
|  |      * @param status | |||
|  |      */ | |||
|  |     public void changeMoreStatus(int status){ | |||
|  |         mLoadMoreStatus=status; | |||
|  |         notifyDataSetChanged(); | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { | |||
|  |         super.onAttachedToRecyclerView(recyclerView); | |||
|  | 
 | |||
|  |         RecyclerView.LayoutManager layout = recyclerView.getLayoutManager(); | |||
|  |         if (layout != null && layout instanceof GridLayoutManager) { | |||
|  |             final GridLayoutManager manager = (GridLayoutManager) layout; | |||
|  |             manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { | |||
|  |                 @Override | |||
|  |                 public int getSpanSize(int position) { | |||
|  |                     int type = getItemViewType(position); | |||
|  |                     if (/*type == ITEM_VIEW_TYPE_LOAD_MORE ||*/ type == ITEM_VIEW_TYPE_VIDEO) { | |||
|  |                         return manager.getSpanCount(); | |||
|  |                     } | |||
|  |                     return 1; | |||
|  |                 } | |||
|  |             }); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) { | |||
|  |         //noinspection unchecked | |||
|  |         super.onViewAttachedToWindow(holder); | |||
|  |         ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); | |||
|  |         if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) { | |||
|  |             int position = holder.getLayoutPosition(); | |||
|  |             int type = getItemViewType(position); | |||
|  |             if (/*type == ITEM_VIEW_TYPE_LOAD_MORE || */type == ITEM_VIEW_TYPE_VIDEO) { | |||
|  |                 StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp; | |||
|  |                 p.setFullSpan(true); | |||
|  |             } | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     @SuppressWarnings("WeakerAccess") | |||
|  |     private static class VideoAdViewHolder extends AdViewHolder { | |||
|  |         @SuppressWarnings("CanBeFinal") | |||
|  |         FrameLayout videoView; | |||
|  | 
 | |||
|  |         @SuppressWarnings("RedundantCast") | |||
|  |         public VideoAdViewHolder(View itemView) { | |||
|  |             super(itemView); | |||
|  | 
 | |||
|  |             mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); | |||
|  |             mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); | |||
|  |             mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); | |||
|  |             videoView = (FrameLayout) itemView.findViewById(R.id.iv_listitem_video); | |||
|  |             mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); | |||
|  |             mCreativeButton = (Button) itemView.findViewById(R.id.btn_listitem_creative); | |||
|  |           //  mStopButton = (Button) itemView.findViewById(R.id.btn_listitem_stop); | |||
|  |           //  mRemoveButton = (Button) itemView.findViewById(R.id.btn_listitem_remove); | |||
|  | 
 | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     private static class LargeAdViewHolder extends AdViewHolder { | |||
|  |         ImageView mLargeImage; | |||
|  | 
 | |||
|  |         @SuppressWarnings("RedundantCast") | |||
|  |         public LargeAdViewHolder(View itemView) { | |||
|  |             super(itemView); | |||
|  | 
 | |||
|  |             mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); | |||
|  |             mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); | |||
|  |             mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); | |||
|  |             mLargeImage = (ImageView) itemView.findViewById(R.id.iv_listitem_image); | |||
|  |             mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); | |||
|  |             mCreativeButton = (Button) itemView.findViewById(R.id.btn_listitem_creative); | |||
|  |           //  mStopButton = (Button) itemView.findViewById(R.id.btn_listitem_stop); | |||
|  |            // mRemoveButton = (Button) itemView.findViewById(R.id.btn_listitem_remove); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     private static class SmallAdViewHolder extends AdViewHolder { | |||
|  |         ImageView mSmallImage; | |||
|  | 
 | |||
|  |         @SuppressWarnings("RedundantCast") | |||
|  |         public SmallAdViewHolder(View itemView) { | |||
|  |             super(itemView); | |||
|  | 
 | |||
|  |             mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); | |||
|  |             mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); | |||
|  |             mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); | |||
|  |             mSmallImage = (ImageView) itemView.findViewById(R.id.iv_listitem_image); | |||
|  |             mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); | |||
|  |             mCreativeButton = (Button) itemView.findViewById(R.id.btn_listitem_creative); | |||
|  |           //  mStopButton = (Button) itemView.findViewById(R.id.btn_listitem_stop); | |||
|  |          //   mRemoveButton = (Button) itemView.findViewById(R.id.btn_listitem_remove); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     @SuppressWarnings("CanBeFinal") | |||
|  |     private static class GroupAdViewHolder extends AdViewHolder { | |||
|  |         ImageView mGroupImage1; | |||
|  |         ImageView mGroupImage2; | |||
|  |         ImageView mGroupImage3; | |||
|  | 
 | |||
|  |         @SuppressWarnings("RedundantCast") | |||
|  |         public GroupAdViewHolder(View itemView) { | |||
|  |             super(itemView); | |||
|  | 
 | |||
|  |             mTitle = (TextView) itemView.findViewById(R.id.tv_listitem_ad_title); | |||
|  |             mSource = (TextView) itemView.findViewById(R.id.tv_listitem_ad_source); | |||
|  |             mDescription = (TextView) itemView.findViewById(R.id.tv_listitem_ad_desc); | |||
|  |             mGroupImage1 = (ImageView) itemView.findViewById(R.id.iv_listitem_image1); | |||
|  |             mGroupImage2 = (ImageView) itemView.findViewById(R.id.iv_listitem_image2); | |||
|  |             mGroupImage3 = (ImageView) itemView.findViewById(R.id.iv_listitem_image3); | |||
|  |             mIcon = (ImageView) itemView.findViewById(R.id.iv_listitem_icon); | |||
|  |             mCreativeButton = (Button) itemView.findViewById(R.id.btn_listitem_creative); | |||
|  |          //   mStopButton = (Button) itemView.findViewById(R.id.btn_listitem_stop); | |||
|  |         //    mRemoveButton = (Button) itemView.findViewById(R.id.btn_listitem_remove); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     private static class AdViewHolder extends RecyclerView.ViewHolder { | |||
|  |         ImageView mIcon; | |||
|  |         Button mCreativeButton; | |||
|  |         TextView mTitle; | |||
|  |         TextView mDescription; | |||
|  |         TextView mSource; | |||
|  |        // Button mStopButton; | |||
|  |       //  Button mRemoveButton; | |||
|  | 
 | |||
|  |         public AdViewHolder(View itemView) { | |||
|  |             super(itemView); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     class QQ_ExpressViewHolder extends RecyclerView.ViewHolder { | |||
|  |      //   public TextView title; | |||
|  |         public ViewGroup container; | |||
|  | 
 | |||
|  |         public QQ_ExpressViewHolder(View view) { | |||
|  |             super(view); | |||
|  |          //   title = (TextView) view.findViewById(R.id.title); | |||
|  |             container = (ViewGroup) view.findViewById(R.id.express_ad_container); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  |     private void bindData(final AdViewHolder adViewHolder, TTFeedAd ad) { | |||
|  |         //可以被点击的view, 也可以把convertView放进来意味item可被点击 | |||
|  |         List<View> clickViewList = new ArrayList<>(); | |||
|  |         clickViewList.add(adViewHolder.itemView); | |||
|  |         //触发创意广告的view(点击下载或拨打电话) | |||
|  |         List<View> creativeViewList = new ArrayList<>(); | |||
|  |         creativeViewList.add(adViewHolder.mCreativeButton); | |||
|  |         //如果需要点击图文区域也能进行下载或者拨打电话动作,请将图文区域的view传入 | |||
|  | //            creativeViewList.add(convertView); | |||
|  |         //重要! 这个涉及到广告计费,必须正确调用。convertView必须使用ViewGroup。 | |||
|  |         ad.registerViewForInteraction((ViewGroup) adViewHolder.itemView, clickViewList, creativeViewList, new TTNativeAd.AdInteractionListener() { | |||
|  |             @Override | |||
|  |             public void onAdClicked(View view, TTNativeAd ad) { | |||
|  |                 if (ad != null) { | |||
|  |                   //  TToast.show(mContext, "广告" + ad.getTitle() + "被点击"); | |||
|  |                 } | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onAdCreativeClick(View view, TTNativeAd ad) { | |||
|  |                 if (ad != null) { | |||
|  |                   //  TToast.show(mContext, "广告" + ad.getTitle() + "被创意按钮被点击"); | |||
|  |                 } | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onAdShow(TTNativeAd ad) { | |||
|  |                 if (ad != null) { | |||
|  |                    // TToast.show(mContext, "广告" + ad.getTitle() + "展示"); | |||
|  |                 } | |||
|  |             } | |||
|  |         }); | |||
|  |         adViewHolder.mTitle.setText(ad.getTitle()); | |||
|  |         adViewHolder.mDescription.setText(ad.getDescription()); | |||
|  |         adViewHolder.mSource.setText(ad.getSource() == null ? "广告来源" : ad.getSource()); | |||
|  |         TTImage icon = ad.getIcon(); | |||
|  |         if (icon != null && icon.isValid()) { | |||
|  |             ImageOptions options = new ImageOptions(); | |||
|  |             mAQuery.id(adViewHolder.mIcon).image(icon.getImageUrl(), options); | |||
|  |         } | |||
|  |         Button adCreativeButton = adViewHolder.mCreativeButton; | |||
|  |         switch (ad.getInteractionType()) { | |||
|  |             case TTAdConstant.INTERACTION_TYPE_DOWNLOAD: | |||
|  |                 //如果初始化ttAdManager.createAdNative(getApplicationContext())没有传入activity 则需要在此传activity,否则影响使用Dislike逻辑 | |||
|  |                 if (mContext instanceof Activity) { | |||
|  |                     ad.setActivityForDownloadApp((Activity) mContext); | |||
|  |                 } | |||
|  |                 adCreativeButton.setVisibility(View.VISIBLE); | |||
|  |               //  adViewHolder.mStopButton.setVisibility(View.VISIBLE); | |||
|  |                // adViewHolder.mRemoveButton.setVisibility(View.VISIBLE); | |||
|  |                 bindDownloadListener(adCreativeButton, adViewHolder, ad); | |||
|  |                 //绑定下载状态控制器 | |||
|  |                 bindDownLoadStatusController(adViewHolder, ad); | |||
|  |                 break; | |||
|  |             case TTAdConstant.INTERACTION_TYPE_DIAL: | |||
|  |                 adCreativeButton.setVisibility(View.VISIBLE); | |||
|  |                 adCreativeButton.setText("立即拨打"); | |||
|  |               //  adViewHolder.mStopButton.setVisibility(View.GONE); | |||
|  |              //   adViewHolder.mRemoveButton.setVisibility(View.GONE); | |||
|  |                 break; | |||
|  |             case TTAdConstant.INTERACTION_TYPE_LANDING_PAGE: | |||
|  |             case TTAdConstant.INTERACTION_TYPE_BROWSER: | |||
|  | //                    adCreativeButton.setVisibility(View.GONE); | |||
|  |                 adCreativeButton.setVisibility(View.VISIBLE); | |||
|  |                 adCreativeButton.setText("查看详情"); | |||
|  |               //  adViewHolder.mStopButton.setVisibility(View.GONE); | |||
|  |               //  adViewHolder.mRemoveButton.setVisibility(View.GONE); | |||
|  |                 break; | |||
|  |             default: | |||
|  |                 adCreativeButton.setVisibility(View.GONE); | |||
|  |              //   adViewHolder.mStopButton.setVisibility(View.GONE); | |||
|  |              //   adViewHolder.mRemoveButton.setVisibility(View.GONE); | |||
|  |               //  TToast.show(mContext, "交互类型异常"); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     private void bindDownLoadStatusController(AdViewHolder adViewHolder, final TTFeedAd ad) { | |||
|  |         final DownloadStatusController controller = ad.getDownloadStatusController(); | |||
|  |       /*  adViewHolder.mStopButton.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View v) { | |||
|  |                 if (controller != null) { | |||
|  |                     controller.changeDownloadStatus(); | |||
|  |                    // TToast.show(mContext, "改变下载状态"); | |||
|  |                     Log.d(TAG, "改变下载状态"); | |||
|  |                 } | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  |         adViewHolder.mRemoveButton.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View v) { | |||
|  |                 if (controller != null) { | |||
|  |                     controller.cancelDownload(); | |||
|  |                    // TToast.show(mContext, "取消下载"); | |||
|  |                     Log.d(TAG, "取消下载"); | |||
|  |                 } | |||
|  |             } | |||
|  |         });*/ | |||
|  |     } | |||
|  | 
 | |||
|  |     private void bindDownloadListener(final Button adCreativeButton, final AdViewHolder adViewHolder, TTFeedAd ad) { | |||
|  |         TTAppDownloadListener downloadListener = new TTAppDownloadListener() { | |||
|  |             @Override | |||
|  |             public void onIdle() { | |||
|  |                 if (!isValid()) { | |||
|  |                     return; | |||
|  |                 } | |||
|  |                 adCreativeButton.setText("开始下载"); | |||
|  |               // adViewHolder.mStopButton.setText("开始下载"); | |||
|  |             } | |||
|  | 
 | |||
|  |             @SuppressLint("SetTextI18n") | |||
|  |             @Override | |||
|  |             public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) { | |||
|  |                 if (!isValid()) { | |||
|  |                     return; | |||
|  |                 } | |||
|  |                 if (totalBytes <= 0L) { | |||
|  |                     adCreativeButton.setText("下载中 percent: 0"); | |||
|  |                 } else { | |||
|  |                     adCreativeButton.setText("下载中 percent: " + (currBytes * 100 / totalBytes)); | |||
|  |                 } | |||
|  |               //  adViewHolder.mStopButton.setText("下载中"); | |||
|  |             } | |||
|  | 
 | |||
|  |             @SuppressLint("SetTextI18n") | |||
|  |             @Override | |||
|  |             public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) { | |||
|  |                 if (!isValid()) { | |||
|  |                     return; | |||
|  |                 } | |||
|  |                 if (totalBytes <= 0L) { | |||
|  |                     adCreativeButton.setText("下载中 percent: 0"); | |||
|  |                 } else { | |||
|  |                     adCreativeButton.setText("下载暂停 percent: " + (currBytes * 100 / totalBytes)); | |||
|  |                 } | |||
|  |                // adViewHolder.mStopButton.setText("下载暂停"); | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) { | |||
|  |                 if (!isValid()) { | |||
|  |                     return; | |||
|  |                 } | |||
|  |                 adCreativeButton.setText("重新下载"); | |||
|  |               //  adViewHolder.mStopButton.setText("重新下载"); | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onInstalled(String fileName, String appName) { | |||
|  |                 if (!isValid()) { | |||
|  |                     return; | |||
|  |                 } | |||
|  |                 adCreativeButton.setText("点击打开"); | |||
|  |              //   adViewHolder.mStopButton.setText("点击打开"); | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onDownloadFinished(long totalBytes, String fileName, String appName) { | |||
|  |                 if (!isValid()) { | |||
|  |                     return; | |||
|  |                 } | |||
|  |                 adCreativeButton.setText("点击安装"); | |||
|  |              //   adViewHolder.mStopButton.setText("点击安装"); | |||
|  |             } | |||
|  | 
 | |||
|  |             @SuppressWarnings("BooleanMethodIsAlwaysInverted") | |||
|  |             private boolean isValid() { | |||
|  |                 return mTTAppDownloadListenerMap.get(adViewHolder) == this; | |||
|  |             } | |||
|  |         }; | |||
|  |         //一个ViewHolder对应一个downloadListener, isValid判断当前ViewHolder绑定的listener是不是自己 | |||
|  |         ad.setDownloadListener(downloadListener); // 注册下载监听器 | |||
|  |         mTTAppDownloadListenerMap.put(adViewHolder, downloadListener); | |||
|  |     } | |||
|  | 
 | |||
|  | } |