1. 程式人生 > >RecyclerView 多條目 時間戳 條目點選 僅有介面卡

RecyclerView 多條目 時間戳 條目點選 僅有介面卡

package com.first.firstga.zkutil.zkadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
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 android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.first.firstga.R;
import com.first.firstga.tools.GlideCircleTransform;
import com.first.firstga.zkbean.HomePageTopicBean;
import com.first.firstga.zkbean.SearchShowtopicBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Xs on 2018/8/24.
 * 類描述:
 */

public class HomePageTopicAdapter extends   RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context context;
        private List<HomePageTopicBean.DataBean.ListBean> list;
        private int Viewk_0=0;
        private int Viewk_1=1;
        private int Viewk_2=2;
        private int Viewk_3=3;
        private int Viewk_img2=4;
        private int Viewk_img3=5;
        private View view;
        private List<String> lo;
    private ViewGroup.LayoutParams pp;


    public HomePageTopicAdapter(Context context, List<HomePageTopicBean.DataBean.ListBean> list){
        this.context = context;
        this.list=list;
        }

@Override
public int getItemViewType(int position) {
    if(list.get(position).getImageThumbSmall()!=null){
        if("image".equals(list.get(position).getTopicType())&&list.get(position).getImageThumbSmall().size()==1){
            return Viewk_0;
        }else if("video".equals(list.get(position).getTopicType())){
            return Viewk_1;
        }else if("share".equals(list.get(position).getTopicType())){
            return Viewk_2;
        }else if("image".equals(list.get(position).getTopicType())&&list.get(position).getImageThumbSmall().size()>=3){
            return Viewk_img3;
        }else if("image".equals(list.get(position).getTopicType())&&list.get(position).getImageThumbSmall().size()==2){
            return Viewk_img2;
        }else{
            return Viewk_3;
        }

    }else{

        if("image".equals(list.get(position).getTopicType())){
            return Viewk_0;
        }else if("video".equals(list.get(position).getTopicType())){
            return Viewk_1;
        }else if("share".equals(list.get(position).getTopicType())){
            return Viewk_2;
        }else {
            return Viewk_3;
        }
    }
        }

