Jackky777
Jackky777

Reputation: 654

Ripple Effect along the TextView width

I use a ripple effect in the recycler`s view item ,but effect does not expand to the whole width of the view. For example if textview contains only few symbols, ripple effect apply for this symbols width , but not for all item width( item width = match_parent)

Here`s my code:

MyFragment

<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" />

How can I fix this?

Upvotes: 1

Views: 188

Answers (1)

mDroidd
mDroidd

Reputation: 1233

Had the exact same issue. Solved it by changing this lines of code in your 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);
    }

to

    @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);
    }

Apparently inflating without providing the inflater a parent causes different LayoutParams.

Note:

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

also does not work because the item should not yet be added to the parent, otherwhise causing an error: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Good luck!

Upvotes: 1

Related Questions