安卓recyclerview
程序员文章站
2022-05-05 14:21:12
...
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="650dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="51dp"
android:background="#D6D5D5"
android:text="微信(73)"
android:textColor="@color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="TextView"
android:textColor="@color/black"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView2" />
<ImageView
android:id="@+id/imageView"
android:layout_width="61dp"
android:layout_height="56dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="285dp"
android:layout_height="39dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
chat.java
package com.hnucm.c16;
import android.graphics.drawable.Drawable;
public class Chat {
private String name;
private String content;
private String time;
private int img;
public Chat (String name,String content,String time,int img){
this.name=name;
this.content=content;
this.time=time;
this.img=img;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
MainActivity.java
package com.hnucm.c16;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Chat> list =new ArrayList<>();
RecyclerView mRecyvleView;
MyAdatper myAdatper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Chat c1=new Chat("腾讯新闻","女子去三亚发现海滩大量...","21:18",R.drawable.l1);
list.add(c1);
Chat c2=new Chat("订阅号","场库:《我的姐姐》,来...","20:20",R.drawable.l2);
list.add(c2);
Chat c3=new Chat("微信支付","微信支付凭证","19:55",R.drawable.l3);
list.add(c3);
Chat c4=new Chat("微信运动","[应用消息]","16:56",R.drawable.l4);
list.add(c4);
Chat c5=new Chat("瑶瑶","再见","12:00",R.drawable.l5);
list.add(c5);
Chat c6=new Chat("妈妈","[视频通话]","昨天",R.drawable.l6);
list.add(c6);
Chat c7=new Chat("爸爸","[视频通话]","昨天",R.drawable.l7);
list.add(c7);
mRecyvleView =findViewById(R.id.recyclerview);
myAdatper =new MyAdatper();
mRecyvleView.setLayoutManager(new LinearLayoutManager(this));
mRecyvleView.setAdapter(myAdatper);
}
public class MyAdatper extends RecyclerView.Adapter<MyViewHold>{
@NonNull
@Override
public MyViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_list,parent,false);
MyViewHold myViewHold=new MyViewHold(view);
return myViewHold;
}
@Override
public void onBindViewHolder(@NonNull MyViewHold holder, int position) {
holder.nametextView1.setText(list.get(position).getName());
holder.ctextView2.setText(list.get(position).getContent());
holder. ttextView3.setText(list.get(position).getTime());
holder.imagetv.setImageResource(list.get(position).getImg());
}
@Override
public int getItemCount() {
return list.size();
}
}
public class MyViewHold extends RecyclerView.ViewHolder{
TextView nametextView1;
TextView ctextView2;
TextView ttextView3;
ImageView imagetv;
public MyViewHold(@NonNull View itemView) {
super(itemView);
nametextView1=itemView.findViewById(R.id.textView2);
ctextView2=itemView.findViewById(R.id.textView4);
ttextView3=itemView.findViewById(R.id.textView3);
imagetv=itemView.findViewById(R.id.imageView);
}
}
上一篇: 安卓的fragment切换+左右滑动
下一篇: 去洗浴中心洗澡
推荐阅读
-
微信小程序 wx.uploadFile在安卓手机上面the same task is working问题解决
-
拉拉公园廖卓营:有了620万元融资,继续做女同社区的领跑者
-
安卓手机助手Jihosoft Android Manager英文安装及注册教程(附注册码)
-
关于ADB的Android Debug Bridge(安卓调试桥)那些事
-
如何使用雷电安卓模拟器获得挖矿共生币?
-
Android RecyclerView实现点击条目删除
-
安卓11给力升级:可快速查看蓝牙耳机所能支持的编码格式
-
iQOO 3 5G安兔兔跑分曝光:59.7万分无敌
-
报告:苹果降低iPhone出货量 安卓手机厂商忙着清库存
-
余承东怒赞华为Mate30:安卓手机首次在流畅度超越iPhone 11