android碎片功能实现,Android中碎片的使用方法详解

Android中碎片的使用方法详解

发布时间:2020-10-11 09:29:57

来源:脚本之家

阅读:70

作者:Kepler

Fragment的使用

其实碎片很简单,但是网上胡乱充数的博文太多了,以至于我们有时候觉得比较乱,今天就来简单讲解一下碎片的使用.

碎片的使用分为两种,静态添加碎片和动态添加碎片,我们就先来看一下静态添加碎片如何实现.

静态添加碎片

首先,先建两个Layout文件,这就是碎片的布局文件,大家可能也发现了,Android Studio里面可以直接快速建立碎片,就像Activity一样,但是这样会生成很多没用的代码,所以我们还是选择自己创建碎片布局.

两个布局都很简单,里面只有一个居中显示的TextView,下面贴一下代码.

第一个布局文件:fragment_first.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/holo_blue_light"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:text="This is the first fragment!"/>

第二个布局文件fragment_second.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/holo_green_light"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:text="This is the second fragment!"/>

现在布局文件建完了,我们该建立他们对应的Fragment了,也就是后台代码了。新建两个类,分别叫FirstFragment和SecondFragment,都继承于Fragment,需要注意一点,我们教程里面所使用的Fragment全都是android.support.v4.app.Fragment这个包下的,这样更有利于程序的兼容性.

贴一下两个类的代码,也很简单,只是重写了onCreateView方法来加载不同的布局文件.

public class FirstFragment extends Fragment {

private View view;//得到碎片对应的布局文件,方便后续使用

//记住一定要重写onCreateView方法

@Nullable

@Override

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

view = inflater.inflate(R.layout.fragment_first, container, false);//得到对应的布局文件

return view;

}

}

public class SecondFragment extends Fragment {

private View view;//得到碎片对应的布局文件,方便后续使用

//记住一定要重写onCreateView方法

@Nullable

@Override

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

view = inflater.inflate(R.layout.fragment_second, container, false);//得到对应的布局文件

return view;

}

}

好,基本的工作我们做完了,现在我们用两个Activity展示如何静态添加碎片和动态添加碎片.

静态添加控件的话,需要使用fragment控件,指定其名称是你刚才创建的Fragment就可以,让我们来看一下.

先贴一下第一个Activity的布局:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="me.worldskills.android.testfragment.MainActivity">

android:id="@+id/main_firstfragment"

android:name="me.worldskills.android.testfragment.FirstFragment"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1">

android:id="@+id/main_secondfragment"

android:name="me.worldskills.android.testfragment.SecondFragment"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1">

android:id="@+id/main_btnGoNext"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="Go to next page"

android:textAllCaps="false"/>

其中那个按钮是用来跳转到下一个界面的,也就是动态添加碎片案例的Activity,在这里可以忽略.

这里我们看见了,两个fragment分别指定name为FirstFragment和SecondFragment,也就是你刚才创建的两个Fragment,一定要记得加上包名.对了,还有一个问题,就是这样的话是没有预览的,如果想要预览,需要在fragment标签中加上一句代码:

Tools:layout="@layout/布局文件名称"  .

好了,静态添加碎片就完成了,什么?就这么简单,对啊...就这么简单.

动态添加碎片

动态添加碎片我们就不需要用fragment控件了,而是需要用个FrameLayout控件,这是为什么呢,首先我们都知道FrameLayout中的控件,都是从左上角开始显示,不用进行位置控制,动态添加碎片其实就是向容器里面动态添加碎片,而fragment控件只能用来静态绑定一个碎片.

先贴一下第二个Activity的布局:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_second"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/second_btnLoadFirst"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Load First!"

android:textAllCaps="false"/>

android:id="@+id/second_btnLoadSecond"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Load Second!"

android:textAllCaps="false"/>

android:id="@+id/second_fl"

android:layout_width="match_parent"

android:layout_height="match_parent">

上面的两个按钮用来加载不同的碎片,而下面的FrameLayout就是碎片显示的容器.

废话不多说,贴代码:

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentTransaction;

public class SecondActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

//点击第一个按钮的时候加载第一个碎片

findViewById(R.id.second_btnLoadFirst).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction transaction = fragmentManager.beginTransaction();

transaction.replace(R.id.second_fl, new FirstFragment());

transaction.commit();

}

});

//点击第二个按钮的时候加载第二个碎片

findViewById(R.id.second_btnLoadSecond).setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit();//简写

}

});

}

}

getSupportFragmentManager方法用来获得一个碎片管理器对象(使用这个方法的时候注意是android.support.v4.app包下的哦),然后通过这个方法开始一个碎片事物对象,这个对象比较关键,可以用来动态添加碎片,调用它的replace方法,会把指定容器里面的其他控件全部清除掉,然后添加新的碎片进去.在这里就是先把R.id.second_f1里面的控件清空,然后添加传入一个FirstFragment进去.

替换完之后一定要记得调用commit方法提交,要不然你的所有操作都不会生效,切记.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值