本案例来自于学校的一个简单的课程实验 先看效果图,可以显然的看到,一些item是不同的布局,而其他布局就是简单的布局嵌套 看一下xml代码:
本案例来自于学校的一个简单的课程实验
先看效果图,可以显然的看到,一些item是不同的布局,而其他布局就是简单的布局嵌套
看一下xml代码:
XML代码中部分重复样式写入到了style文件中
看一下加入style.xm的代码,这个文件是存放在value文件夹下,存放样式、主题等。
看一下Activity的Java代码
使用数组存放标题、内容、时间、图片等数据(因为现在还只是死数据)
setDate(单词拼错了!) 该函数,就是将数据填充到NewsList(一个泛型List)中
然后就是new一个自己声明的一个自定义适配器,使用Listview的setAdapter方法设置其适配器
public class MainActivity40 extends AppCompatActivity {private String[] titles={"各地餐企齐行动,杜绝餐饮浪费","花菜有人焯水,有人直接炒,都错了,看饭店大厨如何做","睡觉时,双脚突然蹬一下,有踩空感,像从高楼坠落,是咋回事?","实拍外卖小哥砸开小吃店的卷帘门救火,灭火后淡定决定继续送外卖", "还没成熟就被迫提前采摘,8毛一斤却没人要,果农无奈,不摘不行","大会、大展、大赛一起来,北京电竞“好嗨哟”"};private String[] names={"央视新闻客户端","味美食记","民富康健康","生活小记","禾木报告","燕鸣"};private String[] comments={"1234评","14214评","534评","134评","1353评","876评"};private String[]times={"刚刚","6小时前","8小时前","2小时前","刚刚","4小时前"};private int[] icons1={R.drawable.food,R.drawable.takeout,R.drawable.e_sports};private int[] icons2={R.drawable.sleep1,R.drawable.sleep2,R.drawable.sleep3,R.drawable.fruit1,R.drawable.fruit2,R.drawable.fruit3};private int[] types={1,1,2,1,2,1};private RecyclerView mRecyclerView;private myAdapter myAdapter;private List NewsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main40); setDate(); mRecyclerView=findViewById(R.id.rv_list); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); myAdapter=new myAdapter(MainActivity40.this,NewsList); mRecyclerView.setAdapter(myAdapter); } private void setDate(){ NewsList=new ArrayList(); NewsBean bean; for (int i = 0; i < titles.length; i++) { bean=new NewsBean(); bean.setId(i+1); bean.setTitle(titles[i]); bean.setName(names[i]); bean.setComment(comments[i]); bean.setTime(times[i]); bean.setType(types[i]); switch (i){ case 0: List imgList0=new ArrayList<>(); bean.setImgList(imgList0); break; case 1: List imgList1=new ArrayList<>(); imgList1.add(icons1[i-1]); bean.setImgList(imgList1); break; case 2: List imgList2=new ArrayList<>(); imgList2.add(icons2[i-2]); imgList2.add(icons2[i-1]); imgList2.add(icons2[i]); bean.setImgList(imgList2); break; case 3: List imgList3=new ArrayList<>(); imgList3.add(icons1[i-2]); bean.setImgList(imgList3); break; case 4: List imgList4=new ArrayList<>(); imgList4.add(icons2[i-1]); imgList4.add(icons2[i]); imgList4.add(icons2[i+1]); bean.setImgList(imgList4); break; case 5: List imgList5=new ArrayList<>(); imgList5.add(icons1[i-3]); bean.setImgList(imgList5); break; } NewsList.add(bean); } }}
其中涉及到了适配器,话不多说,上适配器代码,适配器是连接数据与Listview的一个“桥梁”
RecyclerView与ListView的适配器其实共同特征有很多。
在自定义的适配器中,首先是一个构造方法,来获取当前上下文以及数据列表
class myAdapter extends RecyclerView.Adapter{ private Context mContext; private List NewsList; public myAdapter(Context context, List list){ this.mContext=context; this.NewsList=list; } @NonNull @NotNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) { View itemView=null; RecyclerView.ViewHolder holder=null; if(viewType==1){ itemView= LayoutInflater.from(mContext).inflate(R.layout.list_item_one,parent,false); holder=new ViewHolder1(itemView); }else if(viewType==2){ itemView= LayoutInflater.from(mContext).inflate(R.layout.list_item_two,parent,false); holder=new ViewHolder2(itemView); } return holder; } @Override public int getItemViewType(int position) { return NewsList.get(position).getType(); } @Override public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) { NewsBean bean=NewsList.get(position); if(holder instanceof ViewHolder1){ if(position==0){ ((ViewHolder1)holder).iv_top.setVisibility(View.VISIBLE); ((ViewHolder1)holder).iv_img.setVisibility(View.GoNE); }else { ((ViewHolder1)holder).iv_top.setVisibility(View.GONE); ((ViewHolder1)holder).iv_img.setVisibility(View.VISIBLE); } ((ViewHolder1)holder).title.setText(bean.getTitle()); ((ViewHolder1)holder).name.setText(bean.getName()); ((ViewHolder1)holder).comment.setText(bean.getComment()); ((ViewHolder1)holder).time.setText(bean.getTime()); if (bean.getImgList().size()==0)return; ((ViewHolder1)holder).iv_img.setImageResource(bean.getImgList().get(0)); }else if(holder instanceof ViewHolder2){ ((ViewHolder2) holder).title.setText(bean.getTitle()); ((ViewHolder2) holder).name.setText(bean.getName()); ((ViewHolder2) holder).comment.setText(bean.getComment()); ((ViewHolder2) holder).time.setText(bean.getTime()); ((ViewHolder2) holder).iv_img1.setImageResource(bean.getImgList().get(0)); ((ViewHolder2) holder).iv_img2.setImageResource(bean.getImgList().get(1)); ((ViewHolder2) holder).iv_img3.setImageResource(bean.getImgList().get(2)); } } @Override public int getItemCount() { return NewsList.size(); } class ViewHolder1 extends RecyclerView.ViewHolder { ImageView iv_top,iv_img; TextView title,name,comment,time; public ViewHolder1(@NonNull @NotNull View itemView) { super(itemView); iv_top=itemView.findViewById(R.id.iv_top); iv_img=itemView.findViewById(R.id.iv_img); title=itemView.findViewById(R.id.tv_title); name=itemView.findViewById(R.id.tv_name); comment=itemView.findViewById(R.id.tv_comment); time=itemView.findViewById(R.id.tv_time); } } class ViewHolder2 extends RecyclerView.ViewHolder { ImageView iv_img1,iv_img2,iv_img3; TextView title,name,comment,time; public ViewHolder2(@NonNull @NotNull View itemView) { super(itemView); iv_img1=itemView.findViewById(R.id.iv_img1); iv_img2=itemView.findViewById(R.id.iv_img2); iv_img3=itemView.findViewById(R.id.iv_img3); title=itemView.findViewById(R.id.tv_title); name=itemView.findViewById(R.id.tv_name); comment=itemView.findViewById(R.id.tv_comment); time=itemView.findViewById(R.id.tv_time); } }}
代码解说将会在后续补充
5-3 现在补充一下 list_item_one.xml与list_item_two.xml,这两个布局文件是Listview中两个item
补充一下NewBean 其实就是个实体类啦 其中get、set方法以及构造方法可以快速生成
import java.util.List;public class NewsBean { private int id; private String title; private List imgList; private String name; private String comment; private String time; private int type; public NewsBean() { } public NewsBean(int id, String title, List imgList, String name, String comment, String time, int type) { this.id = id; this.title = title; this.imgList = imgList; this.name = name; this.comment = comment; this.time = time; this.type = type; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List getImgList() { return imgList; } public void setImgList(List imgList) { this.imgList = imgList; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public int getType() { return type; } public void setType(int type) { this.type = type; }}
6-26补充一下使用的顶部布局
在res/layout下创建title_bar.xml
来源地址:https://blog.csdn.net/m0_59558544/article/details/130224474
--结束END--
本文标题: Android Studio初学者实例:RecyclerView学习--模仿今日头条
本文链接: https://lsjlt.com/news/433539.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0