RecyclerView重叠没有阴影

时间:2016-11-10 04:46:52

标签: android android-recyclerview android-cardview cardview

我想像这张图片一样开发List

enter image description here

我曾经习惯RecylerView ItemDecorator重叠。但它没有阴影就重叠了。屏幕&装饰器代码在下面

enter image description here

public class OverlapDecoration extends RecyclerView.ItemDecoration {

private final static int vertOverlap = -50;

@Override
public void getItemOffsets (Rect outRect, View view, RecyclerView parent,       RecyclerView.State state) {

outRect.set(0, 0, 0, vertOverlap);
}
}

card_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="cards main container">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="@color/color_white"
    card_view:cardElevation="5dp"
    card_view:cardUseCompatPadding="true">

            <TextView
                android:id="@+id/textViewName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="30dp"
                android:layout_marginBottom="30dp"
                android:text="Android Name"
                android:textAppearance="?android:attr/textAppearanceLarge"/>

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

</LinearLayout>

3 个答案:

答案 0 :(得分:7)

enter image description here尝试此代码

card_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:tag="cards main container">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:clipToPadding="false"
        android:elevation="0dp"
        card_view:cardUseCompatPadding="false"
        card_view:paddingEnd="0dp"
        card_view:paddingStart="0dp">

        <TextView
            android:id="@+id/textViewName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="30dp"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:text="Android Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_above="@id/textViewName"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/card_shadow" />
    </android.support.v7.widget.CardView>


</RelativeLayout>

用于卡片阴影 card_shadow

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="90"
        android:endColor="#77111111"
        android:startColor="@android:color/transparent" />
</shape>

答案 1 :(得分:4)

这是静态CardView问题解决方案的示例,您每次都会将card_view:cardElevation减少2dp或5dp。但是,如果您使用recyclerview执行此操作并想要动态CardView,那么您必须动态地在RecyclerView Adapter中添加高程。随着其地位的提高而减少。设置卡片抬高holder.cardView.setCardElevation()

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:tag="cards main container">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="20dp"
    card_view:cardUseCompatPadding="false">

    <TextView
        android:id="@+id/textViewName1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:text="Android Name"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

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

<android.support.v7.widget.CardView
    android:id="@+id/card_view2"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="18dp"
    card_view:cardUseCompatPadding="false">

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:text="Android Name"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

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

<android.support.v7.widget.CardView
    android:id="@+id/card_view3"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardUseCompatPadding="false">

    <TextView
        android:id="@+id/textViewName3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:text="Android Name"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

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

</LinearLayout>

希望它会对你有所帮助。感谢。

答案 2 :(得分:4)

试试这个。

在RecyclerView Adapter- on OnBindViewHolder方法中添加以下代码:

  CardView cardView = ((MyViewHolder) holder).cardView;
  float elevation = cardView.getCardElevation();
  float value = elevation-Float.parseFloat((position*0.5)+"");
  cardView.setCardElevation(value);

请参阅输出here

注意:取决于您的代码修改上述代码并在正确的位置使用它。

此处参考我的示例适配器代码。

public class CustomAdapter extends RecyclerView.Adapter {
        public static final String Name = "Name";
        public static final String Author = "Author";
        public static final String Description = "Description";
        private List<Book> dataSet;
        private Context context;


        public CustomAdapter(List<Book> data, Context context) {
            this.dataSet = data;
            this.context = context;
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
           ImageView profile;
            TextView name, author,description;
            CardView cardView;

            public MyViewHolder(View itemView) {
                super(itemView);
                profile = (ImageView) itemView.findViewById(R.id.profile_image);
                name = (TextView) itemView.findViewById(R.id.name);
                author = (TextView) itemView.findViewById(R.id.author);
                description = (TextView) itemView.findViewById(R.id.description);
                cardView = (CardView) itemView.findViewById(R.id.cardview);
            }
        }

        @Override
        public int getItemCount() {
            return dataSet.size();
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            RecyclerView.ViewHolder vh;
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.book_layout_sample, parent, false);

            vh = new MyViewHolder(v);
            return vh;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof MyViewHolder) {
                Book book = dataSet.get(position);
                ((MyViewHolder) holder).profile.setImageDrawable(getResources().getDrawable(R.drawable.blank_user,this.context.getTheme()));
                ((MyViewHolder) holder).name.setText(book.title);
                ((MyViewHolder) holder).author.setText(book.author);
                ((MyViewHolder) holder).description.setText(book.description);
                CardView cardView = ((MyViewHolder) holder).cardView;
                float ele = cardView.getCardElevation();
                float val = ele-Float.parseFloat((position*0.5)+"");
                cardView.setCardElevation(val);

                if(position==0)
                {
                    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)cardView.getLayoutParams();
                    params.setMargins(0,0,0,0);
                    cardView.setLayoutParams(params);
                }
            }
        }
    }