【问题标题】:i want to hide fragment layout when click any place in screen单击屏幕中的任何位置时,我想隐藏片段布局
【发布时间】:2017-11-17 11:34:49
【问题描述】:

这是在底部视图中添加的一种框架布局。如果用户点击屏幕上的任何地方,我想隐藏

我已尝试获取父布局并在该触摸侦听器上设置可见性 GONE,但在触摸侦听器上不起作用。这是我的代码。 查看 rootView = getWindow().getDecorView().getRootView();

    rootView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                // show-hide view here

                contentSubTableList.setVisibility(View.GONE);

                AppUtility.showToast(PhoneBaseActivity.this, "On touch listner");

                return true;
            }

            contentSubTableList.setVisibility(View.GONE);

            AppUtility.showToast(PhoneBaseActivity.this, "On touch listner");
            return true;
        }
    });

    rootView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });

    llParentView = (LinearLayout) findViewById(R.id.llParentView);
    llParentView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                // show-hide view here

                contentSubTableList.setVisibility(View.GONE);

                AppUtility.showToast(PhoneBaseActivity.this, "On touch listner");

                return true;
            }


            contentSubTableList.setVisibility(View.GONE);

            AppUtility.showToast(PhoneBaseActivity.this, "On touch listner");

            return true;
        }
    });

请任何人帮助我。

【问题讨论】:

  • 您可以尝试将其添加到您的 xml 的父布局中
  • @VivekMishra 是的,我也尝试在 xml 中添加父布局

标签: android


【解决方案1】:

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llParentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">


<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:maxWidth="@dimen/_50sdp"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="@dimen/_50sdp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:maxWidth="@dimen/_50sdp">


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/transparent">

            <RelativeLayout
                android:layout_width="@dimen/_50sdp"
                android:layout_height="match_parent"
                android:background="@color/black">


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="@dimen/_minus2sdp"
                    android:layout_marginTop="@dimen/_30sdp"
                    android:src="@drawable/ic_whitetable" />


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/rvTableList"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="@dimen/_5sdp"
                    android:layout_marginTop="@dimen/_60sdp"
                    android:background="@color/black" />

            </RelativeLayout>


        </RelativeLayout>


    </android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

<FrameLayout
    android:id="@+id/content_bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvSubTable"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_40sdp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="bottom"
        android:layout_marginLeft="@dimen/_50sdp"
        android:background="@color/black"
        android:clipToPadding="false"
        android:orientation="horizontal"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager">

    </android.support.v7.widget.RecyclerView>


</FrameLayout>

【讨论】:

    【解决方案2】:

    您可以使用此方法隐藏和显示片段

    public void showHideFragment(final Fragment fragment){
    
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.setCustomAnimations(android.R.animator.fade_in,
                        android.R.animator.fade_out);
    
        if (fragment.isHidden()) {
            ft.show(fragment);
            Log.d("hidden","Show");
        } else {
            ft.hide(fragment);
            Log.d("Shown","Hide");                        
        }
    
        ft.commit();
    }
    

    【讨论】:

    • 布局未添加到特定片段中,它添加到活动中以供所有人查看
    【解决方案3】:

    在 Fragment 的父视图布局上的 setOnClickListener()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 2018-05-26
      • 2012-04-20
      • 2018-12-13
      • 2015-10-24
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多