【问题标题】:Ripple Effect along the TextView width沿 TextView 宽度的波纹效果
【发布时间】:2016-02-14 22:42:39
【问题描述】:

我在回收者的视图项中使用了涟漪效果,但效果没有扩大 到视图的整个宽度。例如,如果 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" />

我该如何解决这个问题?

【问题讨论】:

    标签: java android xml android-recyclerview ripple


    【解决方案1】:

    遇到了完全相同的问题。通过更改 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.

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2016-05-24
      • 1970-01-01
      • 2017-05-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多