2021年2月2日星期二

How to add a Recycler View to an activity in Java

I have tried to create RecyclerView in an Activity, but I don't get the correct way to add a RecyclerView. I searched on the Internet and saw a lot of videos on it, but the method is different on every website, so I was confused. I want to create a RecyclerView that displays flags and Names of all the list of countries in Asia.

Here is my code

Main Activity

    import androidx.appcompat.app.AppCompatActivity      import android.os.Bundle      import androidx.recyclerview.widget.LinearLayoutManager      import androidx.recyclerview.widget.RecyclerView      import kotlinx.android.synthetic.main.activity_main.view.*        class MainActivity : AppCompatActivity() {            lateinit var recyclerDashboard: RecyclerView            lateinit var layoutManager:RecyclerView.LayoutManager                    val sumList= arrayListOf("Russia","China","India","Kazakhstan","Saudi Arabia","Iran","Mangolia","Indonesia","Pakistan","Turkey","Mayanmar","Afghanistan","Yemen","Thailand","Turkmenistan","Uzbekistan","Iraq","Japan","Vietnam","Malaysia",          "Oman","Philippines","Iaos","kyrgyzstan","Syria","Combodia","Bangladesh","Nepal","Tujikistan","North Korea","South Korea","Azerbaijan","United Arab Emirates",          "Georgia", "Sri Lanka","Bhutan","Tiwan","Armenia","Israel","Kuwait","Timor Leste","Qatar","Lebanon","Cyprus","Pelastine","Brunei","Bahrain","Singapore","Maldives")            lateinit var recyclerAdapter: DashboardRecyclerAdapter            override fun onCreate(savedInstanceState: Bundle?) {              super.onCreate(savedInstanceState)              setContentView(R.layout.activity_main)                recyclerDashboard=findViewById(R.id.recyclerDashboard)              layoutManager=LinearLayoutManager(this)              recyclerAdapter= DashboardRecyclerAdapter(sumList)          }      }  

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>      <RelativeLayout          xmlns:android="http://schemas.android.com/apk/res/android"          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/recyclerDashboard"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:layout_margin="5dp"          android:scrollbars="vertical"          android:padding="10dp"          tools:listitem="@layout/recycler_dashboard_single_row" />          </RelativeLayout>  

Data Class

    data class User(          val id:Int=0,          val country_name:String?=null,          var country_capital:String?=null,          var country_flag:String?=null,          var country_region:String?=null,          var country_subregion:String?=null,          var country_population:String?=null,          var country_borders:String?=null,          var country_languages:String?=null      )  

Adapter Class

    package com.bindu.asiancountries      import android.view.LayoutInflater      import android.view.View      import android.view.ViewGroup      import android.widget.ImageView      import android.widget.TextView      import androidx.recyclerview.widget.RecyclerView        class DashboardRecyclerAdapter(          val itemList:ArrayList<String>      ):RecyclerView.Adapter<DashboardRecyclerAdapter.ViewHolder>() {            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {              val v=LayoutInflater.from(parent.context).inflate(R.layout.recycler_dashboard_single_row,parent,false)                            return ViewHolder(v)          }            override fun getItemCount(): Int {              return itemList.size          }            override fun onBindViewHolder(holder: ViewHolder, position: Int) {              val text=itemList[position]                        holder.textViewName.text= text          }            class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {              val image:ImageView=itemView.findViewById(R.id.russia)              val textViewName :TextView= itemView.findViewById(R.id.txtRussia)          }      }  

Item 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="match_parent"      xmlns:app="http://schemas.android.com/apk/res-auto">            <androidx.cardview.widget.CardView              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:layout_margin="10dp"              app:cardCornerRadius="4dp"              app:cardElevation="4dp">                <RelativeLayout                  android:layout_width="wrap_content"                  android:layout_height="wrap_content">                    <ImageView                      android:layout_width="100dp"                      android:id="@+id/russia"                      android:layout_height="100dp"                      android:src="@drawable/russia"                      android:layout_margin="20dp"/>                    <TextView                      android:id="@+id/txtRussia"                      android:layout_width="wrap_content"                      android:layout_toEndOf="@id/russia"                      android:layout_marginStart="120dp"                      android:textSize="30sp"                      android:layout_marginTop="50dp"                      android:layout_height="wrap_content"                      android:text="@string/russia" />                </RelativeLayout>            </androidx.cardview.widget.CardView>        </RelativeLayout>  
https://stackoverflow.com/questions/66012410/how-to-add-a-recycler-view-to-an-activity-in-java February 02, 2021 at 11:10PM

没有评论:

发表评论