【问题标题】:CardView in the ListViewListView 中的 CardView
【发布时间】:2017-01-18 17:51:11
【问题描述】:

我正在尝试使用列表中的 CardView 加载带有图像和文本的视图。但是,卡片之间存在分隔线。

这是我的输出:


我正在尝试像 video 那样做,没有分隔线。

这是我的 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;
    }
}

【问题讨论】:

  • 你能分享你设置适配器到这个列表视图的java代码吗?
  • @HarshitDwivedi 完成。
  • 设置分隔符属性为空,stackoverflow.com/questions/1914477/…
  • @PavneetSingh 我已经这样做了,还是不行。
  • @IhsanRamli 有时在 xml 中将分隔符设置为 null 不起作用。所以尝试以编程方式进行。 listView.setDivider(null);listView.setDividerHeight(0);

标签: java android listview android-cardview


【解决方案1】:

您可以使用 xml 删除分隔符:

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

【讨论】:

  • 它还在。
  • 我尝试了我给你的代码并且它正在工作。使用活动布局代码编辑您的帖子,以便我们检查。
  • 您可以将分隔线标记为透明,这也应该可以,但将其设置为 null 应该可以,并且应该是首选。
猜你喜欢
  • 2018-03-08
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
相关资源
最近更新 更多