[Android] CardView in Android
The day before her had called us and instruct you to use RecyclerView in Android. This article will continue to guide you using CardView – View used a combination with RecyclerView or ListView.
Content
What is CardView
CardView allow you to display content in a more prominent, shadow effect, bo beautiful corner. And especially when combined with RecyclerView. If you have used android 4.0 or more often see the app has a list like this.
Use CardView in Eclipse
For those you do not use Android weak machine but must use Eclipse Studio
Step 1: Import CardView
Chọn menu File -> Import -> Existing Android code into workspace –> Browse (Go to sdk/extras/android/support/v7/cardview) –> Click ok –> Click Finish
Step 2: The definition is CardView 1 library.
Right click on the project Cardview –> Properties –> Android –> Enable isLibrary (tick the checkbox) –> Apply –> ok
Step 3: Use cardview in the main Project
Right click on your project –> Properties –> Android –> Add –> cardview –> apply –> ok
That's it.
Use CardView in Android Studio
Add the following to the file build.grade
compile 'com.android.support:cardview-v7:21.0.+'
That's it…
Code generated in xml CardView
Suppose we do more examples RecyclerView in Android as the day before and now we give each item is 1 cardview. (as the first photo post)
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_margin="5dp" card_view:cardCornerRadius="4dp" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="8dp" android:layout_weight="1" /> <ImageButton android:id="@+id/btn_delete" android:layout_width="32dp" android:layout_height="32dp" android:src="@drawable/ic_delete" /> </LinearLayout> </android.support.v7.widget.CardView>
In which you noticed card_view:cardCornerRadius =”4dp” Help us make the items have rounded corners 4dp.
Download the complete code RecyclerView_with_CardView
Hi Nguyenvanquan7826 ,
First its very thankful for your article on Android, because it is useful for you.
Nguyenvanquan7826, ask yourself why the add libraries to add by RecyclerView then put the .jar files ending in directory libs, even when add the import library for using project CardView cardview ??? (I used eclipse)
Of. Có thể add bằng jar như RecylerView nhưng ko hiểu sao mình làm bị lỗi nên phải làm vậy 🙂
Thanks Nguyenvanquan7826,
I also failed to add the 2 file .jar RecyclerView và CardView, but add in project when there were no errors CardView.
Em muốn bắt sự kiện click vào 1 cardview sẽ chuyển sang 1 activity khác mà làm mãi không được anh ạ, đây là file ItemAdapter và file Activity em viết, anh giúp em được không ạ, thank you
ItemAdapter: http://codepad.org/q62dofev
Activity: http://codepad.org/I2x2VFgz
Ở Adapter bạn setOnClick cho itemView nhé.