我使用嵌套片段,用代码从一个片段传递到另一个片段
ImageView search = (ImageView) root.findViewById(R.id.search);
search.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.parent, ChildFragment.newInstance());
transaction.commit();
}
});但是,有时在子片段中,我仍然可以单击父片段的项,即使它们是不可见的(由子片段覆盖)。特别是,当子节点有一个简单的类型布局时,就会发生这种情况。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF0D1"
android:id="@+id/child">
...some stuff...
</RelativeLayout>但是,当我使用嵌套布局时,就不会发生这种情况。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF0D1"
android:id="@+id/child">
<ScrollView
android:id="@+id/scr"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:padding="10dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
...same stuff...
</RelativeLayout>
</ScrollView>
</RelativeLayout>有人知道为什么会这样吗?
发布于 2014-11-15 16:55:54
原因:如果不截取/处理子片段,单击子片段-它将传递给父片段。即使您处理对的某些元素的单击,它也不会改变整个情况。
最简单的解决方案:为了避免这种行为-声明最外层布局,您的子片段可点击如下:
<RelativeLayout
...
android:clickable="true"
...>
...在这种情况下,您甚至不必实现onClickListener
https://stackoverflow.com/questions/26948067
复制相似问题