2014-07-20 79 views
0

我是Android Fragments的新手。我有2個不同的片段,並希望使用按鈕從一個片段導航到另一個片段。例如,在片段A中,當我按下按鈕時,我會去fragmentB和vise詩句。如何在片段之間導航?

這裏是我的簡單代碼:

FragmentA.java

public class FragmentA extends Fragment { 

    public FragmentA(){} 

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

     View rootView = inflater.inflate(R.layout.fragment_a, container, false); 

     return rootView; 
    } 

} 

fragmenta.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="670dp" 
    android:gravity="top" 
    android:orientation="vertical" > 


    <TextView 
     android:id="@+id/txtLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="15dp" 
     android:text="Fragment 1" 
     android:textStyle="bold" 
     android:textSize="22dp" /> 
</LinearLayout> 

FragmentB.java

public class FragmentB extends Fragment { 

    public FragmentB(){} 

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

     View rootView = inflater.inflate(R.layout.fragment_b, container, false); 

     return rootView; 
    } 

} 

fragmentb.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="670dp" 
    android:gravity="top" 
    android:orientation="vertical" > 


    <TextView 
     android:id="@+id/txtLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="15dp" 
     android:text="Fragment 2" 
     android:textStyle="bold" 
     android:textSize="22dp" /> 
</LinearLayout> 

我需要代碼(包括java和xml)按鈕。從頭開始幫助,將不勝感激。提前致謝!!

+0

http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ –

+0

片段A和B通過其活動託管? – Raghunandan

+0

@Raghunandan片段A和B雖然是主要活動... 我想在片段A中創建一個按鈕來導航到片段B ...所以我如何繼續? – hnvasa

回答

2

檢查這個Tutorial和下面的代碼做參考

Fragment1.java

public class Fragment1 extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment1, container, false); 
    } 

    public void onClick(View view) { 
     Fragment2 fragment2 = new Fragment2(); 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment1, fragment2); 
     fragmentTransaction.commit(); 
    } 

    } 

Fragment2.java

public class Fragment2 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment2, container, false); 
     } 
} 
0

代碼在活動

public class MyActivity extends FragmentActivity implements OnListenerChangeFragment { 

     FragmentA mFragmentA; 
     FragmentB mFragmentB; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
      // transaction.replace(R.id.fragment_container, newFragment); 
      transaction.addToBackStack(null); 
      transaction.commit(); 
     } 

     @Override 
     public void onShowFragmenA() { 
      //replace mFragmentB by mFragmentA 

     } 

     @Override 
     public void onShowFragmenB() { 
      //replace mFragmentA by mFragmentA 
     } 
} 

//代碼在片段

public class FragmentA extends Fragment { 

     private OnListenerChangeFragment mCallback; 

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

       Button mButton = new Button(getActivity()); 

       mButton.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
           mCallback.onShowFragmenB(); 
         } 
       }); 


       return mButton; 
     } 

     @Override 
     public void onAttach(Activity activity) { 
       super.onAttach(activity); 

       try { 
         mCallback = (OnListenerChangeFragment) activity; 
       } catch (ClassCastException e) { 
         throw new ClassCastException(activity.toString()+ " must implement OnListenerChangeFragment"); 
       } 
     } 
} 

public class FragmentB extends Fragment { 

     private OnListenerChangeFragment mCallback; 


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

       Button mButton = new Button(getActivity()); 

       mButton.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
           mCallback.onShowFragmenA(); 
         } 
       }); 

       return mButton; 
     } 


     @Override 
     public void onAttach(Activity activity) { 
       super.onAttach(activity); 

       try { 
         mCallback = (OnListenerChangeFragment) activity; 
       } catch (ClassCastException e) { 
         throw new ClassCastException(activity.toString()+ " must implement OnListenerChangeFragment"); 
       } 
     } 
}