使用两个Viewholder查看RecyclerView中的重叠

时间:2017-07-21 07:19:22

标签: android android-recyclerview android-viewholder

我在RecyclerView中使用了2个ViewHolders A和B,其中A位于每个第4位置。但问题是每隔4个位置,A重叠B. B的第4个视图隐藏在A下面。我怎样才能跳过B的每个第4个位置并在那里显示A.

这是我的代码。

public class A extends RecyclerView.ViewHolder {
......Code
}


public class B extends RecyclerView.ViewHolder {
......Code
}

@Override
public int getItemViewType(int position) {
        return (position % 4 == 3) ? A : B;
    }         
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == A) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.view_A, parent, false);
        return new A(view);

    } else {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.view_B, parent, false);
        return new B(v);
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

    if (holder.getItemViewType() == A) {
        .....Code
    } else {
        ......Code        
}
}

View_B.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoBt xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/videoLayout"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardBackgroundColor="@color/colorPrimary"
    app:cardElevation="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/videoListImage"
            android:layout_width="fill_parent"
            android:layout_height="210dp"
            android:scaleType="fitXY"/>

        <TextView
            android:id="@+id/videoHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorSecondary"
            android:textStyle="bold"
            android:padding="12dp"
            android:textSize="18sp"/>

    </LinearLayout>

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

View_A.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/adLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardBackgroundColor="@color/colorPrimary"
    app:cardElevation="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.facebook.ads.MediaView
            android:id="@+id/adMedia"
            android:layout_width="fill_parent"
            android:layout_height="210dp"
            android:scaleType="fitXY" />

        <ImageView
            android:id="@+id/adImage"
            android:layout_width="fill_parent"
            android:layout_height="210dp"
            android:scaleType="fitXY" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/adIcon"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8sp"
                android:layout_marginStart="8sp"
                android:layout_marginTop="8sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

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

                    <TextView
                        android:id="@+id/adHeading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="18dp"
                        android:layout_marginRight="8dp"
                        android:padding="4dp"
                        android:textColor="@color/colorSecondary"
                        android:textSize="16sp"
                        android:textStyle="bold" />

                    <ImageView
                        android:id="@+id/mAdChoice"
                        android:layout_width="14dp"
                        android:layout_height="14dp"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentRight="true"
                        android:layout_margin="3dp" />
                </RelativeLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/adSocialContext"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="4dp"
                        android:textColor="@color/colorSecondary"
                        android:textSize="12sp" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">

            <TextView
                android:id="@+id/adBody"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:ellipsize="end"
                android:maxLines="2"
                android:paddingBottom="10dp"
                android:paddingEnd="0dp"
                android:paddingLeft="10dp"
                android:paddingRight="0dp"
                android:paddingStart="10dp"
                android:textColor="@color/colorSecondary"
                android:textSize="13sp" />

            <StyledButton
                android:id="@+id/adCallToAction"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="0.6"
                android:background="?android:attr/selectableItemBackground"
                android:textColor="@color/colorSecondary" />

        </LinearLayout>

    </LinearLayout>

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

2 个答案:

答案 0 :(得分:1)

为viewA和viewB的父线性插图添加边距

答案 1 :(得分:0)

return (position % 4 == 3) ? A : B;替换为return (position+1) % 4 == 0 ? A: B;并检查

例如:

class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        int A = 1;
        int B = 2;
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType == A){
                return new HolderA(new TextView(parent.getContext()));
            }else {
                return new HolderB(new TextView(parent.getContext()));
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if(holder.getItemViewType() == A){
                ((HolderA) holder).textView.setText("A");
            }else{
                ((HolderB) holder).textView.setText("B");
            }
        }

        @Override
        public int getItemCount() {
            return 50;
        }

        @Override
        public int getItemViewType(int position) {
            return (position+1) % 4 == 0 ? A: B;
        }

        class HolderA extends RecyclerView.ViewHolder{
            TextView textView;

            public HolderA(View itemView) {
                super(itemView);
                textView = (TextView) itemView;
                textView.setBackgroundColor(Color.GREEN);
                textView.setPadding(10,10,10,10);
            }
        }

        class HolderB extends RecyclerView.ViewHolder{
            TextView textView;

            public HolderB(View itemView) {
                super(itemView);
                textView = (TextView) itemView;
                textView.setPadding(10,10,10,10);
            }
        }
    }