Android Fragments Android片段中的问题:仍单击上一个片段

e37o9pze  于 2022-11-24  发布在  Android
关注(0)|答案(3)|浏览(187)

我开发了一个具有导航抽屉和抽屉内许多片段的应用程序,因此在打开片段内的片段时遇到问题,在一个片段中,当用户单击列表视图项目时,他们会获得与列表项目相关的数据,因此我面临的问题是,仍然单击不可见的列表,但单击
片段布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:clickable="true"
        android:background="@drawable/backgroung"
        android:id="@+id/content_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"></FrameLayout>

    <LinearLayout
        android:id="@+id/linearDrawer"
        android:layout_width="@dimen/dp260"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/white"
        android:layout_gravity="start">

        <RelativeLayout
            android:id="@+id/userDrawer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_margin="@dimen/dp10">

打开片段的代码

Fragment fragment = new FragmentContactDetails();
            FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            transaction.add(((ViewGroup) getView().getParent()).getId(), fragment);
            transaction.addToBackStack(null);
            transaction.commit();

            Bundle args = new Bundle();
            int index = adapter.arrayList.indexOf(adapter.propertyList.get(position));

            args.putInt(Constant.POSITION, index);
            Toast.makeText(getActivity(), "" + index + "----" + adapter.propertyList.get(position).getName(), Toast.LENGTH_LONG).show();
            fragment.setArguments(args);
pokxtpni

pokxtpni1#

看看我的这个问题:
An invisible layout behind the fragment is getting clicked:
在这种情况下,正如公认的答案所述,帮助我的是添加
android:clickable="true"
属性设置为您为顶部片段(正在被点击通过的片段)设计的布局的顶部层次结构ViewGroup。这样,无论您的点击发生在何处(即使没有对其应用任何操作),顶部片段都会拦截您的点击,并且不会传递到较低级别的片段/Activity布局。

n53p2ov0

n53p2ov02#

到目前为止,使用编译SDK 33
android:可点击=“true”
如果视图是match_parent,则还不够,应改为fill_parent

dsf9zpds

dsf9zpds3#

从中间到结尾编辑您的代码提交行

Fragment fragment = new FragmentContactDetails();
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.add(((ViewGroup) getView().getParent()).getId(), fragment);

                Bundle args = new Bundle();
                int index = adapter.arrayList.indexOf(adapter.propertyList.get(position));

                args.putInt(Constant.POSITION, index);
                Toast.makeText(getActivity(), "" + index + "----" + adapter.propertyList.get(position).getName(), Toast.LENGTH_LONG).show();
                fragment.setArguments(args);
transaction.addToBackStack(null);
                transaction.commit();

相关问题