How to use recyclerView in android studio
recyclerView widget is a viewGroup that is the most powerful tool in android.
It is a very useful tool to display items in recyclerViewview.
In this blog we using recyclerView, also we will use adapter,viewholder and number of data.
All data display in a single XML file so also we will create a single textview data file.
MainActivity.java
package com.pd.recyclerview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyData[] myData = new MyData[]{ new MyData("account balance", R.drawable.ic_baseline_account_balance_24), new MyData("photo", R.drawable.ic_baseline_add_a_photo_24), new MyData("alert", R.drawable.ic_baseline_add_alert_24), new MyData("box", R.drawable.ic_baseline_add_box_24), new MyData("call", R.drawable.ic_baseline_add_ic_call_24), new MyData("shopping", R.drawable.ic_baseline_add_shopping_cart_24), new MyData("car", R.drawable.ic_baseline_electric_car_24), new MyData("check", R.drawable.ic_baseline_fact_check_24), new MyData("translate", R.drawable.ic_baseline_g_translate_24), }; RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview); MyAdapter myAdapter = new MyAdapter(myData); recyclerView.setAdapter(myAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); } }
activity_main.java
<?xml version="1.0" encoding="utf-8"?> <androidx.recyclerview.widget.RecyclerView 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" android:orientation="vertical" android:id="@+id/recyclerview" tools:context=".MainActivity"> </androidx.recyclerview.widget.RecyclerView>
MyAdapter.java
package com.pd.recyclerview; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private MyData[] mylist; public MyAdapter(MyData[] mylist) { this.mylist = mylist; } @NonNull @Override public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); View item = layoutInflater.inflate(R.layout.singledata, parent, false); ViewHolder viewHolder = new ViewHolder(item); return viewHolder; } @Override public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) { final MyData myData = mylist[position]; holder.textView.setText(mylist[position].getName()); holder.imageView.setImageResource(mylist[position].getImages()); holder.relativeLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), "You are Click on" + myData.getName(), Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return mylist.length; } public class ViewHolder extends RecyclerView.ViewHolder { public ImageView imageView; public TextView textView; public RelativeLayout relativeLayout; public ViewHolder(@NonNull View itemView) { super(itemView); this.imageView = (ImageView) itemView.findViewById(R.id.imageview); this.textView = (TextView) itemView.findViewById(R.id.textview); this.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativelayout); } } }
singledata.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="70sp" android:id="@+id/relativelayout" android:background="@drawable/border"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageview" android:layout_marginLeft="10dp" android:layout_centerVertical="true" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:gravity="center_vertical" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/imageview" android:layout_marginTop="15sp" android:text="@string/app_name" android:textSize="20dp"/> </RelativeLayout>
MyData.java
package com.pd.recyclerview; public class MyData { String name; int images; //create contractor public MyData(String name, int images) { this.name = name; this.images = images; } //getter and setter public String getName() { return name; } public void setName(String name) { this.name = name; } public int getImages() { return images; } public void setImages(int images) { this.images = images; } }
https://youtu.be/leq6dk6M8zI
SUBSCRIBE to downloadDownload Source code