做了兩個界面跳轉的Fragment 只實現了簡單跳轉 代碼不多可以借鑑
有個佈局文件 兩個java文件
代碼如下
public class Frame_main extends AppCompatActivity { public Frame_data mFrame_data; public Frame_chat mFrame_chat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } public void initView(){ ImageView frame_data; ImageView frame_chat; frame_data = (ImageView) findViewById(R.id.frame_data); frame_chat = (ImageView) findViewById(R.id.frame_chat); frame_data.setOnClickListener ( new View.OnClickListener ( ) { @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); //開啓Fragment事務 FragmentTransaction fragmentTransaction = fm.beginTransaction(); if (mFrame_data == null){ mFrame_data = new Frame_data (); } //使用當前的Fragment的佈局代替id_bottombar_liaotian的控件 fragmentTransaction.replace(R.id.frame_main,mFrame_data); //事務提交 fragmentTransaction.commit(); } } ); frame_chat.setOnClickListener ( new View.OnClickListener ( ) { @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); //開啓Fragment事務 FragmentTransaction fragmentTransaction = fm.beginTransaction(); if (mFrame_chat == null){ mFrame_chat = new Frame_chat (); } fragmentTransaction.replace(R.id.frame_main,mFrame_chat); //事務提交 fragmentTransaction.commit(); } } ); //設置默認的Fragment setDefaultFragment(); } private void setDefaultFragment(){ FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); mFrame_data=new Frame_data (); fragmentTransaction.replace(R.id.frame_main,mFrame_data); fragmentTransaction.commit(); } }2、第一個界面的java
public class Frame_data extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ return inflater.inflate(R.layout.frame_data,container,false); } }3、第二個界面的java
public class Frame_chat extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ return inflater.inflate(R.layout.frame_chat,container,false); } }佈局文件如下
1、frame_main.xml
<RelativeLayout android:id="@+id/content_main" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.mars.aaaa.MainActivity" tools:showIn="@layout/app_bar_main"> <FrameLayout android:id="@+id/frame_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true"/> <include android:id="@+id/include_title" android:layout_width="match_parent" android:layout_height="45dp" layout="@layout/frame_title" android:layout_marginBottom="10dp" android:layout_alignParentBottom="true" android:layout_alignParentStart="true"/> </RelativeLayout>2、frame_title.xml
<?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" xmlns:tools="http://schemas.android.com/tools" tools:showIn="@layout/content_main"> <LinearLayout android:background="#f2f0f0" android:layout_width="match_parent" android:layout_height="wrap_content"> <Space android:layout_width="70dp" android:layout_height="wrap_content"/> <ImageView android:id="@+id/frame_data" android:background="@drawable/touxiang_nv" android:layout_width="55dp" android:layout_height="55dp"/> <Space android:layout_width="130dp" android:layout_height="wrap_content"/> <ImageView android:id="@+id/frame_chat" android:background="@drawable/touxiang" android:layout_width="55dp" android:layout_height="55dp"/> </LinearLayout> </LinearLayout>3、剩下兩個文件基本差不多 就列舉一個
<?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"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="data"/> </LinearLayout>