[英]FragmentTransaction remains old fragment
我是新的android編程。 我想使滑塊左或右片段。 例如,當單擊按鈕時,單擊列表片段顯示和地圖片段隱藏,反之亦然。首先,我在我的框架布局中添加了2個片段,然后使用隱藏/顯示而不是替換。
但是我的fragmenttransaction有時起作用,有時不起作用。 例如,單擊按鈕時,地圖片段向左滑動進入(顯示),而列表向右滑動則隱藏列表。 但有時列表仍然是我的框架布局。 在這種情況下,當我按下按鈕時,通常會發生反向效果,但是列表片段進入(顯示)我的框架布局,並且重復了。
我如何才能解決有關遺留清單視圖的問題。
我的代碼在這里
列表視圖片段
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
MainActivity.java
public void onClick(View view) {
if (mapshere) {
android.support.v4.app.FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
ft2.setCustomAnimations(R.anim.map_slide_in_right, R.anim.map_left_evade);
ft2.show(list);
ft2.hide(map);
ft2.commit();
btnList.setImageResource(R.drawable.icon_pin_menu1);
} else {
android.support.v4.app.FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
ft2.setCustomAnimations(R.anim.map_slide_in_left, R.anim.map_right_evade);
ft2.show(map);
ft2.hide(list);
ft2.commit();
btnList.setImageResource(R.drawable.icon1);
}
我的Listvie OnCreatView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.map_fragment_item_list, container, false);
// Set the adapter
mListView = (AbsListView) view.findViewById(android.R.id.list);
((AdapterView<ListAdapter>) mListView).setAdapter(getmAdapter());
mListView.setOnItemClickListener(this);
return view;
}
如前所述,我已經在OnCreate函數中添加了這些片段。 但是我隱藏列表片段。
嘗試使用
getSupportFragmentManager()
.beginTransaction()
// set your custom in/out animations
.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left,R.anim.slide_out_right)
// replace the content of the layout defined with R.id.content_frame by your fragment.
.replace(R.id.content_frame, fragment)
.commit();
這樣可以解決多個片段實例的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.