FragmentTransaction仍然是旧片段

时间:2014-06-30 22:56:51

标签: java android listview android-fragments

我是新的Android编程。我想制作滑块左或右片段。例如,当按钮点击列表片段显示和地图片段隐藏,反之亦然。首先,我将2片段添加到我的framelayout,我使用hide / show代替替换。

然而,我的片段交易有时会起作用,有时也不起作用。例如,当点击按钮地图片段通过向左滑动进入(显示)并通过向右滑出列表隐藏。但有时列表仍然是我的框架布局。在这种情况下,当我按下按钮时,通常会出现反向效果但是列表片段进入(显示)我的帧布局并且它是重复的。

我如何解决遗留列表视图的问题。

我的代码在这里

Listview片段

<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函数中添加了这些片段。但是我隐藏了列表片段。

1 个答案:

答案 0 :(得分:0)

尝试使用

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

这可以解决多个片段实例的问题。

相关问题