[英]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.