خرید بک لینک

Step 6: Create RecyclerView
For creating our RecyclerView. Navigate to the app > res > layout > activity_main.xml and add RecyclerView as shown below. Below is the code for the activity_main.xml file.

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:id="@+id/idRVCourse"

android:layout_width="match_parent"

android:layout_height="match_parent" />

Step 7: Initializing RecyclerView in our MainActivity
Navigate to the app > java > your apps package name > MainActivity and initialize your RecyclerView. Below is the code for the MainActivity file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.LinearLayoutManager;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

RecyclerView courseRV = findViewById(R.id.idRVCourse);

// Here, we have created new array list and added data to it

ArrayList courseModelArrayList = new ArrayList();

courseModelArrayList.add(new CourseModel("DSA in Java", 4, R.drawable.gfgimage));

courseModelArrayList.add(new CourseModel("Java Course", 3, R.drawable.gfgimage));

courseModelArrayList.add(new CourseModel("C++ Course", 4, R.drawable.gfgimage));

courseModelArrayList.add(new CourseModel("DSA in C++", 4, R.drawable.gfgimage));

courseModelArrayList.add(new CourseModel("Kotlin for Android", 4, R.drawable.gfgimage));

courseModelArrayList.add(new CourseModel("Java for Android", 4, R.drawable.gfgimage));

courseModelArrayList.add(new CourseModel("HTML and CSS", 4, R.drawable.gfgimage));

// we are initializing our adapter class and passing our arraylist to it.

CourseAdapter courseAdapter = new CourseAdapter(this, courseModelArrayList);

// below line is for setting a layout manager for our recycler view.

// here we are creating vertical list so we will provide orientation as vertical

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

// in below two lines we are setting layoutmanager and adapter to our recycler view.

courseRV.setLayoutManager(linearLayoutManager);

courseRV.setAdapter(courseAdapter);

}
}

Note: Star used as an image is stored in our drawable folder. Strings used are present in our strings.xml.

strings.xml: Navigate to the app > res > values > strings.xml to add various strings in your app.
drawable: Navigate to the app > res > drawable to add images used for your app.
Output:

CardView using RecyclerView in Android

+ نوشته شده در جمعه بیست و ششم آبان ۱۴۰۲ ساعت 12 توسط وحید حسن نژاد  | 

برچسب: نویسنده: دانیال باقری تاريخ: سه شنبه 30 آبان 1402 ساعت: 19:52

صفحه بندی