Fragment使用小记

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下会自动生成)
<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>

创建一个mainActivity。

其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;
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值