Recyclerview的多种条目

Stella981
• 阅读 697

package comp.example.practise9.adapter;

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.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import comp.example.practise9.Lei.ShiTi; import comp.example.practise9.R;

/** * Created by 潘明杰 on 2017/11/13. */ public class RecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<ShiTi.ResultBean.DataBean> data;

public RecyAdapter(Context context, List<ShiTi.ResultBean.DataBean> data) {
    this.context = context;
    this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder holder = null;
    if (viewType == 0) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.one\_item, null, false);
        holder = new ViewHolder1(inflate);
    }
    if (viewType == 1) {
        View inflate1 = LayoutInflater.from(context).inflate(R.layout.two\_item, null, false);
        holder = new ViewHolder2(inflate1);
    }
    return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof ViewHolder1) {
        ((ViewHolder1) holder).tv1.setText(data.get(position).getAuthor\_name());
        ((ViewHolder1) holder).tv2.setText(data.get(position).getTitle());
        ((ViewHolder1) holder).tv3.setText(data.get(position).getCategory());
        Glide.with(context).load(data.get(position).getThumbnail\_pic\_s()).into(((ViewHolder1) holder).image\_1);
        Glide.with(context).load(data.get(position).getThumbnail\_pic\_s03()).into(((ViewHolder1) holder).image\_2);
        Glide.with(context).load(data.get(position).getThumbnail\_pic\_s02()).into(((ViewHolder1) holder).image\_3);

    }
    if (holder instanceof ViewHolder2) {
        ((ViewHolder2) holder).tv\_content.setText(data.get(position).getAuthor\_name());
        ((ViewHolder2) holder).tv\_text.setText(data.get(position).getTitle());
        ((ViewHolder2) holder).tv\_title.setText(data.get(position).getDate());
        Glide.with(context).load(data.get(position).getThumbnail\_pic\_s()).into(((ViewHolder2) holder).imagee);

    }

}

@Override
public int getItemCount() {
    return data.size();
}

@Override
public int getItemViewType(int position) {
    String thumbnail\_pic\_s02 = data.get(position).getThumbnail\_pic\_s02();
    if (thumbnail\_pic\_s02 != null) {
        return 0;

    } else {
        return 1;
    }

}

public  class ViewHolder1 extends RecyclerView.ViewHolder {
    public View rootView;
    public TextView tv1;
    public TextView tv2;
    public TextView tv3;
    public ImageView image\_1;
    public ImageView image\_2;
    public ImageView image\_3;

    public ViewHolder1(View rootView) {
        super(rootView);
        this.rootView = rootView;
        this.tv1 = (TextView) rootView.findViewById(R.id.tv1);
        this.tv2 = (TextView) rootView.findViewById(R.id.tv2);
        this.tv3 = (TextView) rootView.findViewById(R.id.tv3);
        this.image\_1 = (ImageView) rootView.findViewById(R.id.image\_1);
        this.image\_2 = (ImageView) rootView.findViewById(R.id.image\_2);
        this.image\_3 = (ImageView) rootView.findViewById(R.id.image\_3);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.setOnItemClickListener(view,getLayoutPosition());
            }
        });
        rootView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                OnLongItemClickListener.OnLongItemClickListener(view,getLayoutPosition());
                return true;
            }
        });
    }

}

public  class ViewHolder2 extends RecyclerView.ViewHolder {
    private  ImageView imagee;
    public View rootView;
    public TextView tv\_text;
    public TextView tv\_content;
    public TextView tv\_title;

    public ViewHolder2(View rootView) {
        super(rootView);
        this.rootView = rootView;
        this.tv\_text = (TextView) rootView.findViewById(R.id.tv\_text);
        this.tv\_content = (TextView) rootView.findViewById(R.id.tv\_content);
        this.tv\_title = (TextView) rootView.findViewById(R.id.tv\_title);
        this.imagee = (ImageView) rootView.findViewById(R.id.imagee);
       rootView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               itemClickListener.setOnItemClickListener(view,getLayoutPosition());
           }
       });
        rootView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                OnLongItemClickListener.OnLongItemClickListener(view,getLayoutPosition());
                return true;
            }
        });
    }

}
//1.第一步
public interface OnItemClickListener {
    void setOnItemClickListener(View v, int position);
}

public OnItemClickListener itemClickListener;



public void onClick(View v) {
    if (itemClickListener != null) {
        itemClickListener.setOnItemClickListener(v, (int) v.getTag());
    }
}

public void setOnItemClickListener(OnItemClickListener listener) {
    this.itemClickListener = listener;
}
//\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
public interface OnLongItemClickListener {
    void OnLongItemClickListener(View v, int position);
}

public OnLongItemClickListener OnLongItemClickListener;



public void onLongClick(View v) {
    if (OnLongItemClickListener != null) {
        OnLongItemClickListener.OnLongItemClickListener(v, (int) v.getTag());
    }
}

public void OnLongItemClickListener(OnLongItemClickListener listener) {
    this.OnLongItemClickListener = listener;
}

}

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Souleigh ✨ Souleigh ✨
3年前
前端性能优化 - 雅虎军规
无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化35条军规,这样对于优化有一个比较清晰的方向.35条军规1.尽量减少HTTP请求个数——须权衡2.使用CDN(内容分发网络)3.为文件头指定Expires或CacheControl,使内容具有缓存性。4.避免空的
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
35岁是技术人的天花板吗?
35岁是技术人的天花板吗?我非常不认同“35岁现象”,人类没有那么脆弱,人类的智力不会说是35岁之后就停止发展,更不是说35岁之后就没有机会了。马云35岁还在教书,任正非35岁还在工厂上班。为什么技术人员到35岁就应该退役了呢?所以35岁根本就不是一个问题,我今年已经37岁了,我发现我才刚刚找到自己的节奏,刚刚上路。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这