/** * * @param parent * @param viewType * @return */
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if (viewType == Viewk_0) {
        view = LayoutInflater.from(context).inflate(R.layout.item_topic_img, parent, false);
        ViewHolderA viewHolderA = new ViewHolderA(view);
        return viewHolderA;
    } else if (viewType == Viewk_1) {
        view = LayoutInflater.from(context).inflate(R.layout.item_topic_video, parent, false);
        ViewHolderB viewHolderB = new ViewHolderB(view);
        return viewHolderB;
    } else if (viewType == Viewk_2) {
        view = LayoutInflater.from(context).inflate(R.layout.item_topic_share, parent, false);
        ViewHolderC viewHolderC = new ViewHolderC(view);
        return viewHolderC;
    } else if(viewType==Viewk_3) {
        view = LayoutInflater.from(context).inflate(R.layout.item_topic_text, parent, false);
        ViewHolderD viewHolderD = new ViewHolderD(view);
        return viewHolderD;
    } else if(viewType==Viewk_img2){
        view = LayoutInflater.from(context).inflate(R.layout.item_topic_img2, parent, false);
        ViewHolderimg2 viewHolderimg2 = new ViewHolderimg2(view);
        return viewHolderimg2;
    }else{
        view = LayoutInflater.from(context).inflate(R.layout.item_topic_img3, parent, false);
        ViewHolderimg3 viewHolderimg3 = new ViewHolderimg3(view);
        return viewHolderimg3;
    }
        }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof ViewHolderA){
            Toast.makeText(context, "這裡的辣椒有點甜", Toast.LENGTH_SHORT).show();
            ViewHolderA tViewHolder = (ViewHolderA) holder;
            if(list.get(position).getIsSelected().equals("1")){
                tViewHolder.pick.setVisibility(View.VISIBLE);
            }else{
                tViewHolder.pick.setVisibility(View.GONE);
            }
            tViewHolder.zk_headname.setText(list.get(position).getUser().getNickname());//設定title
            String j=stampToTime(list.get(position).getPublishTime());
            tViewHolder.zk_headtime.setText(j);//xx閱讀
            tViewHolder.imgtitle.setText(list.get(position).getTitle());
          //  Glide.with(context).load(list.get(position).getUser().getHeadThumb()).error(R.mipmap.head).into(tViewHolder.zk_headimg);
            Glide.with(context).load(list.get(position).getUser().getHeadThumb()).centerCrop().placeholder(R.mipmap.mine_photo)
                    .transform(new GlideCircleTransform(context,2,context.getResources().getColor(R.color.white)))
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(tViewHolder.zk_headimg);

            if(list.get(position).getImageThumbSmall()!=null){
                Glide.with(context).load(list.get(position).getImageThumbSmall().get(0)).error(R.mipmap.placeholder).into(tViewHolder.img);
            }else{
                Glide.with(context).load(R.mipmap.placeholder).error(R.mipmap.placeholder).into(tViewHolder.img);

            }
        }else if(holder instanceof ViewHolderB){
            //****視訊型別
            ViewHolderB viewHolderB=(ViewHolderB) holder;
            if(list.get(position).getIsSelected().equals("1")){
                viewHolderB.pick.setVisibility(View.VISIBLE);
            }else{
                viewHolderB.pick.setVisibility(View.GONE);
            }
            viewHolderB.zk_headname.setText(list.get(position).getUser().getNickname());//設定title
            String j=stampToTime(list.get(position).getPublishTime());
            viewHolderB.zk_headtime.setText(j);//xx閱讀
            Glide.with(context).load(list.get(position).getUser().getHeadThumb()).centerCrop().placeholder(R.mipmap.mine_photo)
                    .transform(new GlideCircleTransform(context,2,context.getResources().getColor(R.color.white)))
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(viewHolderB.zk_headimg);
            viewHolderB.footer_read.setText(list.get(position).getReadCount()+"");
            viewHolderB.footer_critic.setText(list.get(position).getCommentCount()+"");
            viewHolderB.footer_like.setText(list.get(position).getLikeCount()+"");
            viewHolderB.item_topic_video_title.setText(list.get(position).getTitle());
            if(list.get(position).getImageThumbSmall()!=null){
                Glide.with(context).load(list.get(position).getImageThumbSmall().get(0)).error(R.mipmap.placeholder).into(viewHolderB.item_topic_video_img);
            }else{
                Glide.with(context).load(R.mipmap.placeholder).error(R.mipmap.placeholder).into(viewHolderB.item_topic_video_img);
            }

        }else if(holder instanceof ViewHolderC){
            //一張大圖
            ViewHolderC tViewHolder = (ViewHolderC) holder;
            if(list.get(position).getIsSelected().equals("1")){
                tViewHolder.pick.setVisibility(View.VISIBLE);
            }else{
                tViewHolder.pick.setVisibility(View.GONE);
            }
            tViewHolder.zk_headname.setText(list.get(position).getUser().getNickname()+"");//設定title





              String j=stampToTime(list.get(position).getPublishTime());
            tViewHolder.zk_headtime.setText(j);//xx閱讀

            Glide.with(context).load(list.get(position).getUser().getHeadThumb()).centerCrop().placeholder(R.mipmap.mine_photo)
                    .transform(new GlideCircleTransform(context,2,context.getResources().getColor(R.color.white)))
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(tViewHolder.zk_headimg);



            tViewHolder.footer_read.setText(list.get(position).getReadCount()+"");
            tViewHolder.footer_critic.setText(list.get(position).getCommentCount()+"");
            tViewHolder.footer_like.setText(list.get(position).getLikeCount()+"");

            //連結的左邊圖

            if(null==list.get(position).getImageThumbSmall()){
                //如果連結沒有圖片
                Toast.makeText(context, "連結介面"+"///"+list.get(position).getTitle()+"///上", Toast.LENGTH_SHORT).show();
                Glide.with(context).load(R.mipmap.head).into(tViewHolder.img);
            }else {
                Toast.makeText(context, "連結介面"+"///"+list.get(position).getTitle()+"///下", Toast.LENGTH_SHORT).show();

                Glide.with(context).load(list.get(position).getImageThumbSmall().get(0)).error(R.mipmap.head).into(tViewHolder.img);
            }

            tViewHolder.title.setText(list.get(position).getTitle()+"");


        }else if(holder instanceof ViewHolderimg2){
           //****兩張圖片型別
         ViewHolderimg2 viewHolderimg2= (ViewHolderimg2) holder;
            if(list.get(position).getIsSelected().equals("1")){
                viewHolderimg2.pick.setVisibility(View.VISIBLE);
            }else{
                viewHolderimg2.pick.setVisibility(View.GONE);
            }
            viewHolderimg2.zk_headname.setText(list.get(position).getUser().getNickname());//設定title
            String j=stampToTime(list.get(position).getPublishTime());
            viewHolderimg2.zk_headtime.setText(j);//xx閱讀
            viewHolderimg2.imgtitle.setText(list.get(position).getTitle());
          //  Glide.with(context).load(list.get(position).getUser().getHeadThumb()).error(R.mipmap.head).into(viewHolderimg2.zk_headimg);
            Glide.with(context).load(list.get(position).getUser().getHeadThumb()).centerCrop().placeholder(R.mipmap.mine_photo)
                    .transform(new GlideCircleTransform(context,2,context.getResources().getColor(R.color.white)))
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(viewHolderimg2.zk_headimg);


            Glide.with(context).load(list.get(position).getImageThumbSmall().get(0)).error(R.mipmap.stanceimg).into(viewHolderimg2.img1);
            Glide.with(context).load(list.get(position).getImageThumbSmall().get(1)).error(R.mipmap.stanceimg).into(viewHolderimg2.img2);

            viewHolderimg2.footer_read.setText(list.get(position).getReadCount()+"");
            viewHolderimg2.footer_critic.setText(list.get(position).getCommentCount()+"");
            viewHolderimg2.footer_like.setText(list.get(position).getLikeCount()+"");

        }else if(holder instanceof ViewHolderD){
            //****text型別
            ViewHolderD viewHolderD= (ViewHolderD) holder;
            if(list.get(position).getIsSelected().equals("1")){
                viewHolderD.pick.setVisibility(View.VISIBLE);
            }else{
                viewHolderD.pick.setVisibility(View.GONE);
            }
            viewHolderD.zk_headname.setText(list.get(position).getUser().getNickname());//設定title
            String j=stampToTime(list.get(position).getPublishTime());
            viewHolderD.zk_headtime.setText(j);//xx閱讀
            viewHolderD.texttitle.setText(list.get(position).getTitle());
           // Glide.with(context).load(list.get(position).getUser().getHeadThumb()).error(R.mipmap.head).into(viewHolderD.zk_headimg);
            Glide.with(context).load(list.get(position).getUser().getHeadThumb()).centerCrop().placeholder(R.mipmap.mine_photo)
                    .transform(new GlideCircleTransform(context,2,context.getResources().getColor(R.color.white)))
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(viewHolderD.zk_headimg);




            viewHolderD.footer_read.setText(list.get(position).getReadCount()+"");
            viewHolderD.footer_critic.setText(list.get(position).getCommentCount()+"");
            viewHolderD.footer_like.setText(list.get(position).getLikeCount()+"");
        } else  {
            // image    三張圖片型別
            ViewHolderimg3 viewHolderimg3= (ViewHolderimg3) holder;
            if(list.get(position).getIsSelected().equals("1")){
                viewHolderimg3.pick.setVisibility(View.VISIBLE);
            }else{
                viewHolderimg3.pick.setVisibility(View.GONE);
            }
            viewHolderimg3.zk_headname.setText(list.get(position).getUser().getNickname());//設定title
            Toast.makeText(context, "三張圖片"+list.get(position).getUser().getNickname()+list.get(position).getPublishTime(), Toast.LENGTH_SHORT).show();


            String j=stampToTime(list.get(position).getPublishTime());
            viewHolderimg3.zk_headtime.setText(j);//xx閱讀


            viewHolderimg3.imgtitle.setText(list.get(position).getTitle());
         //   Glide.with(context).load(list.get(position).getUser().getHeadThumb()).error(R.mipmap.head).into(viewHolderimg3.zk_headimg);
            Glide.with(context).load(list.get(position).getUser().getHeadThumb()).centerCrop().placeholder(R.mipmap.mine_photo)
                    .transform(new GlideCircleTransform(context,2,context.getResources().getColor(R.color.white)))
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(viewHolderimg3.zk_headimg);




            Glide.with(context).load(list.get(position).getImageThumbSmall().get(0)).error(R.mipmap.stanceimg).into(viewHolderimg3.img1);
            Glide.with(context).load(list.get(position).getImageThumbSmall().get(1)).error(R.mipmap.stanceimg).into(viewHolderimg3.img2);
            Glide.with(context).load(list.get(position).getImageThumbSmall().get(2)).error(R.mipmap.stanceimg).into(viewHolderimg3.img3);

            viewHolderimg3.footer_read.setText(list.get(position).getReadCount()+"");
            viewHolderimg3.footer_critic.setText(list.get(position).getCommentCount()+"");
            viewHolderimg3.footer_like.setText(list.get(position).getLikeCount()+"");
        }
        if(null != onItemClickListener)

        { /*回撥點選事件*/ holder.itemView.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                onItemClickListener.setOnItemClick(v,position);

            }

        });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener(){

                @Override

                public boolean onLongClick(View v) {

                    onItemClickListener.setOnItemLongClick(v,position); return true; } }); }
    }


    /*   生命自己的一個事件回撥介面*/

