当前位置:主页 > 查看内容

Fragment学习总结(1)

发布时间:2021-08-04 00:00| 位朋友查看

简介:1什么是FragmentFragment是一种可以嵌入在活动中的UI片段能够让程序更加合理和充分地利用大屏幕的空间出现的初衷是为了适应大屏幕的平板电脑可以将其看成一个小型Activity又称作Activity片段。 2Fragment依赖于Activity不能独立存在一个Activity可以有多个Fr……

1:什么是Fragment,Fragment是一种可以嵌入在活动中的UI片段,能够让程序更加合理和充分地利用大屏幕的空间,出现的初衷是为了适应大屏幕的平板电脑,可以将其看成一个小型Activity,又称作Activity片段。

2:Fragment依赖于Activity,不能独立存在,一个Activity可以有多个Fragment,一个Fragment可以被多个Activity重用,Fragment有自己的生命周期,并能接收输入事件,可以在Activity运行时动态地添加或删除Fragment
在这里插入图片描述
3:Fragment的生命周期
生命周期我们下次再来细说。
在这里插入图片描述

3:接下来就写一个比较简单的实例
(1)activity_main.xml

<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="vertical"
    tools:context="com.example.myapplication.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:gravity="center">
        <TextView
            android:id="@+id/text_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="Hello World!" />
    </LinearLayout>

    <fragment
        android:id="@+id/fargment_id"
        android:name="com.example.myapplication.FragmentButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:layout_gravity="center"/>

(2)fragment_button.xml

<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"
    tools:context="com.example.myapplication.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:gravity="center">
        <Button
            android:id="@+id/button_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="click" />
    </LinearLayout>


</LinearLayout>

(3)FragmentButton类

public class FragmentButton extends Fragment implements View.OnClickListener {

    private Button button;
    private TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_button,null);

        button = (Button) view.findViewById(R.id.button_id);
        button.setOnClickListener(this);

        //这里就是把view返回给MainActivity里的方法
        return view;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button_id:
                //在Fragment中使用Activity中控件的方式
                // 在当前的Fragment中调用getActivity方法获取依附着的那个Activity,
                // 然后再用获取到的Activity去findViewById拿到你需要的控件对其操作就行了。
                AppCompatActivity activity = (AppCompatActivity) getActivity();
                textView = (TextView) activity.findViewById(R.id.text_id);
                textView.setText("我是 fragment Button");
                break;
            default:
                break;
        }
    }
}

(4)MainActivity类

public class MainActivity extends AppCompatActivity {

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

5:运行结果
在这里插入图片描述
点击按钮
在这里插入图片描述

;原文链接:https://blog.csdn.net/weixin_44941105/article/details/115532482
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