Cardview对物品没有任何影响

时间:2019-09-07 09:55:03

标签: android android-recyclerview android-cardview cardview

我试图让RecyclerView更漂亮,所以我添加了CardView。但是,当我运行该应用程序时,我看不到任何更改,用户界面看起来好像根本没有CardView-项目之间没有分隔。您能在这里提出我的代码有什么问题吗?

这是我的list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="6dp">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/link_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:padding="1dp"
            android:layout_marginBottom="10dp"
            android:layout_marginStart="10dp"
            android:text="@string/title_place_holder"/>

        <LinearLayout
            android:layout_below="@id/link_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/link_img"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                android:layout_gravity="center"
                app:srcCompat="@drawable/ic_launcher_background"/>

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/link_summary"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="15sp"
                android:padding="1dp"
                android:text="@string/summary_place_holder"/>

        </LinearLayout>

    </RelativeLayout>

</android.support.v7.widget.CardView>

这是我初始化RecylerView的方式:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);

        mAdapter = new PostsAdapter(mPostList);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);

        preparePostData();
    }

5 个答案:

答案 0 :(得分:1)

您需要SELECT .... FROM `analytics_152347283.events_20190902` 中的物品装饰,物品周围的间距相等!

注意:请确保您的RecyclerView背景与项目背景不同

EqualSpacingItemDecoration.java

RecyclerView

用法类似:

import android.graphics.Canvas;
    import android.graphics.Rect;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;


public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration {
 private final int top;
 private final int left;
 private final int bottom;
 private final int right;
 private int displayMode;

 public static final int HORIZONTAL = 0;
 public static final int VERTICAL = 1;
 public static final int GRID = 2;

 public EqualSpacingItemDecoration(int top, int left, int bottom, int right) {
      this(top, left, bottom, right, -1);
 }

 public EqualSpacingItemDecoration(int top, int left, int bottom, int right, int displayMode) {
      this.top = top;
      this.left = left;
      this.bottom = bottom;
      this.right = right;
      this.displayMode = displayMode;
 }

 @Override
 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
      int position = parent.getChildViewHolder(view).getAdapterPosition();
      int itemCount = state.getItemCount();
      RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
      setSpacingForDirection(outRect, layoutManager, position, itemCount);
 }

 private void setSpacingForDirection(Rect outRect,
                                     RecyclerView.LayoutManager layoutManager,
                                     int position,
                                     int itemCount) {

      // Resolve display mode automatically
      if (displayMode == -1) {
           displayMode = resolveDisplayMode(layoutManager);
      }

      switch (displayMode) {
           case HORIZONTAL:
                outRect.left = left;
                outRect.right = position == itemCount - 1 ? right : 0;
                outRect.top = top;
                outRect.bottom = bottom;
                break;
           case VERTICAL:
                outRect.left = left;
                outRect.right = right;
                outRect.top = top;
                outRect.bottom = position == itemCount - 1 ? bottom : 0;
                break;
           case GRID:
                if (layoutManager instanceof GridLayoutManager) {
                     GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                     int cols = gridLayoutManager.getSpanCount();
                     int rows = itemCount / cols;

                     outRect.left = left;
                     outRect.right = position % cols == cols - 1 ? right : 0;
                     outRect.top = top;
                     outRect.bottom = position / cols == rows - 1 ? bottom : 0;
                }
                break;
      }
 }

 private int resolveDisplayMode(RecyclerView.LayoutManager layoutManager) {
      if (layoutManager instanceof GridLayoutManager) return GRID;
      if (layoutManager.canScrollHorizontally()) return HORIZONTAL;
      return VERTICAL;
 }

 @Override
 public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
      super.onDraw(c, parent, state);
 }
}

答案 1 :(得分:1)

这可以通过对CardView元素应用margin的xml中进行1行更改来实现。

android:layout_margin"@dimen/default_margin"

使用您的代码:

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="6dp"
    android:layout_margin"@dimen/default_margin">

并在dimens资源文件中

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <dimen name="default_margin">10dp</dimen>
</resources>

答案 2 :(得分:1)

我对您的代码进行了更改,请尝试以下代码

位于

card_view:cardCornerRadius="4dp"
card_view:cardElevation="6dp"

我已更改为以下

app:cardCornerRadius="4dp"
app:cardElevation="6dp"
app:cardUseCompatPadding="true"

只需替换下面的布局代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com /apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardCornerRadius="4dp"
app:cardElevation="6dp"
app:cardUseCompatPadding="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/link_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:padding="1dp"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="10dp"
        android:text="@string/title_place_holder"/>

    <LinearLayout
        android:layout_below="@id/link_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/link_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_gravity="center"
            app:srcCompat="@drawable/ic_launcher_background"/>

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/link_summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="15sp"
            android:padding="1dp"
            android:text="@string/summary_place_holder"/>

    </LinearLayout>

</RelativeLayout>

</android.support.v7.widget.CardView>

答案 3 :(得分:1)

只需在CardView标签中使用app:cardUseCompatPadding="true"

答案 4 :(得分:0)

在清单应用标签中尝试。

android:hardwareAccelerated="true"