首页 文章

回收商视图Android中的商品之间的保证金

提问于
浏览
37

嗨,我正在按照本教程

http://www.journaldev.com/10024/android-recyclerview-and-cardview-example-tutorial

现在我面临一个奇怪的问题,回收者视图内的每个cardview项目之间的差距太大了 .

ISSUE

如何减少放置在回收站视图内的每个cardview项目之间的保证金 .

enter image description here

9 回答

  • 0

    我遇到了类似的问题,RelativeLayout是recyclerview中每行的根元素 .

    要解决此问题,请找到包含每一行的xml文件,并确保根元素的高度为 wrap_content NOT match_parent .

  • 24

    我上了一堂课来管理这个问题 . 此类为recyclerView中的项设置不同的边距:只有第一行具有上边距,只有第一列具有左边距 .

    public class RecyclerViewMargin extends RecyclerView.ItemDecoration {
    private final int columns;
    private int margin;
    
    /**
     * constructor
     * @param margin desirable margin size in px between the views in the recyclerView
     * @param columns number of columns of the RecyclerView
     */
    public RecyclerViewMargin(@IntRange(from=0)int margin ,@IntRange(from=0) int columns ) {
        this.margin = margin;
        this.columns=columns;
    
    }
    
    /**
     * Set different margins for the items inside the recyclerView: no top margin for the first row
     * and no left margin for the first column.
     */
    @Override
    public void getItemOffsets(Rect outRect, View view,
                               RecyclerView parent, RecyclerView.State state) {
    
        int position = parent.getChildLayoutPosition(view);
        //set right margin to all
        outRect.right = margin;
        //set bottom margin to all
        outRect.bottom = margin;
        //we only add top margin to the first row
        if (position <columns) {
            outRect.top = margin;
        }
        //add left margin only to the first column
        if(position%columns==0){
            outRect.left = margin;
        }
    }
    

    }

    您可以通过这种方式在Recyclerview中进行设置

    RecyclerViewMargin decoration = new RecyclerViewMargin(itemMargin, numColumns);
     recyclerView.addItemDecoration(decoration);
    
  • 78
    • cards_layout.xml 中找到属性 card_view:cardUseCompatPadding="true" 并将其删除 . 启动应用程序,您会发现每个cardview项目之间没有边距 .

    • 添加您喜欢的边距属性 . 例如:

    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    
  • 0

    不使用XML在RecyclerView中的项目之间添加边距,而是使用由android框架提供的RecyclerView.ItemDecoration的更好方法 .

    所以,我创建了一个库来解决这个问题 .

    https://github.com/TheKhaeng/recycler-view-margin-decoration

    enter image description here

  • 0

    在Recyclerview项目布局中使用CardView像这样:

    <android.support.v7.widget.CardView
            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"
            xmlns:card_view="http://schemas.android.com/tools"
            card_view:cardCornerRadius="10dp"
            app:cardBackgroundColor="#ACACAC"
            card_view:cardElevation="5dp"
            app:contentPadding="10dp"
            card_view:cardUseCompatPadding="true">
    
  • 31

    在CardView项目中使用CompatPadding

  • 2
  • 2

    将Recycler视图 match_parent 更改为 wrap_content

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    还要更改项目布局xml

    将父布局高度 match_parent 设为 wrap_content

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    
  • 2

    如果您想在 XML 中执行此操作,请将 paddingToppaddingLeft 设置为 RecyclerView ,将 layoutMarginBottomlayoutMarginRight 等量设置为您膨胀到 RecyclerView 的项目(反之亦然) .

相关问题