Beruflich Dokumente
Kultur Dokumente
Firstly, to have some visual view about this post, you can take a look at this demo video:
A – C a rd V i e w
1 – W h a t i s C a rd V i e w?
CardView was brought along with Android 5.0 Lollipop. It extends FrameLayout and has
rounded background with shadow by default.
CardView is convenient for displaying complex content that has multiple heterogeneous
data type like images, text, videos…
2 – H o w t o u s e C a rd V i e w
To be able to use CardView in your application, you have to declare the required
dependencies like below:
1 dependencies {
2 compile 'com.android.support:appcompat-v7:21.0.0'
3 compile 'com.android.support:cardview-v7:21.0.+'
4 }
After that, in you layout XML file, you also need to declare the card_view namespace:
XHTML
1 xmlns:card_view=http://schemas.android.com/apk/res-auto
Like other controls, you can add CardView to the layout using XML.
1 <android.support.v7.widget.CardView
2 android:layout_width="fill_parent"
3 android:layout_height="wrap_content"
4 card_view:contentPadding="16dp"
5 card_view:cardElevation="2dp"
6 card_view:cardCornerRadius="5dp">
7
8 <LinearLayout
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:orientation="vertical">
12
13 <TextView
14 style="@style/Base.TextAppearance.AppCompat.Headline"
15 android:layout_width="fill_parent"
16 android:layout_height="wrap_content"
17 android:text="Title" />
18
19 <TextView
20 style="@style/Base.TextAppearance.AppCompat.Body1"
21 android:layout_width="fill_parent"
22 android:layout_height="wrap_content"
23 android:text="Content here" />
24
25 </LinearLayout>
26
27 </android.support.v7.widget.CardView>
1 <android.support.v7.widget.CardView
2 android:layout_width="fill_parent"
3 android:layout_height="wrap_content"
4 card_view:contentPadding="16dp"
5 card_view:cardElevation="2dp"
6 card_view:cardCornerRadius="5dp">
7
8 <LinearLayout
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:orientation="vertical">
12
13 <ImageView
14 android:layout_width="fill_parent"
15 android:layout_height="150dp"
16 android:src="@drawable/sample"
17 android:scaleType="fitXY"/>
18
19 <TextView
20 style="@style/Base.TextAppearance.AppCompat.Body1"
21 android:layout_width="fill_parent"
22 android:layout_height="wrap_content"
23 android:layout_marginTop="8dp"
24 android:text="Material Design is stunning!!!" />
25
26 </LinearLayout>
27
28 </android.support.v7.widget.CardView>
As you can see on above XML code, CardView provides some properties like
Demo result:
B – Re c y c le rV ie w
1 – W h a t i s R e c y c l e r V i e w?
RecyclerView is an “upgraded” version of ListView which is more advance and flexible.
RecyclerView is a container for displaying large data sets that can be scrolled efficiently
by maintaining a limited number of views.
RecyclerView also provides some default animations for common item operations like
inserting or removing an item.
2 – H o w t o u s e R e c y c l e r V i e w?
To be able to use RecyclerView in your application, you have to declare the required
dependencies like below:
XHTML
1 dependencies {
2 compile 'com.android.support:appcompat-v7:21.0.0'
3 compile 'com.android.support:recyclerview-v7:21.0.+'
4 }
– RecyclerView.Adapter: provides access to data sets items, creates views for items
and binds items info to item view.
2 .1 – L a y o u t M a n a g e r
RecyclerView provides some built-in LayoutManager that you can use immediately:
2 . 2 – R e c y c l e r V i e w. A d a p t e r
Adapter provides access to all the items in dataset.
Adapter also takes responsibility to create view for each item and replace new content for
the view whenever the original item is no longer visible.
3 – D e m o s im p le Re c y c le rV ie w
In this demo, we will create a simple demo to show you how to use RecyclerView in your
Android application. Actually, it’s quite similar to ListView implementation.
1 RecyclerView mRecyclerView;
2
3 @Override
4 protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.activity_recycler_view);
7 mRecyclerView = (RecyclerView)findViewById(R.id.recycler_view);
8 }
If you’re 100% sure that the item layout size will not change in future, call
setHasFixedSize() method with true value. This will help RecyclerView improve its
performance significantly.
Java
1 mRecyclerView.setHasFixedSize(true);
Next, create a LayoutManager and set it to the RecyclerView. Here, I use the built-in
LayoutManager – LinearLayoutManager this simple demo. In next post, I’ll show you how
to customize LayoutManager.
Java
1 mLayoutManager = new LinearLayoutManager(this);
2 mRecyclerView.setLayoutManager(mLayoutManager);
Before touching the Adapter, we need to create a layout for RecyclerView item:
– Constructor: where we get the dataset that can be passed from parameter or
created inside the constructor
After that, we need to create an instance of the Adapter and assign to RecyclerView:
Java
1 RecyclerView.Adapter mAdapter;
2 mAdapter = new SimpleAdapter();
3 mRecyclerView.setAdapter(mAdapter);
In this section, I’ll show you how to combine CardView and RecyclerView to create
stunning view displaying a dataset.
This demo will display a list of Movie info with name and thumbnail. So that we also need
to create an entity class for Movie:
Then, create CardAdapter class that similar to SimpleAdapter above. Instead only one
TextView, now the ViewHolder will have the ImageView to display the movie thumbnail as
well.
Java
1 mAdapter = new CardAdapter();
2 mRecyclerView.setAdapter(mAdapter);
Now you can run and enjoy the result when combine CardView and RecyclerView:
D – D o w n l o a d S o u rc e C o d e C a rd V i e w a n d R e c y c l e r V i e w i n
M a te r ia l D e s ig n
https://github.com/trinhlbk1991/DemoCardViewRecyclerView
http://www.mediafire.com/download/5s94lj10cyy4rj3
/DemoCardViewRecyclerView.zip
SShhaarree tthhiiss::
LLiikkee tthhiiss::
Like
Be the first to like this.
Related
Android, Java
9 t h o u g h t s o n “ [ A n d ro i d ] C a rd V i e w a n d R e c y c l e r V i e w i n
M a te r i a l D e s i g n”
Hi,
If you want to make any View clickable, just set the on click
listener for it. Example:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, “Button Clicked”,
Toast.LENGTH_SHORT).show();
}
});
If you want to make any View clickable, just set the on click
listener for it. Example:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, “Button Clicked”,
Toast.LENGTH_SHORT).show();
}
});
Subscribe
Ice Tea 09
173 likes
Archives
Select Month
Categories
Select Category
Friends
Tung Nguyen
Meta
Log in Entries R S S
Comments R S S WordPress.org