Children 来自自定义 LinearLayout 不显示波纹效果

Children from custom LinearLayout not displaying ripple effect

我使用的是自定义 LinearLayout(扩展 LinearLayout),因此适配器 (BaseAdapter) 可以附加到它。

(感谢c的原作者)

 * @author Vincent Mimoun-Prat @ MarvinLabs
 */
public class MyLinearLayout extends LinearLayout {
    private static final String TAG = "MyLinearLayout";
    private Adapter adapter;

....

private void reloadChildViews() {
    removeAllViews();

    if (adapter == null) return;

    int count = adapter.getCount();
    for (int position = 0; position < count; ++position) {
        View v = adapter.getView(position, null, this);
        if (v != null) {
            int[] attrs =new int[]{R.attr.selectableItemBackground};

            TypedArray typedArray =getContext().obtainStyledAttributes(attrs);

            int backgroundResource =typedArray.getResourceId(0, 0);

            v.setBackgroundResource(backgroundResource);

            typedArray.recycle();

            addView(v);
        }

    }

    requestLayout();
}

}

所以我基本上是通过 addView() 方法动态添加每个 child。 如您所见,我已经尝试对每个以编程方式添加的 child 视图施加涟漪效应。

添加到此 ViewGroup 的 child 视图具有相应的属性:

<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <data>
    <variable
        name="contentProduct"
        type="com.example.flyhigh.data.pojos_entities.content.variations.ContentProduct" />
    <variable
        name="touchListener"
        type="android.view.View.OnTouchListener"/>
    </data>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context="com.example.flyhigh.ui.DailyJournalFragment"
        android:clickable="true"
        android:focusable="true"
        setOnTouchListener="@{touchListener}"
        android:background="?android:attr/selectableItemBackground"
        >
        <TextView

.....
        />
</LinearLayout>
</layout>

绑定的 touchListener 正在相应地工作。

线性布局:

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.flyhigh.adapters.PhaseTypePagerAdapter2"
>
<com.example.flyhigh.custom_artifacts.MyLinearLayout
    android:id="@+id/myLinearLayout"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    app:layout_constraintTop_toTopOf="parent"

    android:orientation="vertical"
    android:background="?selectableItemBackground" //I've tried with this on and off
    android:clickable="true" //I've tried with this on and off
    android:focusable="true" //I've tried with this on and off


    android:divider="@drawable/divider"
    android:dividerPadding="10dp"
    android:showDividers="middle"

    />

</layout>

此 LinearLayout 在 ViewPager 中。 这个 ViewPager 在没有 FragmentManager 的情况下工作(它的 Adapter 是一个 PagerAdapter)。 我怎么知道是 LinearLayout 混淆了 children 而不是 ViewPager 混淆了一切?因为当玩弄 LinearLayout 属性时,变化变得可见,这包括在 children 之外触摸时 LinearLayout 本身(不是它的 children)的实际涟漪效应(我给了一些空间为了这成为可能)。

如果您想查看 ViewPager:

        <com.example.flyhigh.custom_artifacts.MyViewPager
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            >

我计划为每个 children 添加拖放功能,但我担心这种症状也会影响到该功能。

我 100% 确定解决方案依赖于覆盖 MyLayout class 中的方法,但哪个方法?

我似乎对视图重叠的顺序有一些概念错误。 似乎顺序总是最后嵌套的 object 是最重要的(我怎么会这么认为......),也许我对代码的编写方式感到困惑(以嵌套方式)...

总之...

问题不在于 parent ViewGroup 混淆了它更内在的 children,而是反过来:

始终是 children 混淆了它的 parent。

为了重复我最初的问题,我认为我的外部布局(自定义 LinearLayout)阻止了其列表项(也是 ViewGroup)的连锁反应。

所以,解决方案,不使用似乎流行的android:foreground=,是下一个:

ViewGroup 中的这个你想要用波纹效果突出显示(我的列表项):

<LinearLayout
    ...
    android:clickable="true"
    android:focusable="true"
    android:background="?android:attr/selectableItemBackground"
    android:addStatesFromChildren="true" // this is the important one, but without the other attrs is useless
    >

//obfuscating children should have the according:
    <TextView
        ...
        android:clickable="true"
        android:focusable="true"
        ...
    />

    <TextView
        ...
        android:clickable="true"
        android:focusable="true"
       ...
    />

    <TextView
        ...
        android:clickable="true"
        android:focusable="true"
       ...
    />

    Etc...etc...etc...
    </LinearLayout>