如何在recyclerView单元格的顶部添加边到边的覆盖?

时间:2018-03-06 20:12:02

标签: android android-recyclerview

以下是我的recyclelerview单元格的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
    android:id="@+id/root_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:paddingTop="3dp">

    <RelativeLayout
        android:id="@+id/top_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp">

        <com.company.voice.view.font.CustomFontTextView
            android:id="@+id/left_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:lines="1"
            android:textColor="@color/nameHintGrey"
            android:textSize="11sp" />

        <com.company.voice.view.font.CustomFontTextView
            android:id="@+id/right_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:lines="1"
            android:textColor="@color/nameHintGrey"
            android:textSize="11sp" />

    </RelativeLayout>

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

        <com.company.voice.view.custom.UserProfileView
            android:id="@+id/user_right_image"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <com.company.voice.view.font.CustomFontTextView
            android:id="@+id/message_right_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="23dp"
            android:layout_marginRight="23dp"
            android:layout_toLeftOf="@+id/user_right_image"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

    </RelativeLayout>

</RelativeLayout>

<View
    android:id="@+id/empty_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_below="@+id/root_container" />

<RelativeLayout
    android:id="@+id/multi_select_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/empty_view"
    android:background="@color/multiSelectChatBG"
    android:gravity="center"
    android:visibility="visible">

    <android.support.v7.widget.AppCompatImageView
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter"
        android:layout_centerInParent="true"
        app:srcCompat="@drawable/ic_selectchat" />

</RelativeLayout>

问题:我想将此绿色背景填充到回收站视图的整个单元格中。每个单元格的高度根据textview的行数而变化。我尝试过很多东西但都失败了。

以下是当前输出

Current Output

预期输出低于

Expected output

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

前景色很容易,也可以节省额外的容器透支水平 请按照以下步骤进行搜索

http://antoine-merle.com/blog/2013/07/17/adding-a-foreground-selector-to-a-view/


或我的测试版本 https://gist.github.com/parthdave93/1b1f6871053a575e0e19d2c17eaf4514


输出: enter image description here

答案 1 :(得分:0)

您可以在ViewHolder中获取root_container,然后在onBindViewHolder中更改它的背景:

@Override
public void onBindViewHolder(MainViewHolder holder, final int position) {
    holder.rootContainer.setBackgroundColor(Color.GREEN);
}

public class MainViewHolder extends RecyclerView.ViewHolder {
    private RelativeLayout rootContainer;

    public MainViewHolder(View view) {
        super(view);
        rootContainer = (RelativeLayout) view.findViewById(R.id.root_container);
    }
}

编辑:

您可以将视图放在布局的底部:

<View
    android:id="@+id/overlay_view"
    android:layout_width="match_parent"
    android:background="@color/transprentGreen"
    android:layout_height="match_parent"/>

然后在onBindViewHolder中更改它的大小:

@Override
public void onBindViewHolder(MainViewHolder holder, final int position) {
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.rootContainer.getLayoutParams();
    holder.overlayView.setLayoutParams(params);
}

public class MainViewHolder extends RecyclerView.ViewHolder {
    private RelativeLayout rootContainer;
    private View overlayView;

    public MainViewHolder(View view) {
        super(view);
        rootContainer = (RelativeLayout) view.findViewById(R.id.root_container);
        overlayView = (View) view.findViewById(R.id.overlay_view);
    }
}