添加片段到片段运行良好,但finish()方法删除第二个片段不能正常工作

时间:2017-08-03 06:12:55

标签: android android-fragments

我将一个片段添加到String newString = rsl1.get(i).replace(n.charAt(i)+"\\rsl1+", ""); 中,这是其他片段(包含viewpager)的一小部分,它正在添加得很好,但是当我试图消除调用RelativeLayout的片段时它不起作用

xml代码:

finish()

用于删除新片段的片段代码:

</RelativeLayout>

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:paddingBottom="10dp"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true"
            >

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Share you thoughts here..."
                android:paddingLeft="10dp"
                android:background="@drawable/edittext_status_background"
                android:padding="20dp"
                android:elevation="5dp"
                android:id="@+id/editTextWritePostId"
                />



            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:fitsSystemWindows="true"
                android:background="#f9f9f9"
                android:id="@+id/relativeLayoutIdNewsFeedForPostBar"
                >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Post"
                    android:textColor="#ffffff"
                    android:background="#1470a6"
                    android:id="@+id/postButtonId"
                    />

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/checkInButtonId"
                    android:layout_toLeftOf="@+id/smileyButtonId"
                    android:layout_centerInParent="true"
                    android:src="@drawable/ic_checkin"
                    android:background="@android:color/transparent"
                    android:layout_marginRight="3dp"
                    />

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/smiley"
                    android:layout_toLeftOf="@+id/imageUploadButtonId"
                    android:layout_centerInParent="true"
                    android:layout_marginRight="10dp"
                    android:id="@+id/smileyButtonId"
                    />

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/camera"
                    android:layout_toLeftOf="@+id/videoUploadButtonId"
                    android:layout_centerInParent="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginRight="10dp"
                    android:id="@+id/imageUploadButtonId"
                    />
            </RelativeLayout>

               **//adding new fragment to below layout**
                **<RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/relativeLayoutIdForSmiley"
                    >
                </RelativeLayout>**

        </LinearLayout>

我试图消除RelativeLayout的可见度已消失,并且还消失了smileyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SmileyShowFragment smileyFragment=new SmileyShowFragment(); FragmentManager fragmentManager=getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); if(!(smileyFragment.isVisible())) { fragmentTransaction.replace(R.id.relativeLayoutIdForSmiley, smileyFragment, "Smiley").commit(); }else { relativeLayoutIdForSmiley.setVisibility(View.GONE); } } }); 片段,但这些都没有工作。请你帮我。我在互联网上搜索了很多与我一样的东西。

2 个答案:

答案 0 :(得分:1)

我想我已经找到了你的问题。这就是你犯的错误。在您的smileButton onclick中,您正在创建一个新的SmileyShowFragment实例(SmileyShowFragment smileyFragment=new SmileyShowFragment();),并使用if(!smileyFragment.isVisible())检查片段是否可见。无论您多长时间触发一次点击,这种情况总是如此,因为每次点击按钮都会创建片段实例。解决方案是,在按钮单击之外创建片段的实例。尝试这样的事情。

final SmileyShowFragment smileyFragment = new SmileyShowFragment();
smileyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Fragment fragment = fragmentManager.findFragmentById(R.id.relativeLayoutIdForSmiley);
            if(!(fragment instanceof SmileyShowFragment)) {
                fragmentTransaction.replace(R.id.relativeLayoutIdForSmiley, smileyFragment, "Smiley").commit();
            }else {
                fragmentTransaction.remove(smileyFragment).commit();
            }
        }
    });

答案 1 :(得分:0)

您是否尝试在片段上使用detachremove

去检查:https://developer.android.com/reference/android/app/FragmentTransaction.html