【发布时间】: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