简体   繁体   English

如何从另一个片段打开片段

[英]How to open a fragment from another fragment

I have a tabbed activity in which I have three tabs. 我有一个标签式活动,其中有三个标签。 The main tab is the home tab and it contains a button. 主选项卡是主选项卡,它包含一个按钮。 My question is how do I open a new fragment after pressing the button and keeping it in the same tabbed activity ? 我的问题是如何在按下按钮并将其保持在相同的选项卡活动后打开一个新片段? Here is my Home tab class code: 这是我的Home选项卡类代码:

import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
 * Created by user-pc on 15/01/2017.
 */


public class Home extends Fragment {
    private boolean flag;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.hometab, container, false);
        final Button RestButton = (Button) rootView.findViewById(R.id.RestButton);
        final Button ShopButton = (Button) rootView.findViewById(R.id.ShopButton);
        final Button HangoutButton = (Button) rootView.findViewById(R.id.HangoutButton);
        RestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return rootView;
    }
}

When you add a default tabbed activity in android you get an activity that uses ViewPager as a container for fragments managed by a FragmentPagerAdapter. 当您在android中添加默认选项卡式活动时,您将获得一个活动,该活动使用ViewPager作为FragmentPagerAdapter管理的片段的容器。

From FragmentPagerAdapter's documentation : 来自FragmentPagerAdapter的文档

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. 此版本的寻呼机最适合在有少量通常更多静态片段进行分页时使用,例如一组选项卡。

If I understand you correctly, you want one of the fragments, namely the Home fragment, which resides in a page (let's call it Page1) to be dynamically swapped with another fragment (let's call this new fragment FragmentB) and to persist as the new fragment if you switch to another page (let's call it Page2) and back to its page (meaning you will see FragmentB when switching back to Page1). 如果我理解正确的话,你想要一个片段,即Home片段,它驻留在一个页面中(让我们称之为Page1)与另一个片段动态交换(让我们调用这个新片段FragmentB)并保持为新的片段,如果你切换到另一个页面(让我们称之为Page2)并返回到它的页面(意味着当切换回Page1时你会看到FragmentB)。

For that it seems like you are better off using FragmentStatePagerAdapter . 为此,您最好使用FragmentStatePagerAdapter Inside the fragment for Page1 - have a container for another fragment (a fragment within the fragment), occupying the whole area of the parent fragment. 在Page1的片段内 - 有一个容器用于另一个片段(片段中的片段),占据父片段的整个区域。 The first and default fragment inside it would be the Home fragment and it would be loaded dynamically in onCreateView method if there was no state saved before. 其中的第一个和默认片段是Home片段,如果之前没有保存状态,它将在onCreateView方法中动态加载。 Else - the loaded fragment would be according the saved state - or even from the saved state directly. 否则 - 加载的片段将根据保存的状态 - 甚至直接从保存的状态。 To have that saved state - you need to implement the onSaveInstanceState(Buncle) to do have the saved state. 要拥有该已保存的状态 - 您需要实现onSaveInstanceState(Buncle)以获得已保存的状态。 Then you swap the Home fragment with FragmentB in inside the container fragment which is the one loaded every time into Page1 - but it remembers which fragment was loaded in it last - and can restore it correctly. 然后将Home片段与FragmentB交换到容器片段内,该容器片段每次都加载到Page1中 - 但它会记住最后加载到哪个片段 - 并且可以正确地恢复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM