How to use RecyclerView in Android
This article mainly introduces the relevant knowledge of how to use RecyclerView in Android, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use RecyclerView in Android. Let's take a look at it.
First define a class Abc
Public class Abc {private String name;private int imageid;public Abc (String name,int iamgeid) {this.name=name;this.imageid=imageid;} public String getname () {return name;} public int getimageid () {return imageid;}}
Then prepare the adapter
Public class AbcAdapter extands RecyclerView.Adapter {private List M. public class ViewHolder extands RecyclerView.ViewHolder {ImageView abcimage;TextView abctext;public class ViewHolder (View view) {super (view); abcimage= (ImageView) findViewByid (R.id.XXX); abctext= (TextView) findVieByid (R.id.XX);} public AbcAdapter (Listabc) {mendabc;} public ViewHolder onCreateViewHolder (ViewGroup parent,int Viewtype) {View view=LayoutInflate.from (parent.getContext ()). } public ViewHolder onBindViewHolder (ViewHolder holder int position) {Abc abc=m.get (position); holder.iamge.setImageResource (abc.getimage); holder.name=setText (abc.gettext);} public int getItemCount () {return m.size ();}}
Finally, in MainActivity
XXX// loads data, and custom functions load data that need to be displayed in the function: RecyclerView recycler= (RecyclerView) findViewByid (R.id.XXX); LinearLayoutManager LayoutManager= new LinearLayoutManager (thsi); recycler.setLayoutmanager (layoutmanager); AbcAdapter dapter= new AbcAdapter (m); / / m is a collection, and a set recyclerView.setAdapter (adapter) should be defined above; this is the end of the article on "how to use RecyclerView in Android". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use RecyclerView in Android". If you want to learn more, you are welcome to follow the industry information channel.