How to use RecyclerView and CardView

I don't have much Android development experience and am trying to implement RecyclerView in my application. There is no Android L in android studio version and no way to install. Every time he says android.support.widget.v7.RecyclerView

not used and disconnects it from the import packages. I give refrences in layout file also in Gradle.build but my problem is still there, can anyone help?

+3


source to share


5 answers


follow this line

  • CardView and RecyclerView in Material Design

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

add them depending on :

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

      

and update girdle



  • example for each:

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>

      

+8


source


As per the documentation, you need to add custom 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.

+1


source


EXPLANATION HOW YOU CAN USE THE RECORDING VIEW IN ANDROID

Below are the steps explaining how you can use Recycler View

  • go to app gradle file

  • add dependency for Recycler view compile 'com.android.support:recyclerview-v7:25.3.1'

    (use the most recent dependency at the time of project creation) and sync the project

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

  • go to the corresponding java file and get the link to this recycler view

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

  • Create Java class and name it RecyclerviewHolder and extend it with RecyclerView.ViewHolder and create super constructor and then access all views in custom-row.xml file

  • now create another java class named RecylerAdapter or MyRecyclerAdapter and extend it to RecyclerView.Adapter and pass the 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 the RecyclerView.Adapter class

these three methods: -

{onCreateViewHolder, OnBindViewHolder, getCount}

  • OnCreateViewHolder is a method where you will pass your own view layout to unfold using LayoutInflater and in this method you need to create a RecyclerviewHolder class object also after that you will return this view object to this method.

  • OnBindViewHolder - You can do any action here on your views, just for the prefix holder. to all your views and use them to perform any action or to handle any events

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

  • go to your Activity or Fragment File and create one object of class RecyclerAdapter and install this adapter in your Recycler .

+1


source


The RecyclerView uses an adapter that passes the list items to it. The adapter returns an object of the list item type that you want to pass to the RecyclerView. In this post, you will learn how to implement a RecyclerView in your application and how to pass CardView objects to scroll them.

Steps: 1) Create CardView 2) Create data model to fill CardView 3) Create RecyclerView 4) Create adapter connecting your data to RecyclerView 5) Set adapter to your RecyclerView

This tutorial will guide you through the following 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/

0


source


You can use Recyclerview without CardView.But CardView provides more options for creating list row.

0


source







All Articles