簡體   English   中英

ListView中的CardView

[英]CardView in the ListView

我正在嘗試通過使用列表中的CardView加載帶有圖像和文本的視圖。 但是,卡之間有一個分隔線。

這是我的輸出:

在此處輸入圖片說明


我正在嘗試做這個視頻 ,沒有分隔線。

這是我的XML代碼。

activity_cat_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ListView
        android:id="@+id/lvCat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

item_cat

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FEFEFE"
        android:layout_margin="8dp"
        app:cardCornerRadius="4dp"
        card_view:cardCornerRadius="4dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true"
        card_view:cardElevation="8dp">

        <!-- Main Content View -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/ivCat"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_alignParentTop="true"
                android:scaleType="centerCrop"/>

            <TextView
                android:id="@+id/tvCatName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ivCat"
                android:text="Name"
                android:maxLines="3"
                android:padding="8dp"
                android:textColor="#222"
                android:textStyle="bold"
                android:textSize="22dp" />

            <TextView
                android:id="@+id/tvDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tvCatName"
                android:text="Description"
                android:maxLines="3"
                android:padding="8dp"
                android:textColor="#666"
                android:textSize="14dp" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

編輯1:添加CatAdapter.java

public class CatAdapter extends ArrayAdapter<Cat> {
    public CatAdapter(Context context, ArrayList<Cat> cats) {
        super(context,0,cats);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //get data item from this position
        Cat cats = getItem(position);

        //check if existing view is being reused, otherwise inflate the views
        if (convertView == null)
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_cat, parent, false);

        //lookup view for data population
        TextView tvName = (TextView)convertView.findViewById(R.id.tvCatName);
        TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
        ImageView ivCat = (ImageView)convertView.findViewById(R.id.ivCat);

        //populate into template view from data source
        tvName.setText(cats.getName());
        tvDescription.setText(cats.getDescription());
        Glide.with(getContext()).load(cats.getImages()).into(ivCat);

        return convertView;
    }
}

您可以使用xml刪除分隔線:

<ListView
    android:id="@+id/lvCat"
    android:dividerHeight="0dp"
    android:divider="@null"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM