Fragment学习

来源:互联网 发布:卖家开通淘宝客要求 编辑:程序博客网 时间:2024/06/04 18:14

官方教程 https://developer.android.com/training/basics/fragments/index.html

使用as新建两个空的fragment

这里写图片描述

新建一个activity用于显示fragment

package com.example.h.learn.activity;import android.net.Uri;import android.os.Bundle;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.view.View;import com.example.h.learn.R;import com.example.h.learn.fragment.BlankFragment1;import com.example.h.learn.fragment.BlankFragment2;public class FragmentActivity extends AppCompatActivity  implements View.OnClickListener, BlankFragment1.OnFragmentInteractionListener, BlankFragment2.OnFragmentInteractionListener{    //必须实现此方法,否则会报错    @Override    public void onFragmentInteraction(Uri uri) {    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_fragment);        int[] ids = new int[]{R.id.btn_fragment1, R.id.btn_fragment2};        for (int id:ids             ) {            findViewById(id).setOnClickListener(this);        }        if(findViewById(R.id.fragment_layout)!=null){            //避免fragment重叠            if(savedInstanceState !=null){                return;            }            //第一次创建,默认显示第一个fragment            BlankFragment1 itemFragment = new BlankFragment1();            //传参数            itemFragment.setArguments(getIntent().getExtras());            getSupportFragmentManager().beginTransaction().add(R.id.fragment_layout, itemFragment).commit();        }    }    @Override    public void onClick(View v) {        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();        switch (v.getId()){            //显示第一个fragment            case R.id.btn_fragment1:                transaction.replace(R.id.fragment_layout, new BlankFragment1());                transaction.addToBackStack(null);                transaction.commit();                break;            //显示第二个fragment            case R.id.btn_fragment2:                transaction.replace(R.id.fragment_layout, new BlankFragment2());                transaction.addToBackStack(null);                transaction.commit();                break;            default:        }    }}
<?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="vertical"    tools:context="com.example.h.learn.activity.FragmentActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:text="fragment1"            android:id="@+id/btn_fragment1"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <Button            android:text="fragment2"            android:id="@+id/btn_fragment2"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout>    <FrameLayout        android:id="@+id/fragment_layout"        android:layout_width="match_parent"        android:layout_height="match_parent"></FrameLayout></LinearLayout>

运行效果
这里写图片描述

这里写图片描述

fragment传参 http://dditblog.com/itshare_296.html