八、fragment静态、动态加载

首先时要加载的fragment,以及对应的java

fragmentitem.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <TextView
        android:id="@+id/fragmentitem_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="没改变"
        android:textSize="20dp" />

    <Button
        android:id="@+id/fragmentitem_but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="改变"
        app:layout_constraintTop_toBottomOf="@+id/fragmentitem_textview" />
</android.support.constraint.ConstraintLayout>

静态:fragmentitem.java

package com.example.administrator.listview;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Administrator on 2018/3/11 0011.
 */

public class fragmentitem extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragmentitem,container,false);
        TextView t=view.findViewById(R.id.fragmentitem_textview);
        t.setText("静态改变fragment");
        return view;
    }
}

在这个页面中加载上面的fragment

fragment_staticonlod.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/fragmentstatic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.administrator.listview.fragmentitem"></fragment>
</android.support.constraint.ConstraintLayout>

对应的fragment_staticonlod.java:

package com.example.administrator.listview;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 2018/3/11 0011.
 */

public class fragment_staticonlod extends AppCompatActivity {
    private TextView t;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_staticonlod);
        Button b=findViewById(R.id.fragmentitem_but);
        b.setText("改变");
        t=findViewById(R.id.fragmentitem_textview);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                t.setText("textview改变了");
            }
        });
    }
}


记得要把这个页面初始化!

<activity android:name=".fragment_staticonlod"
    android:label="fragment_staticonlod"></activity>

结果:


动态:

要在这个控件中加载fragment:

    <LinearLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"></LinearLayout>

改写上面的fragmentitem-》fragmentitem2:

package com.example.administrator.listview;

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.TextView;

/**
 * Created by Administrator on 2018/3/11 0011.
 */

public class fragmentitem2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragmentitem,container,false);
        TextView t=view.findViewById(R.id.fragmentitem_textview);
        t.setText("动态改变fragment");
        return view;
    }
}

点击后显示:

fragmentitem2 fragmentitem2=new fragmentitem2();
                FragmentManager fragmentManager=getFragmentManager();
                FragmentTransaction beginTransaction=fragmentManager.beginTransaction();
                beginTransaction.add(R.id.frame, fragmentitem2);
                beginTransaction.commit();






猜你喜欢

转载自blog.csdn.net/qq_38234785/article/details/79521569
今日推荐