Android开发Android开发经验谈Android技术知识

Android日记之Fragment的基本使用

2019-11-10  本文已影响0人  居居居居居居x

前言

(本篇基于Fragment基本使用,因为此篇总结的很好,我就基于此篇文章再写一些关于自己的理解)
Fragment,一般称之为碎片,是在Android 3.0出现的,Fragment是一种可以嵌入在活动中的UI片段,能够让程序更加合理和充分地利用大屏幕的空间,出现的初衷是为了适应大屏幕的平板电脑,官方对Fragment的定义是:

Fragment也是我们最常使用的组件,使用的频率可谓是非常的高,它也有以下几点优势:

Fragment生命周期

(此段来源于Fragment基本使用,因为这篇文章对于Fragment的生命周期总结的很好,参考这个就好了)

Fragment的生命周期
Fragment的一般生命周期如上图所示:

Fragment生命周期会经历:运行、暂停、停止、销毁。

Fragment的使用

在使用Fragment的时候,先介绍以及会用到的核心类:

开发Fragment不建议使用android.app下的Fragment而应是android:support.v4.app,因为support库是不断更新的。Fragment使用方式有有两种,静态使用和动态使用,我们先来看看静态使用。

1. 静态使用

首先创建一个Fragment,我们可以直接通过Android Stuodio进行创建,在你需要的位置右键新建选择Fragment(Blank)。


Fragment创建

这里我们发现下面有两个打钩选项,看意思就知道,第1个是是否创建工程方法,第2个是是否创建这个Fragment的回调接口,我们这里2个都不选,创建完毕就会生成对应的Fragment和XML文件了。


//TestFragment的Fragment文件
public class TestFragment extends Fragment {


    public TestFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_test, container, false);
    }

}


//TestFragment的XML文件
<?xml version="1.0" encoding="utf-8"?>
<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=".TestFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是Fragment" />

</FrameLayout>

然后我们在Activity中声明fragmen控件,然后就可以运行了。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/testFrag"
        android:name="com.ju.fragmentdemo.TestFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
Fragment测试运行结果
静态使用的缺点很明显,位置是固定的,不能删除,所以基本很少使用。

2.动态使用

我们还是老样子,先创建一个Fragment,因为刚刚已经创建过了,我们就用那个Fragment就行,然后把Activity布局上的Fragment删除,这里我们需要一个布局控件来去替换这个Fragment,我们这里就可以直接把LinearLayout来替换掉,我们给他声明个id,然后在Activity的onCreate()写入方法。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/lin_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

</LinearLayout>

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.lin_test,new TestFragment());
        transaction.commit();
    }
}
动态使用Fragment

这里说几点,在动态的加载Fragment中,FragmentTransaction是有提供各种方法来完成增删改等操作,完成后使用commit()方法就可以。

Fragment和Activity传递数据

有时候是需要将数据传递给Fragment的,但是不太推荐直接通过构造函数去进行传递,而且Fragment和Activity的互相传递方式也有不同。

1. Activity传递给Fragment

在Activity创建一个Bundle,把要传入的数据放入Bundle中,然后通过Fragment的setArguments()把Bundle设置进去,最后在Fragment中使用getArguments()来获取Bundle对象,就可以获取传递过来的值了。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建Bundle
        Bundle bundle = new Bundle();
        bundle.putString("key","传递的值");

        TestFragment testFragment = new TestFragment();
        testFragment.setArguments(bundle);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.lin_test,testFragment);
        transaction.commit();
    }
}


public class TestFragment extends Fragment {


    private TextView textView;

    public TestFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_test, container, false);
    }
    
    //在这里获取
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView =  getView().findViewById(R.id.text);
        Bundle bundle = getArguments();
        textView.setText(bundle.getString("key"));
    }
}
通过Bundle获取值
还有一种办法就是通过onAttach()方法来强转。
//Activity创建一个函数,可以设置要传入的值
public String getValue(){
    return "这里是传给Fragment的值";
}

//然后我们在Fragment的onAttach方法这样写,获取的值设置到控件上
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    value = ((MainActivity)context).getValue();
}
通过onAttach()结果

2. Fragment传递给Activity

这里我们可以使用接口回调来传递数据,也可以使用EventBus来进行传递,接口回调传递数据的代码如下。

//设置个接口
public interface Listener {
    
    void getValue(String v);
}

//在Fragment里面进行如下代码
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    //设置传给Activity的值
    listener.getValue("hello");
}


@Override
public void onAttach(Context context) {
    listener =  ((Listener)context);
    super.onAttach(context);
}

//Activit继承这个接口,接收回调过来的参数。
@Override
public void getValue(String v) {
    Toast.makeText(this, "已收到Fragment的消息:--" + v + "--,客气了", Toast.LENGTH_SHORT).show();
}
传给Activity结果

参考

上一篇 下一篇

猜你喜欢

热点阅读