//如果要將這個介面移到外面去  那麼久別用這個介面了  直接在

//onBindViewHolder中寫點選邏輯

public interface OnItemClickListener{

    void setOnItemClick(View view, int position);

    void setOnItemLongClick(View view, int position);

}

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){

        this.onItemClickListener = onItemClickListener;

    }



    @Override public int getItemCount() {

        return list.size(); }

class ViewHolderA extends RecyclerView.ViewHolder{

    ImageView zk_headimg;
    TextView zk_headname;
    TextView zk_headtime;
    LinearLayout imagelist;
    TextView imgtitle;
    ImageView img;
    ImageView pick;


    public ViewHolderA(View itemView) {
        super(itemView);
        zk_headimg=itemView.findViewById(R.id.zk_headimg);
        zk_headname=itemView.findViewById(R.id.zk_headname);
        zk_headtime=itemView.findViewById(R.id.zk_headtime);
        imagelist=itemView.findViewById(R.id.imagelist);
        imgtitle=itemView.findViewById(R.id.item_topic_img_tite);
        img=itemView.findViewById(R.id.item_topic_img_img);
        pick=itemView.findViewById(R.id.pick);

    } }

class ViewHolderB extends RecyclerView.ViewHolder{
    ImageView img;
    TextView title;
    ImageView zk_headimg;
    TextView zk_headname;
    TextView zk_headtime;
    TextView footer_read;
    TextView footer_critic;
    TextView footer_like;
    ImageView item_topic_video_img;
    TextView item_topic_video_title;
    ImageView pick;
    public ViewHolderB(View itemView) {
        super(itemView);


        zk_headimg=itemView.findViewById(R.id.zk_headimg);
        zk_headname=itemView.findViewById(R.id.zk_headname);
        zk_headtime=itemView.findViewById(R.id.zk_headtime);
        footer_read=itemView.findViewById(R.id.footer_read);
        footer_critic=itemView.findViewById(R.id.footer_critic);
        footer_like=itemView.findViewById(R.id.footer_like);
        item_topic_video_img=itemView.findViewById(R.id.item_topic_video_img);
        item_topic_video_title=itemView.findViewById(R.id.item_topic_video_title);
        pick=itemView.findViewById(R.id.pick);
      }

}

