Fragment的创建与使用

使用Fragment最简单的方式就是把Fragment当成最普通的控件,可以直接写在Activity的布局文件中。整个过程只需要两步:一是继承Fragment,重写onCreateView,决定Fragment的布局,二是在Activity布局文件中加入Fragment,与普通的View一样。
MainActivity.java

package com.sdutacm.viewpager.Fragment;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.sdutacm.viewpager.R;

public class Main2Activity extends AppCompatActivity {

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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.sdutacm.viewpager.Fragment.MainActivity">

    <fragment
        android:id="@+id/id_fragment_title"
        android:name="com.sdutacm.viewpager.Fragment.TitleFragment"
        android:layout_width="match_parent"
        android:layout_height="45dp" />

    <fragment
        android:id="@+id/id_fragment_content"
        android:name="com.sdutacm.viewpager.Fragment.ContentFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

TitleFragment.java

package com.sdutacm.viewpager.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sdutacm.viewpager.R;

/**
 * Created by bummer on 2017/8/14.
 */

public class TitleFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_title,container,false);
        return view;
    }
}

fragment_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="#EE30A7"
    tools:context="com.sdutacm.viewpager.Fragment.TitleFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="新闻浏览器"
        android:textSize="28sp" />

</LinearLayout>

ContentFragment.java

package com.sdutacm.viewpager.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.sdutacm.viewpager.R;

/**
 * Created by bummer on 2017/8/14.
 */

public class ContentFragment extends Fragment{
    private Button contentButton;
    private TextView textView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content,container,false);
        intiView(view);
        return view;
    }

    private void intiView(View view) {
        contentButton = (Button) view.findViewById(R.id.contentButton);
        textView = (TextView) view.findViewById(R.id.contentText);
        contentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("中国羽毛队获得了奥运会冠军");
            }
        });
    }
}

fragment_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.sdutacm.viewpager.Fragment.ContentFragment"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/contentText"
        android:text="@string/content"
        android:textSize="25sp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/contentButton"
        android:text="@string/addNews"
        />

</LinearLayout>
原文地址:https://www.cnblogs.com/CCCrunner/p/11781843.html