沿 TextView 宽度的波纹效果

Ripple Effect along the TextView width

我在回收商的视图项目中使用了涟漪效果,但效果没有扩大 到整个视图的宽度。例如,如果 textview 仅包含几个符号,则涟漪效应适用于此符号宽度,但不适用于所有项目宽度(项目宽度 = match_parent)

这是我的代码:

我的片段

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="2dp" />

RecyclerView_item

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_background">

<TextView
    android:id="@+id/item_question"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center_vertical"
    android:textSize="15sp"
    android:textStyle="bold" />
</FrameLayout>

item_background.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:drawable="@color/colorItem" />

我该如何解决这个问题?

有完全相同的问题。通过更改 RecyclerAdapter

中的这行代码解决了这个问题
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = View.inflate(context, R.layout.simple_list_item_1, parent, null);
        return new ViewHolder(view, viewType);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.simple_list_item_1, parent, false);
        return new ViewHolder(view, viewType);
    }

显然在不提供父级充气器的情况下充气会导致不同的 LayoutParams。

注:

View view = View.inflate(context, R.layout.simple_list_item_1, parent);

也不起作用,因为该项目还不应该添加到父级,否则会导致错误:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

祝你好运!