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



我正在使用一个自定义LinearLayout(扩展LinearLayout(,这样和Adapter(BaseAdapter(就可以连接到它上。

(感谢原作者。(

* @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属性周围播放时,更改会变得可见,这包括当在子对象外部触摸时LinearLayouts本身(而不是其子对象(的实际涟漪效果(我已经为这一点留出了一些空间(。

如果您想查看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类中的方法,但是哪一个?

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

无论如何。。。

问题不在于家长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>

最新更新