CardView和RecyclerView分行为 [英] CardView and RecyclerView divider behaviour

查看:287
本文介绍了CardView和RecyclerView分行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 RecyclerView CardView 作为列表的单个项目。结果
我想要实现水平列表,用的LayoutManager 这是很容易的。结果
于是我开始实现它。似乎一切正常,但并非如我所料这是使用CardView作为列表项的结果。

I am trying to use RecyclerView with CardView as an single item of the list.
I want to implement horizontal list, with LayoutManager it is really easy.
So I started to implement it. It seems that everything works, but not as I expected here is the result using CardView as an list item.

看起来pretty不错,但我并没有设置任何垫,分频器和圆角。结果
这是我对XML卡片视图项目。

Looks pretty good, but I haven't set any paddings, dividers and rounded corners.
Here is my XML for card view item.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:gravity="center"
       >
        <ImageView
            android:id="@+id/image_view"
            android:src="@drawable/androidsss"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            />
        <TextView
            android:id="@+id/info_text"
            android:layout_below="@+id/image_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="@android:color/black"
            android:textSize="14sp" />
    </RelativeLayout>

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

也有类似的布局底部的小分。结果
我还没有指定的任何。结果
这里是我的设置code为RecyclerView

Also there is something like small divider at the bottom of layout.
I haven't specified any.
Here is my setting up code for RecyclerView

 mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    mRecyclerView.setLayoutManager(mLayoutManager);
 //   mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
    mAdapter = new CardViewDataAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);

这与简单的项目,不卡视角,一切正常有趣的事情。下面是一个例子。

Interesting thing that with simple item, not card view, everything works as expected. Here is an example.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="150dp"
    android:layout_height="100dp"
    android:background="@android:color/white"
    >

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        />
    <TextView
        android:id="@+id/item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/item_image"
        android:text="Hello World !!"
        android:layout_centerInParent="true"
        android:textColor="@android:color/black"
        />
</RelativeLayout>

问题出在哪里,难道是我的错还是错误?

Where is the problem, is it my fault or bug ?

推荐答案

你可以看到有 COMPAT垫并他们只是在pre-棒棒糖设备可见(或者如果到处都是的空间您设置 card_view:cardUseCompatPadding =真正的)。你可以找到价值这里(CardView的doc)。

The spaces you can see are compat paddings and they're visible only on pre-Lollipop devices (or everywhere if you set card_view:cardUseCompatPadding="true"). You can find values here (CardView's doc).

前l,CardView添加填充其内容,并绘制阴影该区域。这个填充量等于maxCardElevation 1 +(1 - cos45)* cornerRadius侧面和maxCardElevation * 1.5 +(1 - cos45)* cornerRadius顶部和底部

Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.

此外,据我所看到的,是 CardView 的角落里一些默认的半径(可能是2DP)。

Also, as far as I can see, there is some default radius for CardView's corner (probably it is 2dp).

您可以尝试任何技巧,以避免这些空格或(恕我直言,这是更好)认为,如果你应该与自定义背景的,喜欢简单平铺的视图。
这里你有从谷歌关于设计的一些信息。

You can try any tricks to avoid these spaces or (IMHO it's better) consider if you should make view with custom background, like simple "tile". Here you have some info from Google about design.

这篇关于CardView和RecyclerView分行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