我正在使用自定义的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] 删除。
我来说两句