Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में कोटलिन का उपयोग करके हॉरिजॉन्टल लिस्टव्यू (RecyclerView) कैसे बनाएं?

एंड्रॉइड में हॉरिजॉन्टल RecyclerView बनाना

एंड्रॉइड में हॉरिजॉन्टल सूची बनाने के लिए RecyclerView के साथ LinearLayoutManager.HORIZONTAL का उपयोग किया जाता है। यह पुराने HorizontalListView से बेहतर और लचीला है।

चरण 1: नया प्रोजेक्ट बनाएं

एंड्रॉइड स्टूडियो में File → New Project चुनें और आवश्यक जानकारी भरें।

चरण 2: मुख्य लेआउट (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
    android:id="@+id/rlMain" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="16dp" 
    android:orientation="vertical"> 
    <androidx.recyclerview.widget.RecyclerView 
        android:id="@+id/recyclerView" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
</LinearLayout>

चरण 3: MainActivity.kt

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.ArrayList

class MainActivity : AppCompatActivity() {
    private val movieList = ArrayList<MovieModel>()
    private lateinit var moviesAdapter: MoviesAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        moviesAdapter = MoviesAdapter(movieList)
        
        // हॉरिजॉन्टल लेआउट मैनेजर सेट करें
        val layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        recyclerView.layoutManager = layoutManager
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.adapter = moviesAdapter
        
        prepareMovieData()
    }
    
    private fun prepareMovieData() {
        val movies = listOf(
            MovieModel("Mad Max: Fury Road", "Action & Adventure", "2015"),
            MovieModel("Inside Out", "Animation, Kids & Family", "2015"),
            MovieModel("Star Wars: Episode VII", "Action", "2015"),
            MovieModel("Shaun the Sheep", "Animation", "2015"),
            MovieModel("The Martian", "Science Fiction & Fantasy", "2015"),
            MovieModel("Mission: Impossible Rogue Nation", "Action", "2015"),
            MovieModel("Up", "Animation", "2009"),
            MovieModel("Star Trek", "Science Fiction", "2009"),
            MovieModel("The LEGO Movie", "Animation", "2014"),
            MovieModel("Iron Man", "Action & Adventure", "2008"),
            MovieModel("Aliens", "Science Fiction", "1986"),
            MovieModel("Chicken Run", "Animation", "2000"),
            MovieModel("Back to the Future", "Science Fiction", "1985"),
            MovieModel("Raiders of the Lost Ark", "Action & Adventure", "1981"),
            MovieModel("Goldfinger", "Action & Adventure", "1965"),
            MovieModel("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014")
        )
        movieList.addAll(movies)
        moviesAdapter.notifyDataSetChanged()
    }
}

चरण 4: MovieModel.kt (डेटा क्लास)

data class MovieModel(
    val title: String,
    val genre: String,
    val year: String
)

चरण 5: MoviesAdapter.kt

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView

class MoviesAdapter(private val moviesList: List<MovieModel>) : 
    RecyclerView.Adapter<MoviesAdapter.MyViewHolder>() {

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val title: TextView = view.findViewById(R.id.title)
        val year: TextView = view.findViewById(R.id.year)
        val genre: TextView = view.findViewById(R.id.genre)
    }

    @NonNull
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.movie_list, parent, false)
        return MyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val movie = moviesList[position]
        holder.title.text = movie.title
        holder.genre.text = movie.genre
        holder.year.text = movie.year
    }

    override fun getItemCount(): Int = moviesList.size
}

चरण 6: आइटम लेआउट (movie_list.xml)

<?xml version="1.0" encoding="utf-8"?> 
<androidx.cardview.widget.CardView xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp"> 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp"> 
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:textStyle="bold" /> 
        <TextView
            android:id="@+id/genre"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/darker_gray"
            android:textSize="14sp" /> 
        <TextView
            android:id="@+id/year"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/darker_gray"
            android:textSize="12sp" /> 
    </LinearLayout> 
</androidx.cardview.widget.CardView>

चरण 7: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="https://schemas.android.com/apk/res/android" 
    package="com.example.horizontalrecyclerview"> 
    <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:label="@string/app_name" 
        android:roundIcon="@mipmap/ic_launcher_round" 
        android:supportsRtl="true" 
        android:theme="@style/AppTheme"> 
        <activity android:name=".MainActivity"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
    </application> 
</manifest>

मुख्य बिंदु:

  • LinearLayoutManager.HORIZONTAL: हॉरिजॉन्टल स्क्रॉलिंग सक्षम करता है
  • CardView: प्रत्येक आइटम को कार्ड के रूप में दिखाता है
  • Data Class: कोटलिन में डेटा मॉडल बनाने का संक्षिप्त तरीका
  • wrap_content ऊंचाई: RecyclerView की ऊंचाई सामग्री के अनुसार समायोजित होती है
  1. - Kotlin का उपयोग करके Android ListView पर लॉन्ग क्लिक लिस्टनर कैसे लागू करें?

    परिचय यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android ListView पर लॉन्ग क्लिक लिस्टनर कैसे लागू किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio खोलें, File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2: activity_main.xml लेआउट जोड़ें res/layout/activity_main.xml

  1. - Kotlin का उपयोग करके Android पर स्क्रॉल करने योग्य TextView कैसे बनाएं?

    परिचय यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android पर स्क्रॉल करने योग्य TextView कैसे बनाया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio खोलें, File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2: activity_main.xml लेआउट जोड़ें res/layout/activity_main.xml में

  1. - Kotlin में Android SwipeRefreshLayout कैसे बनाएं?

    परिचय SwipeRefreshLayout उपयोगकर्ताओं को नीचे खींचकर कंटेंट रीफ्रेश करने की अनुमति देता है। यह आधुनिक एंड्रॉइड ऐप्स में एक सामान्य पैटर्न है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट फ़ाइल में कोड जोड़ें res/layout/activity_main.xml में SwipeRefreshLayou