赞
踩
RecyclerView跟ListView类似,但是却比ListView更好用,算是ListView的升级版,谷歌官方也推荐使用RecylerView代替ListView。
关于ListView的用法可以查看上一篇博客
【Android】ListView的用法、自定义adapter(性能优化)
RecyclerView不属于support库的内容,需要添加依赖引入项目
build.gradle
dependencies {
...
implementation 'androidx.recyclerview:recyclerview:1.1.0-rc01'
}
recyclerview版本一直会更新,上面是写博客时的最新版本。可以用Android Studio搜索到最新版本。关于添加依赖,可以查看博客
【Android】记不住gradle的写法?Android Studio搜索添加依赖(dependencies)
把图片复制到drawerable目录内备用,我这里只准备了一张图片重复用
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
<androidx.recyclerview.widget.RecyclerView 注意要写全包名
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
新建一个recyclerView的item布局,就放一个图片和一个文本
contact_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="90dp"> <ImageView android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp"/> <TextView android:id="@+id/tv" android:layout_marginStart="20dp" android:layout_marginTop="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Contact.java
public class Contact { private String name; private int imageId; public Contact(String name, int imageId){ this.name = name; this.imageId = imageId; } public String getName(){ return name; } public int getImageId(){ return imageId; } }
我们需要新建一个Adapter继承于RecyclerView .Adapter,同样地,ViewHolder 也要继承于RecyclerView的ViewHolder,ViewHolder能提高性能。其实代码很简单,就是写了2个构造方法并且实现了3个接口,构造方法用于接收数据,重写的3个方法用于处理数据
ContactAdapter.java
public class ContactAdapter extends RecyclerView .Adapter<ContactAdapter.ViewHolder>{ private List<Contact> mContactList; static class ViewHolder extends RecyclerView.ViewHolder{ View contactView;//存储解析到的view ImageView imageView; TextView textView; public ViewHolder(View view){ super(view); contactView = view; imageView = view.findViewById(R.id.iv); textView = view.findViewById(R.id.tv); } } public ContactAdapter(List<Contact> contactList){ mContactList = contactList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_layout,parent,false);//解析layout final ViewHolder viewHolder = new ViewHolder(view);//新建一个viewHolder绑定解析到的view //监听每一项的点击事件 viewHolder.contactView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int position = viewHolder.getAdapterPosition(); Contact contact = mContactList.get(position); Toast.makeText(view.getContext(),contact.getName(),Toast.LENGTH_SHORT).show(); } }); //监听每一项里的控件的点击事件,如点击了ImageView viewHolder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int position = viewHolder.getAdapterPosition(); Contact contact = mContactList.get(position); Toast.makeText(view.getContext(),contact.getImageId(),Toast.LENGTH_SHORT).show(); } }); return viewHolder; } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Contact contact = mContactList.get(position); holder.imageView.setImageResource(contact.getImageId()); holder.textView.setText(contact.getName()); } @Override public int getItemCount() { return mContactList.size(); } }
MainActivity.java
public class MainActivity extends AppCompatActivity { private List<Contact> contactList = new ArrayList<>();//存储实例化的数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addContact();//往contactList添加数据 RecyclerView recyclerView = findViewById(R.id.recyclerView); LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);//添加布局管理器 //layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);设置为横向水平滚动,默认是垂直 recyclerView.setLayoutManager(layoutManager);//设置布局管理器 ContactAdapter adapter = new ContactAdapter(contactList); recyclerView.setAdapter(adapter); } private void addContact(){ for (int i = 0;i<3;i++){ Contact LiuMengLi = new Contact("柳梦璃",R.drawable.img); contactList.add(LiuMengLi); Contact HanLingSha = new Contact("韩菱纱",R.drawable.img); contactList.add(HanLingSha); Contact YunTianHe = new Contact("云天河",R.drawable.img); contactList.add(YunTianHe); Contact MuRongZiYing = new Contact("慕容紫英",R.drawable.img); contactList.add(MuRongZiYing); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。