第一次认识fragment

文章介绍了Fragment在平板设备中的使用原因,它允现在一个Activity中使用多个Fragment来构建多窗格界面。Fragment有自己的生命周期,并可以通过XML布局或动态添加到Activity中。文中详细讲解了如何创建Fragment,定义布局,处理点击事件,以及如何切换和动态添加Fragment。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fragement初使用

1、产生原因:在平板中实现看新闻等操作时列表是十分占用屏幕的,所以引出fragment,可以在平板一侧显示目录一侧显示具体内容。
2、是什么:fragment有自己的生命周期,所以可以在一个activity里使用多个fragment,构成多窗格的页面。并且一个fragment可以多个activity中重复使用。但是fragment必须委托在activity中运行,受activity的影响,当activity运行时可以独立操作fragment。
3、简单操作
1)首先建立一个空白的Fragment,自动拓展为一个fragment,在其中重写onCreate(),onCreatView()方法。

public class BlankFragment1 extends Fragment{

    private View root;
    private TextView textview;
    private Button button;

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

    }

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

    if(root==null){
        root=inflater.inflate(R.layout.fragment_blank1,container,false);
    }
    textview=root.findViewById(R.id.tv1);
    button=root.findViewById(R.id.btn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            textview.setText("YES and you?");
        }
    });
    return root;
    }
}

2)定义一个fragment_xml文件,添加TextView和button控件。区别:activity在Mainactivity里的onCreate方法里解析,而Fragment在自己的类中用inflater解析器来解析。
3)根据布局里控件的安排来获取点击事件,先找到ID然后设置点击事件。(用textview.setText()来改变显示内容)
4)最后在activity里面加入fragment组件。通过android:name让其与我们定义的fragment进行绑定。同时fragment必须定义一个id。

<fragment
    android:id="@+id/fragment1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  android:name="com.example.fragmenttest.BlankFragment1"/>

在这里插入图片描述

3、添加两个fragment

只用在添加一个fragment类,然后在activity中添加一个fragment组件,将权重设为1就可以得到平分屏幕的两个fragment。
在这里插入图片描述

4、切换fragment

1)首先在我们的activity里添加能够触发转换fragment的两个按钮,并且添加FrameLayout(不是fragment可以不要ID),用于把我们的fragment填到UI,button剩下的部分都属于fragment。
2)然后添加我们要切换的fragment(任意两个)
3)在Mainactivity中修改代码,由于涉及多个点击事件,可以让类连接接口View.onClickListener。在onclick方法中用swtch来选择不同的控件进行操作。再写一个转换的方法replacefragment。获取默认的管理器类(不是自己直接替换,而是拿到触发器)定义FragmentTransaction,完成替换提动作。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=findViewById(R.id.btn1);
        button.setOnClickListener(this);
        //通过button2
        Button button2=findViewById(R.id.btn2);
        button2.setOnClickListener(this);
    }
    public void onClick(View view){
        switch (view.getId())
            //根据不同的ID相应不同d button
        {
            case R.id.btn1:
                replacefragment(new BlankFragment01());
                break;
            case R.id.btn2://切换
                replacefragment(new ItemFragment());
        }
    }//完成动态切换fragment
    private void replacefragment(Fragment fragment){
        FragmentManager fragmentManager=getSupportFragmentManager();
        //获取默认的管理器类(
        FragmentTransaction transaction= fragmentManager.beginTransaction();
        transaction.replace(R.id.fragemnt_1,fragment);

        transaction.commit();//正式执行替换
    }
}

在这里插入图片描述
进一步优化可以用transaction.addToBackStack来添加一个fragment的栈,获取之前操作的存入栈中。

5、动态添加fragment

1)创建一个待处理的fragment;
2)获取fragmentManager,一般都是通过getSupportFragmentManager();
3)开启一个事务transaction,一般调用fragmentManager的beginTransaction();
4)使用transaction进行fragment的替换,transaction.replace;
5)提交事务commit()。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值