自定义LinearLayout的子代不显示波纹效果

德拉克

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

(感谢ofc的原始作者。)

 * @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正在相应地工作。

LinearLayout:

<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类中的Method,但是哪一个呢?

德拉克

我似乎对视图重叠的顺序有一些误解。似乎顺序始终是最后一个嵌套对象是所有其他对象之上的对象(我怎么可能以为...),也许我对代码的编写方式(嵌套)感到困惑道路)...

无论如何...

问题不在于父级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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

自定义导航抽屉上的波纹效果

来自分类Dev

我的自定义按钮的Android波纹效果

来自分类Dev

有时不显示波纹效果

来自分类Dev

自定义菜单onOpen不显示

来自分类Dev

jqtree不显示自定义属性

来自分类Dev

自定义UICollectionViewCell不显示更改

来自分类Dev

jqtree不显示自定义属性

来自分类Dev

自定义UICollectionViewCell不显示更改

来自分类Dev

自定义菜单onOpen不显示

来自分类Dev

自定义ContentProvider建议不显示

来自分类Dev

自定义ValidationAttribute不显示错误

来自分类Dev

SearchView不显示自定义建议

来自分类Dev

自定义UITableViewCell-不显示

来自分类Dev

如何在Flutter中向ListTile添加自定义波纹效果颜色

来自分类Dev

自定义波纹效果(我想在我的内联样式中使用var)

来自分类Dev

RecyclerView onclick不显示波纹效果或突出显示

来自分类Dev

不显示波纹CSS

来自分类Dev

不显示角度自定义过滤器

来自分类Dev

自定义应用程序图标不显示

来自分类Dev

从TButton继承的自定义按钮不显示

来自分类Dev

Xcode ViewController不显示在自定义类中

来自分类Dev

自定义内容类型中的MediaLibraryPickerField不显示

来自分类Dev

SceneKit –自定义几何图形不显示

来自分类Dev

自定义ListView不显示任何内容

来自分类Dev

自定义适配器不显示数据

来自分类Dev

自定义字体在IOS中不显示(.otf类型)

来自分类Dev

自定义UIButton子类不显示标题

来自分类Dev

自定义布局完全不显示-Android

来自分类Dev

自定义UITableViewCell不显示重新排序控件