Android 在另一个片段上添加一个片段 onClickListener 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24764980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a fragment on top of another fragment onClickListener issue
提问by Archer2486
I'm addinga fragment to an activity instead of replacingthe current fragment (because this corresponds to the type of behavior I want to have).
我正在向活动添加片段而不是替换当前片段(因为这对应于我想要的行为类型)。
My problem is that clicking in a spot on the top fragment (the one that's currently visible), where a view in the non-visible fragment is located, causes an onClick event on the view in the second, non-visible fragment, to fire. Why is this happening and how can I prevent this?
我的问题是单击顶部片段(当前可见的片段)上的一个点,即不可见片段中的视图所在的位置,会导致第二个不可见片段中的视图上的 onClick 事件触发. 为什么会发生这种情况,我该如何预防?
This is the code I use to first add the ListView fragment to the activity:
这是我用来首先将 ListView 片段添加到活动的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState == null) {
listFragment = new ListFragment ();
getSupportFragmentManager().beginTransaction()
.add(R.id.frame_container, listFragment)
.addToBackStack(listFragment .TAG)
.commit();
}
...
}
In this same activity I'm adding the second fragment, on top of the list fragment:
在同一个活动中,我在列表片段的顶部添加了第二个片段:
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
...
createItemFragment = new CreateItemFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.frame_container, createItemFragment)
.addToBackStack(createItemFragment.TAG)
.commit();
...
}
回答by C0D3LIC1OU5
You can just add the following attribute to the XML root layout of the fragment that agoes on top-
您只需将以下属性添加到顶部的片段的 XML 根布局中 -
android:clickable="true"
This will ensure that touch events will not propagate further than the top layer.
这将确保触摸事件不会比顶层传播得更远。
回答by Gabe
If you're adding a fragment it'll overlap all the fragments under it So if you want to display both views at the same time this is the way to do it. Naturally with both view present both will listen for touch events. If you want to preserve the fragment but not show it use:
如果您要添加一个片段,它将与它下面的所有片段重叠所以如果您想同时显示两个视图,这就是这样做的方法。自然地,在两个视图都存在的情况下,两个视图都会监听触摸事件。如果您想保留片段但不显示它,请使用:
FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.detach(fragment).commit();
That will remove the fragments view without destroying the fragment.
这将删除片段视图而不破坏片段。
you can call
你可以打电话
FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.attach(fragment).commit();
later to reattach it to the view.
稍后将其重新附加到视图。
alternatively you could just change your on click listener to
或者,您可以将点击监听器更改为
public void onClick (View v){
if(!v.isShown()){
return;
}
//Stuff the listener should do.
}
回答by Michael Mao
well,I think the best way is set onClickListener to the locateView which you clickon the top-fragment .
if you use the way android:clickable="true" answered by The Metal Beard, and also you set any ImageView's selectorstates (see below), then you will find the bug : if you click the other space place ,the imageView will changethe status.
好吧,我认为最好的方法是将 onClickListener 设置为您单击top-fragment的 locateView。
如果您使用The Metal Beard 回答的 android:clickable="true" 方式,并且您设置了任何 ImageView 的选择器状态(见下文),那么您会发现错误:如果您单击其他空间位置,则 imageView 会改变状态。
<ImageView
android:id="@+id/mCollect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/specialprice_collect_selector" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/specialprice_collect_p" android:state_pressed="true"/>
<item android:drawable="@drawable/specialprice_collect_n"/>
</selector>
if the clicked View on top-fragment is mBootomLayout,just do it like this:
如果 top-fragment 上点击的 View 是 mBootomLayout,就这样做:
mBootomLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});