fragment(碎片),在Android开发中可以提供便捷的切换模式。
这里介绍一下啊fragment的动态用法
用例为在Android Studio下的NavigationView模板开发
fragment的class定义如下:举例nameFragment类(Android Studio下可以直接New->fragment->Blank)
public class nameFragment extends Fragment {
...
//onCreateView的return就是要显示的fragment界面
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = null; try { view = inflater.inflate(R.layout.fragment, container, false); } catch (Exception e) { e.printStackTrace(); } return view; }...
}
继承Fragment需继承android.app.Fragment,如果继承v4版本会报Error inflating class fragment错误!(版本兼容问题)
对fragment.xml进行布局。(Android Studio下会自动生成)
创建一个mainActivity。<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="包名.ActivityFragment"> ... <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一个fragment" android:layout_marginTop="6dp" android:layout_marginLeft="6dp" android:textColor="#212121"/> ... </FrameLayout>
其xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" android:fitsSystemWindows="true" tools:context="com.example.administrator.daihuobangv10.SideSlip_Activity"> <android.support.design.widget.AppBarLayout android:id="@+id/id_fragment_title" android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <include layout="@layout/toolbar" /> </android.support.design.widget.AppBarLayout>
//用一个FrameLayout来装fragment <FrameLayout android:id="@+id/id_fragment_content" android:layout_below="@id/id_fragment_title" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </android.support.design.widget.CoordinatorLayout>
在mainActivity中:
public class mainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { //创建nameFragment对象 private nameFragment fragmentowner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_side_slip_); //定义控件 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);//标题栏 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);
//创建默认Fragment setDefaultFragment(); } private void setDefaultFragment() {
//创建FragmentManager对象
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
carowner = new CarOwner_Fragment();
transaction.replace(R.id.id_fragment_content, carowner);
transaction.commit();
}
...
}public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); FragmentManager fm = getFragmentManager(); // 开启Fragment事务 FragmentTransaction transaction = fm.beginTransaction(); if (id == R.id.按钮名字) { if(namewoner == null){ nameowner = new nameFragment(); } transaction.replace(R.id.id_fragment_content, goodowner); } else if (id == xxx) { if(otherfragmentowner == null){otherfragmentowner = new otherFragment();} transaction.replace(R.id.id_fragment_content, otherfragmentowner); }//开始更换碎片 transaction.commit(); return true; }