Вы находитесь на странице: 1из 19

Android :

Use RecyclerView with


CardView in Android application

http://pulse7.net/
* The RecyclerView widget is a more advanced and flexible
version of List View.

* This widget is a container for displaying large data sets


that can be scrolled very efficiently by maintaining a
limited number of views.

* RecyclerView provides LinearLayoutManager, which can


shows the list of items in either horizontal or vertical
pattern.

http://pulse7.net/
* The CardView UI component
shows information inside
cards. We can customize its
corners, elevation and so on.

* In this session we will create an


android application using
RecyclerView and CardView.

http://pulse7.net/
* I will tell you steps for how to create RecyclerView with
CardView in Android application. Follow steps:

1. You have to add following in build.gradle

http://pulse7.net/
2. RecyclerView is very similar to the ListView and we can
use them in the same way. In activity_main layout , we will
define Recyclerview widget.

Continue...
http://pulse7.net/
3. In list view as we define row view, in the same way, we
define our row view in Card view. Create layout file
card_view.xml in resources/layout.

http://pulse7.net/
3.

http://pulse7.net/
4. Now create the model class called Flower.java and create
the constructor of Flower class.

http://pulse7.net/
5. Now we have to create adapter class to hold the view. So,
create CardAdapter.java class. Recyclerview has two
override methods:
a) onCreateViewHolder: Inflate row view in this method
b) onBindViewHolder: Bind the view in this method
(equivalent to getview method of listview)
CardAdapter must extend a class called,
RecyclerView.Adapter passing our class that implements
the ViewHolder pattern.

http://pulse7.net/
5.

http://pulse7.net/
6. Now implement override methods OnCreateViewHolder,
OnBindViewHolder and getItemCount method.

7. In onCreateViewHolder inflate our layout file card_view.

http://pulse7.net/
8. In onBindViewHolder, set text and image resource.

9. Create constructor of CardAdapter class containing context


and list.

http://pulse7.net/
10. So, Final code will look like as follows:

Continue...
http://pulse7.net/
10.

http://pulse7.net/
11. Now in MainActivity class set it's contentview as
activity_main. Create method “initializeData()” which will
contain array list of flowers and call the constructor of
Flower class. Set title and image into it. Call this method
before setting adapter.

http://pulse7.net/
11.

Continue...
http://pulse7.net/
11.

http://pulse7.net/
12. After execute application, we get this type of output:

http://pulse7.net/
Download Full Source Code and Subscribe (Facebook, Twitter,
Google+) for more article

http://pulse7.net/

http://pulse7.net/

Вам также может понравиться