11

我想像这张图一样开发List

在此处输入图像描述

我曾经使用 RecylerView ItemDecorator 进行重叠。但它是重叠的,没有阴影。屏幕和装饰器代码如下

在此处输入图像描述

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>
4

3 回答 3

7

在此处输入图像描述试试这个代码

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>
于 2016-11-15T06:54:20.153 回答
4

尝试这个。

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

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

在此处查看输出:

注意:取决于您的代码修改上面的代码并在正确的地方使用它。

这里是我的示例适配器代码供参考。

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);
                }
            }
        }
    }
于 2016-11-21T11:42:23.037 回答
4

这是静态问题解决方案的示例CardView,在这里您每次减少card_view:cardElevation2dp 或 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>

希望它会帮助你。谢谢。

于 2016-11-17T06:57:52.350 回答