【问题标题】:Children from custom LinearLayout not displaying ripple effect来自自定义 LinearLayout 的子级不显示波纹效果
【发布时间】:2020-11-28 21:27:00
【问题描述】:

我正在使用自定义的 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() 方法动态添加每个孩子。 如您所见,我已经尝试在以编程方式添加的每个子视图上放置涟漪效果。

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

<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 的情况下工作(它的适配器是一个 PagerAdapter)。 我怎么知道它的 LinearLayout 混淆了孩子而不是 ViewPager 混淆了一切?因为当围绕 LinearLayout 属性进行操作时,更改变得可见,这包括在子级外部触摸时 LinearLayout 本身(而不是其子级)的实际涟漪效应(我已经为此提供了一些空间)。

如果您想查看 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"
            >

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

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

【问题讨论】:

    标签: java android android-layout android-linearlayout


    【解决方案1】:

    我似乎对视图重叠的顺序有一些错误的概念。 似乎顺序总是最后一个嵌套对象是其他所有对象之上的对象(否则我怎么会想到...),也许我对代码的编写方式感到困惑(在嵌套的方式)...

    无论如何...

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

    始终是子代混淆了父代。

    重复我最初的问题,我认为我的外部布局(自定义 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>
    

    【讨论】:

    • 非常好,这对我有用。但我的问题不同。我的问题是当我点击父视图子视图时显示涟漪效应,但我不希望这种情况发生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2015-06-08
    相关资源
    最近更新 更多