首页 文章

破坏另一个片段问题

提问于
浏览
235

当我在另一个片段上显示一个片段(带有 #77000000 背景的全屏幕)时(让's call it main), my main fragment still reacts to clicks (we can click a button even if we don't看到它) .

Question :如何防止点击第一(主)片段?

EDIT

不幸的是,我不能只隐藏主片段,因为我在第二个片段上使用透明背景(因此,用户可以看到后面的内容) .

10 回答

  • 1

    在第二个片段's view to true. The view will catch the event so that it will not be passed to the main fragment. So if the second fragment'上设置 clickable 属性视图是一个布局,这将是代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />
    
  • -1

    解决方案非常简单 . 在我们的第二个片段(与我们的主片段重叠)中,我们只需要捕获 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;
    }
    
  • 520

    只需将 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>
    
  • 0

    You should hide the first fragment when you are showing the second Fragment if two fragments is placed in same container view.

    如果您想了解更多有关如何解决Fragment问题的问题,可以查看我的库:https://github.com/JustKiddingBaby/FragmentRigger

    FirstFragment firstfragment;
    SecondFragment secondFragment;
    FragmentManager fm;
    FragmentTransaction ft=fm.beginTransaction();
    ft.hide(firstfragment);
    ft.show(secondFragment);
    ft.commit();
    
  • 0

    你需要添加 android:focusable="true"android:clickable="true"

    Clickable 表示它可以被指针设备点击或被触摸设备点击 .

    Focusable 表示它可以从键盘等输入设备获得焦点 . 键盘等输入设备无法根据输入本身决定将输入事件发送到哪个视图,因此将它们发送到具有焦点的视图 .

  • 0

    这听起来像DialogFragment的情况 . 否则,使用Fragment Manager提交一个隐藏,另一个显示 . 这对我有用 .

  • 0

    添加 android:clickable="true" 并没有't work for me. This solution not works on CoordinatorLayout when it'是父布局 . 's why I'已将RelativeLayout作为父布局,将 android:clickable="true" 添加到它并将CoordinatorLayout放在此RelativeLayout上 .

  • 7

    可接受的答案将“起作用”,但也会导致性能成本(过度绘制,重新测量方向变化),因为底部的片段仍在绘制中 . 也许您只需按标签或ID查找片段,并在需要再次显示时将可见性设置为GONE或VISIBLE .

    在Kotlin:

    fragmentManager.findFragmentByTag(BottomFragment.TAG).view.visibility = GONE
    

    使用动画时,此解决方案优于 hide()hide()show() 方法 . 你只需要 onTransitionStart()Transition.TransitionListenerTransition.TransitionListener 来调用它 .

  • 4

    我有多个具有相同xml的片段 .
    花了好几个小时后,我删除了 setPageTransformer 并开始工作了

    //  viewpager.setPageTransformer(false, new BackgPageTransformer())
    

    我有一个scalling逻辑 .

    public class BackgPageTransformer extends BaseTransformer {
    
        private static final float MIN_SCALE = 0.75f;
    
        @Override
        protected void onTransform(View view, float position) {
            //view.setScaleX Y
        }
    
        @Override
        protected boolean isPagingEnabled() {
            return true;
        }
    }
    
  • 68

    你可以做的是你可以通过使用onClick属性对该主片段的父布局进行空白点击前一个片段的布局,并且在活动中你可以创建一个函数 doNothing(View view) 并且不要在其中写入任何内容 . 这将为你做到 .

相关问题