【问题标题】:issue in Android Fragment : still click on previous fragmentAndroid片段中的问题:仍然单击上一个片段
【发布时间】:2014-07-17 17:42:08
【问题描述】:

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

片段布局

 <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);

【问题讨论】:

  • 添加 transaction.commit();在 fragment.setarguments 之后的最后一行

标签: android android-fragments android-fragmentactivity


【解决方案1】:

看看我的这个问题:

An invisible layout behind the fragment is getting clicked:

在这种情况下对我有帮助的,并且作为接受的答案状态是添加

android:clickable="true"

您为顶部的片段设计的布局的顶级层次结构ViewGroup 的属性(被点击的那个)。这样,无论点击发生在哪里(即使没有对其应用任何操作),顶部片段都会拦截您的点击,并且不会传递到较低级别的片段/活动布局。

【讨论】:

  • 我必须在哪里实现这个标签,在主框架布局中还是......?
  • 这是救命稻草。谢谢!
  • 简单有效的解决方案。谢谢男人:)
  • 救命稻草 :) 谢谢。
  • 像魅力一样工作,非常感谢。我本来打算采用非常困难的方法,但在采用这种方法之前,我想过用谷歌搜索一次,它真的得到了回报。
【解决方案2】:

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

    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();

【讨论】:

    猜你喜欢
    • 2013-05-24
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多