如何用另一个片段替换片段?

时间:2016-10-10 12:28:50

标签: android android-fragments fragmentmanager

我在片段中遇到麻烦。我有两个片段&两个片段都包含List。我正在做的是当用户点击按钮时,片段应该从另一个片段替换。当我点击按钮时,我得到的问题是另一个片段正在下面到当前片段。

截图

enter image description here

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void selectFrag(View view) {
        Fragment fr;

        if(view == findViewById(R.id.button2)) {
            fr = new FragmentTwo();

        }else {
            fr = new FragmentOne();
        }

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();

    }

}

FragmentOne.Class

public class FragmentOne extends ListFragment implements OnItemClickListener {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_one, container, false);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.action, android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        Object o = this.getListAdapter().getItem(position);
        String pen = o.toString();

        // Toast.makeText(getActivity(), pen, Toast.LENGTH_SHORT).show();
    }



}

FragmentTwo.Class

public class FragmentTwo extends ListFragment implements OnItemClickListener {



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_two, container, false);
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.type, android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        Object o = this.getListAdapter().getItem(position);
        String pen = o.toString();

        // Toast.makeText(getActivity(), pen, Toast.LENGTH_SHORT).show();
    }

}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="selectFrag"
        android:text="Fragment No.1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="selectFrag"
        android:text="Fragment No.2" />

    <fragment
        android:id="@+id/fragment_place"
        android:name="com.example.pitech09.myapplication.FragmentOne"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

正如@RishabhDuttSharma建议的那样,在您的活动布局中使用FrameLayout代替fragment并在代码中初始化第一个片段。

布局:

<FrameLayout
    android:id="@+id/fragment_place"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Fragment fr = new FragmentOne();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.commit();
}

答案 1 :(得分:1)

//主要活动

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<!-- a frame layout for replace in full-->
        <FrameLayout
                android:id="@+id/fragment_place"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"/>

    </LinearLayout>

//片段代码  //在这里你必须编写代码并根据click事件你可以替换片段

FragmentOne fr = new FragmentOne();
   FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.commit();

//在下一个片段中你必须这样做

答案 2 :(得分:-1)

MainActivity

中使用此代码
    Button btnList = (Button) findViewById(R.id.btnList);
    Button btnGrid = (Button) findViewById(R.id.btnGrid);
    btnList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Fragment listfragment = new ListFragment();
            getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_place, listfragment).commit();

        }
    });
    btnGrid.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Fragment gridfragment = new GridFragment();
            getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_place, gridfragment).commit();
        }
    });