Awdi
Awdi

Reputation: 161

How to use RecyclerView and CardView

I don't have much experience in android development and try to implement RecyclerView in my application. The version of android studio does not have Android L neither there is option to install. Everytime it says android.support.widget.v7.RecyclerView in not used and disabled it from import packages. I give refrences in layout file also in Gradle.build but my problem is still there anyone help please?

Upvotes: 5

Views: 11125

Answers (6)

Amir Dora.
Amir Dora.

Reputation: 2717

implementation 'com.android.support:design:29+'

Changed version according to your targetSdkVersion in build.grade(:app) file, in my case it's 29

Upvotes: 0

Abhishek
Abhishek

Reputation: 2370

Follow this line

  • CardView and RecyclerView in Material Design

http://icetea09.com/blog/2014/12/19/android-cardview-and-recyclerview-in-material-design/

add these into the dependencies:

    compile 'com.android.support:cardview-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.2.0'

and update the gradle in module's build.gradle

  • Example for each one:

CardView

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:contentPadding="16dp"
    card_view:cardElevation="2dp"
    card_view:cardCornerRadius="5dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Headline"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Title" />

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Body1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Content here" />

    </LinearLayout>

</android.support.v7.widget.CardView>

RecyclerView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Upvotes: 8

Hugo Gresse
Hugo Gresse

Reputation: 17899

According to the documentation you have to add specials dependencies to your gradle file :

dependencies {
    ...
    compile 'com.android.support:appcompat-v7:27.0.1'
    compile 'com.android.support:cardview-v7:27.0.1'
    compile 'com.android.support:recyclerview-v7:27.0.1'

}

To use it, always prefix with android.support.v7.widget.

Upvotes: 1

Sachin Rajput
Sachin Rajput

Reputation: 4344

EXPLANATION OF HOW YOU CAN USE RECYCLER VIEW IN ANDROID

Here are the steps with the explanation how you can use Recycler View

  • go to app gradle file

  • add the dependency for Recycler View compile 'com.android.support:recyclerview-v7:25.3.1' (Use latest one dependency at the time you are creating your project) , and sync the project

  • in your activity/ fragment file use the Recycler View with this tag -- recyler view is available in v7 widget support files

  • go to you respective Java File and get a reference to this recycler view

  • create a new layout (xml) file to create the custom view you want to infilate in your recyler view and name it like custom_row.xml .

  • Create a Java class and name it RecyclerviewHolder and extend it with RecyclerView.ViewHolder and create an super constructor , and then get access to all the views in your custom-row.xml file

  • now create an another Java class named RecylerAdapter or MyRecyclerAdapter and extend it by RecyclerView.Adapter and pass your RecyclerviewHolder class here like

    class RecyclerAdapter extends RecyclerView.Adapter<RecyclerviewHolder>

  • and then create one constructor and just press alt+enter it will generate three methods for you , i.e. you just need to override these three methods of RecyclerView.Adapter class

these three methods are :-

{ onCreateViewHolder , OnBindViewHolder , getCount }

  • OnCreateViewHolder - a method where you will pass your custom view layout to infilate it with LayoutInflater and in this method only you need to create an Object of RecyclerviewHolder class also after that you will return this view object in this method .

  • OnBindViewHolder - you can perform any action here on your views just prefix holder. to all your views name and use them to perform any action or for any event handling

  • getCount - here we will return the value of count how many time you want to infilate your custom view

  • now go to your respectve Activity or Fragment File and create one object of RecyclerAdapter Class and set this adapter on your recycler view .

Upvotes: 2

AlexM
AlexM

Reputation: 1135

You can use Recyclerview without CardView.But CardView gives more features to design the list row.

Upvotes: 0

Neel Krishna
Neel Krishna

Reputation: 25

RecyclerView uses an Adapter, which passes List items to it. The adapter returns an object of the type of List Item you choose to pass to the RecyclerView. In this post, you will learn how to implement RecyclerView in your app, and how to pass CardView objects to scroll through them.

Steps: 1) Build a CardView 2) Build a data model to populate your CardView 3) Build a RecyclerView 4) Build an adapter connecting your data to the RecyclerView 5) Set the adapter to your RecyclerView

This tutorial will help you take these steps to use RecyclerView and CardView in your Android app:

https://knowledgecollisions.wordpress.com/2016/03/29/using-recyclerview-and-cardview-in-your-android-app/

Upvotes: 0

Related Questions