Android 如何在单击按钮时从一个片段移动到另一个片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20784516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:27:49  来源:igfitidea点击:

how to move from one fragment to another fragment on button click

androidandroid-fragments

提问by Sanjeev

I am using the SherlockFragments library for the sliding menu.I have list of items as menu when I click on item fragment get opened as an activity but it is a fragment.Now I am new to fragments.i don't know how to move from one fragment to another fragment.As in activity, we have intent to move to another activity.but in fragment I don't how to move to another fragment.I have a button in fragmentA.when I click on this button it moves to fragment B. By googling I came to know that it has different cycles but anyhow I get toast msg when I click button here is following code

我正在使用 SherlockFragments 库作为滑动菜单。当我点击项目片段时,我将项目列表作为菜单打开,但它是一个片段。现在我是片段的新手。我不知道如何移动从一个片段到另一个片段。就像在活动中一样,我们打算移动到另一个活动。但在片段中我不知道如何移动到另一个片段。我在 fragmentA 中有一个按钮。当我点击这个按钮时,它会移动到片段 B. 通过谷歌搜索,我开始知道它有不同的周期,但无论如何,当我点击这里的按钮时,我得到了吐司味,下面是代码

public class Fragment2 extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.actionnetworklogin, container, false);
        Button login = (Button)view.findViewById(R.id.login);

        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(v.getContext().getApplicationContext(),"login clicked", 5000).show();
            }
        });
        return view;
    }   
}

Can someone please tell me how can I move one fragment to another fragment?

有人可以告诉我如何将一个片段移动到另一个片段吗?

Is there any other method that I can use activities instead of fragments?

有没有其他方法可以使用活动而不是片段?

I have written code for all activities and java files but I don't know that sliding menu has fragmented and now I have to write all the code fragments.

我已经为所有活动和 java 文件编写了代码,但我不知道滑动菜单已经碎片化,现在我必须编写所有代码片段。

回答by Daulat Bachhav

It's simple Only three line code...

很简单只有三行代码...

Fragment fragment = new SalesFragment();

FragmentManager fragmentManager = getFragmentManager();

fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

回答by Thiago

This is the code I use to switch fragments inside a view:

这是我用来在视图中切换片段的代码:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace([viewId], fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();

回答by Amit Jayaswal

You can try this also:-

你也可以试试这个:-

public void ButtonClick(View view) {
  Fragment mFragment = new YourNextFragment(); 
 getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, mFragment ).commit();
 }
public void ButtonClick(View view) {
  Fragment mFragment = new YourNextFragment(); 
 getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, mFragment ).commit();
 }

回答by Gajen Vishwakarma

FragmentManager fragmentManager = getFragmentManager();
                fragmentManager
                        .beginTransaction()
                        .replace(R.id.frame_container,
                                new TeacherFragment()).commit();

回答by Sanjeev

 //Below is the example 

 //In first Fragment 

Fragment fragment = new YourFragmentClassName();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();                       android.support.v4.app.FragmentTransaction ft= fragmentManager.beginTransaction();
ft.replace(R.id.flContent, fragment);
Bundle args = new Bundle();
// Pass the values what you want to send to next fragment 
args.putInt("Year", rYear);
args.putString("Month", rMonth);
args.putInt("Industry", rIndustry);
fragment.setArguments(args);
ft.commit();

//In Second Fragment

//In onCreateView Method get the values



int strYear= getArguments().getInt("Year");
String strMonth = getArguments().getString("Month");
strIndustry= getArguments().getInt("Industry");

//That's it very simple

回答by Vishal Vaishnav

 FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
 FirstFragment regcomplainfragment = new FirstFragment();
 fragmenttransaction.replace(R.id.content_frame,   regcomplainfragment).addToBackStack("tag");
 fragmenttransaction.commit();