dependencies { implementation 'com.android.support:cardview-v7:27.1.1' |
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" app:cardCornerRadius="10dp" app:cardElevation="100dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/itemImage" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:src="@drawable/tour1" /> <TextView android:id="@+id/item_txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Du lịch Phong Nha" android:textStyle="bold" /> <TextView android:id="@+id/item_txtDesc" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Dãy núi hùng vĩ. Khung cảnh thiên nhiên vượt trội. Người dân tộc làm đầy không khí với các truyền thống khác nhau của họ và trang phục đầy màu sắc." /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Đặt ngay" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Thích" /> LinearLayout> LinearLayout> android.support.v7.widget.CardView> LinearLayout> |
import android.content.Context; import android.media.Image; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class DemoCardViewActivity extends AppCompatActivity { // Mảng các tour du lịch ArrayList private void fakeData() { mLstTour.add(new Tour(R.drawable.tour1, "Du lịch Cao Bằng")); mLstTour.add(new Tour(R.drawable.tour2, "Khám phá hang Phong Nha")); mLstTour.add(new Tour(R.drawable.tour3, "Hành trình đất tổ")); mLstTour.add(new Tour(R.drawable.tour4, "Mùa vàng cao nguyên")); mLstTour.add(new Tour(R.drawable.tour5, "Maldiver Việt Nam")); mLstTour.add(new Tour(R.drawable.tour2, "Biển gọi tên em")); mLstTour.add(new Tour(R.drawable.tour4, "Khám phá miền Trung")); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo_card_view); fakeData(); ListView lstView = (ListView) findViewById(R.id.listview); AdapterTour adapter = new AdapterTour(this, R.layout.item_tour, mLstTour); lstView.setAdapter(adapter); } /** * Lớp Tour du lịch * Lớp Tour du lịch */ private class Tour { int imageRes; String infor; public Tour(int imageRes, String infor) { this.imageRes = imageRes; this.infor = infor; } } private class AdapterTour extends ArrayAdapter Context mContext; ArrayList int mRes; public AdapterTour(@NonNull Context context, int resource, ArrayList super(context, resource, mList); this.mContext = context; this.mList = mList; this.mRes = resource; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { View row = convertView; if (row == null) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(mRes, null); } ImageView image = (ImageView) row.findViewById(R.id.itemImage); TextView title = (TextView) row.findViewById(R.id.item_txtTitle); image.setImageResource(mList.get(position).imageRes); title.setText(mList.get(position).infor); return row; } } } |