我只是一個本地android開發的初學者。我有一個應用程序,其中我有一個活動內的片段。該片段具有頁眉,頁腳以及在中間的FrameLayout顯示其它片段 XML片段的:使用動畫替換兩個片段彼此
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
tools:context=".fragments.Fragment1">
<!-- Dummy item for focus at startup -->
<LinearLayout
android:id="@+id/dummy_id"
android:orientation="vertical"
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<FrameLayout
android:id="@+id/header_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="@string/vehicle"
android:textAlignment="center"
android:textColor="@color/dark_grey_text"
android:textSize="@dimen/title_size" />
<ImageButton
android:id="@+id/closeBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="6dp"
android:background="@drawable/close_button_icon" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="top"
android:layout_marginTop="52dp"
android:background="@android:color/darker_gray" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/area_for_fragments"
android:layout_below="@+id/header_section"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/white">
<LinearLayout
android:id="@+id/footerLL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_below="@+id/bottom_separator_line"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="30dp"
android:visibility="gone">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="@string/titular_infractor"
android:textColor="@color/grey_text"
android:textSize="@dimen/text_size"
android:textStyle="bold" />
</LinearLayout>
<ImageButton
android:id="@+id/accept_button"
android:visibility="visible"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignRight="@+id/bottom_separator_line"
android:layout_below="@+id/bottom_separator_line"
android:layout_marginBottom="10dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="33dp"
android:layout_marginTop="-50dp"
android:background="@drawable/accept_button_selector" />
<ImageButton
android:id="@+id/cancel_button"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="@+id/bottom_separator_line"
android:layout_marginBottom="10dp"
android:layout_marginRight="3dp"
android:layout_marginTop="-50dp"
android:layout_toLeftOf="@+id/edit_vehicle_accept_button"
android:background="@drawable/cancel_button_selector"
android:visibility="gone"/>
<View
android:id="@+id/bottom_separator_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_marginBottom="55dp"
android:background="@android:color/darker_gray" />
</RelativeLayout>
要顯示所述第一片段我使用的childFragmentManager:
final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
TestSearchFragment elf = new TestSearchFragment();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.area_for_fragments, elf);
ft.addToBackStack(null);
ft.commit();
這個片段應該簡單的放置在那裏。點擊Accept按鈕後,第一個片段應該從頂部的屏幕上滑出,而新的第二個片段應該從底部滑動。目前我正在使用此代碼替換它們:
FragmentTransaction ft2 = getChildFragmentManager().beginTransaction();
TestDetailFragment elf2 = new TestDetailFragment();
ft2.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up);
ft2.replace(R.id.area_for_fragments, elf2);
ft2.addToBackStack("TEST FRAGMENT TAG")
.commit();
但由於任何原因片段被替換,之後動畫開始。當動畫正在進行時,新片段滑出屏幕上方,舊片段從底部滑入。動畫完成後,會顯示新的片段。
我的代碼爲動畫:
Slide_in_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="5000"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="-100%"/>
</set>
slide_out_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="5000"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="100%"
android:toYDelta="0%"/>
</set>
我不知道如何解決它還是什麼原因是這個瘋狂的替代。 希望有人能幫助。 謝謝
爲什麼你有一個 - 'ft.add(小精靈,「搜索」);'?我認爲這會導致不可預知的行爲,因爲您在一次交易中添加並替換相同的片段。 – Divers
我試了一下,但後來這兩個片段都顯示在同一時間 –
對不起,我誤解了你... –