class ViewHolderC extends RecyclerView.ViewHolder{
    ImageView img;
    TextView title;
    ImageView zk_headimg;
    TextView zk_headname;
    TextView zk_headtime;
    TextView footer_read;
    TextView footer_critic;
    TextView footer_like;
    ImageView pick;

        public ViewHolderC(View itemView) {
            super(itemView);
          img=itemView.findViewById(R.id.item_topic_share_img);
          title=itemView.findViewById(R.id.item_topic_share_title);
            zk_headimg=itemView.findViewById(R.id.zk_headimg);
            zk_headname=itemView.findViewById(R.id.zk_headname);
            zk_headtime=itemView.findViewById(R.id.zk_headtime);
            footer_read=itemView.findViewById(R.id.footer_read);
            footer_critic=itemView.findViewById(R.id.footer_critic);
            footer_like=itemView.findViewById(R.id.footer_like);
            pick=itemView.findViewById(R.id.pick);
        }

    }
class ViewHolderD extends RecyclerView.ViewHolder{
    ImageView zk_headimg;
    TextView zk_headname;
    TextView zk_headtime;
    TextView texttitle;
    TextView footer_read;
    TextView footer_critic;
    ImageView pick;
    TextView footer_like;
        public ViewHolderD(View itemView) {
            super(itemView);
            zk_headimg=itemView.findViewById(R.id.zk_headimg);
            zk_headname=itemView.findViewById(R.id.zk_headname);
            zk_headtime=itemView.findViewById(R.id.zk_headtime);
            texttitle=itemView.findViewById(R.id.item_topic_text_title);
            footer_read=itemView.findViewById(R.id.footer_read);
            footer_critic=itemView.findViewById(R.id.footer_critic);
            footer_like=itemView.findViewById(R.id.footer_like);
            pick=itemView.findViewById(R.id.pick);


        }

    }

    class ViewHolderimg2 extends RecyclerView.ViewHolder{
        ImageView zk_headimg;
        TextView zk_headname;
        TextView zk_headtime;
        LinearLayout imagelist;
        TextView imgtitle;
        ImageView img1;
        ImageView img2;
        TextView footer_read;
        TextView footer_critic;
        TextView footer_like;
        ImageView pick;
        public ViewHolderimg2(View itemView) {
            super(itemView);

            zk_headimg=itemView.findViewById(R.id.zk_headimg);
            zk_headname=itemView.findViewById(R.id.zk_headname);
            zk_headtime=itemView.findViewById(R.id.zk_headtime);
            imgtitle=itemView.findViewById(R.id.item_topic_img2_tite);
            img1=itemView.findViewById(R.id.img2_img1);
            img2=itemView.findViewById(R.id.img2_img2);
            footer_read=itemView.findViewById(R.id.footer_read);
            footer_critic=itemView.findViewById(R.id.footer_critic);
            footer_like=itemView.findViewById(R.id.footer_like);
            pick=itemView.findViewById(R.id.pick);

        }

    }
    class ViewHolderimg3 extends RecyclerView.ViewHolder{
        ImageView zk_headimg;
        TextView zk_headname;
        TextView zk_headtime;
        LinearLayout imagelist;
        TextView imgtitle;
        ImageView img1;
        ImageView img2;
        ImageView img3;
        TextView footer_read;
        TextView footer_critic;
        TextView footer_like;
        ImageView pick;
        public ViewHolderimg3(View itemView) {
            super(itemView);

            zk_headimg=itemView.findViewById(R.id.zk_headimg);
            zk_headname=itemView.findViewById(R.id.zk_headname);
            zk_headtime=itemView.findViewById(R.id.zk_headtime);
            imgtitle=itemView.findViewById(R.id.item_topic_img3_tite);
            img1=itemView.findViewById(R.id.img2_img1);
            img2=itemView.findViewById(R.id.img2_img2);
            img3=itemView.findViewById(R.id.img2_img3);
            footer_read=itemView.findViewById(R.id.footer_read);
            footer_critic=itemView.findViewById(R.id.footer_critic);
            footer_like=itemView.findViewById(R.id.footer_like);
            pick=itemView.findViewById(R.id.pick);
        }

    }


    public static String stampToTime(long stamp) {
        String time;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(stamp*1000);
        time = simpleDateFormat.format(date);
        return time;    }



}