java - 来自自定义 LinearLayout 的子级不显示波纹效果
问题描述
我正在使用自定义的 LinearLayout(扩展 LinearLayout),以便可以将适配器(BaseAdapter)附加到它。
(感谢原作者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 正在相应地工作。
线性布局:
<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 类中的方法,但是哪一个呢?
解决方案
我似乎对视图重叠的顺序有一些错误的概念。似乎顺序总是最后一个嵌套对象是其他所有对象之上的对象(否则我怎么会想到它......),也许我对代码的编写方式感到困惑(在嵌套方式)...
无论如何...
问题不在于父 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>
推荐阅读
- clojure - 试图在 Clojure 中创建一个空集
- c++ - 一一搭建CGAL Mesh_criteria
- scala - 铸造问题
- python - ttk notebook 帧数过多导致故障
- javascript - 为什么我的客户没有听到套接字 io 事件?
- ajax - 为什么来自 Ajax w XMLHttpRequest 的数据没有到达指定的 div id
- python - 打开pyxl。忽略具有样式但没有值的单元格
- django - Django - 将对象添加到模型查询集中而不先保存
- java - Eclipse Java 以相反的顺序编写希伯来语单词
- html - 切换文本时未交换表情符号