Dmi*_*sev 257 android android-fragments
当我#77000000在另一个片段(我们称之为主片段)上显示一个片段(全屏幕为背景)时,我的主片段仍然对点击作出反应(即使我们没有看到它,我们也可以点击一个按钮).
问题:如何防止点击第一(主)片段?
编辑
不幸的是,我不能只隐藏主片段,因为我在第二个片段上使用透明背景(因此,用户可以看到后面的内容).
Jur*_*jak 552
将clickable第二个片段视图的属性设置为true.视图将捕获事件,以便它不会传递给主片段.因此,如果第二个片段的视图是布局,那么这将是代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
Run Code Online (Sandbox Code Playgroud)
Dmi*_*sev 69
解决方案非常简单.在我们的第二个片段(与我们的主片段重叠)中,我们只需要捕获onTouch事件:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstance){
View root = somehowCreateView();
/*here is an implementation*/
root.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
return root;
}
Run Code Online (Sandbox Code Playgroud)
Mir*_*zal 14
只需添加clickable="true"和focusable="true"母公司布局
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<!--Your views-->
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
如果在同一容器视图中放置了两个片段,则在显示第二个片段时应隐藏第一个片段。
如果您想了解有关如何解决Fragment问题的更多问题,可以查看我的库:https : //github.com/JustKiddingBaby/FragmentRigger
FirstFragment firstfragment;
SecondFragment secondFragment;
FragmentManager fm;
FragmentTransaction ft=fm.beginTransaction();
ft.hide(firstfragment);
ft.show(secondFragment);
ft.commit();
Run Code Online (Sandbox Code Playgroud)
您需要添加 android:focusable="true"与android:clickable="true"
Clickable 意味着它可以被指针设备点击或被触摸设备点击。
Focusable意味着它可以从键盘等输入设备获得焦点。键盘等输入设备无法根据输入本身决定将其输入事件发送到哪个视图,因此它们将它们发送到具有焦点的视图。
| 归档时间: |
|
| 查看次数: |
75766 次 |
| 最近记录: |