如何从一个片段导航到另一个片段?

这是我的代码:

activity_main.xml中

    

fragment_view1.xml

    

custom_view.xml

     

MainActivity.java

 package com.example.fragmenttest; import android.os.Bundle; import android.app.Activity; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends FragmentActivity { private ViewPager viewPager; private MyAdapter pageAdapter; private static final int ITEMS = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager)findViewById(R.id.pager); pageAdapter = new MyAdapter(getSupportFragmentManager()); viewPager.setAdapter(pageAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public static class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fragmentManager) { super(fragmentManager); } @Override public int getCount() { return ITEMS; } @Override public Fragment getItem(int position) { if(position==0) { return new FirstView(); } else { return new SecondView(); } } } public void setCurrentItem (int item, boolean smoothScroll) { viewPager.setCurrentItem(item, smoothScroll); } public void onMenuItemClicked(View view) { Toast.makeText(this, "LOL", Toast.LENGTH_LONG).show(); } } 

FirstView.java

 package com.example.fragmenttest; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class FirstView extends DropDownMenu { private TextView firstText; private Button btn; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_view1,container,false); firstText = (TextView)view.findViewById(R.id.viewOneText); btn = (Button)view.findViewById(R.id.viewOneBtn); btn.setOnClickListener(new ButtonEvent()); return view; } private class ButtonEvent implements OnClickListener { } } 

CustomView.java

 package com.example.fragmenttest; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class CustomView extends Fragment { private TextView secondText; private Button secondViewBtn; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.custom_view,container,false); return view; } } 

当我单击fragment_view1.xml的按钮时,我需要转到custom_view.xml屏幕。 这是一个完全不同的片段。 我怎样才能做到这一点?

将其添加到OnClickListener

 CustomView cv = new CustomView(); FragmentManager fm= getFragmentManager(); FragmentTransaction ft= fm.beginTransaction(); ft.replace(R.id.custom_view, cv); ft.commit(); 

你需要将它添加到你的RelativeLayout中

custom_view.xml

 android:id="@+id/custom_view" 

试试这个……放在听众里面

 // Create new fragment and transaction Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); 

//创建一个FrameLayout并在fragment_view1.xml中将id作为fragment_view提供,并在下面执行

在您的布局fragment_view1.xml添加此行

   private class ButtonEvent implements OnClickListener { CustomView custFra = new CustomView(); fragmentTransaction.replace(R.id.fragment_view, custFra).commit(); }