fragment 碎片整理

时间:2023-03-08 19:07:23
fragment  碎片整理

fragment  碎片整理

activity_m1.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.music_bofangqi.MActivity1"> <fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:name="com.hanqi.music_bofangqi.MyFragment1"
android:id="@+id/fragment1"
/>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.hanqi.music_bofangqi.MyFragment2"
android:id="@+id/fragment2"
/>
</LinearLayout>

MActivity.java

package com.hanqi.music_bofangqi;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity; //1-Activity要继承FragmentActivity
public class MActivity1 extends FragmentActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_m1);
}
}

activity_fragment.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.music_bofangqi.FragmentActivity"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加Fragment"
android:onClick="bt1_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="移除Fragment"
android:onClick="bt2_onclick"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/ll"
android:orientation="horizontal"> </LinearLayout> </LinearLayout>

FragmentActivity.java

package com.hanqi.music_bofangqi;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View; public class FragmentActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
//碎片的容器
// 1.获得放置fragment的布局管理器
//linearLayout= (LinearLayout)findViewById(R.id.ll);
//2.动态加载fragment
//1)得到管理器
fragmentManager =getSupportFragmentManager();
//2) 得到事务管理器
fragmentTransaction = fragmentManager.beginTransaction();
//3)向容器内放入fragment,参数:1-容器id 2-fragment的实例,并提交
fragmentTransaction.add(R.id.ll,new MyFragment1()).commit();
}
MyFragment2 myFragment2; //动态添加
public void bt1_onclick(View v)
{
fragmentManager =getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//加入回退栈
fragmentTransaction.addToBackStack(null);
//3)向容器内放入fragment,参数:1-容器id 2-fragment的实例,并提交
// replace替换
fragmentTransaction.replace(R.id.ll, myFragment2).commit();
}
public void bt2_onclick(View v)
{
fragmentManager =getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//加入回退栈
fragmentTransaction.addToBackStack(null);
//remove移除
fragmentTransaction.remove(myFragment2).commit();
} }

fragment_my_fragment1.xml

<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="com.hanqi.music_bofangqi.MyFragment1"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是碎片1"
android:background="#f00"/> </FrameLayout>

fragment_my_fragment2.xml

<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="com.hanqi.music_bofangqi.MyFragment1"> <!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是碎片2" /> </FrameLayout>

MyFragment1.java

package com.hanqi.music_bofangqi;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MyFragment1 extends Fragment { public MyFragment1()
{
Log.e("TAG","构造碎片"); }
//在Activity显示完成之后调用的
// 设置Fragment的视图
//返回一个显示的View
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("TAG","onCreateView被调用"); return inflater.inflate(R.layout.fragment_my_fragment1, container, false);
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TAG", "onCreate被调用");
} //在Activity创建完成后调用
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("TAG", "onActivityCreated被调用");
} @Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e("TAG", "onAttach被调用");
} @Override
public void onStart() {
super.onStart();
Log.e("TAG", "onStart被调用");
} @Override
public void onResume() {
super.onResume();
Log.e("TAG", "onResume被调用");
} @Override
public void onPause() {
super.onPause();
Log.e("TAG", "onPause被调用");
} @Override
public void onStop() {
super.onStop();
Log.e("TAG", "onStop被调用");
} @Override
public void onDestroy() {
super.onDestroy();
Log.e("TAG", "onDestroy被调用");
} @Override
public void onDestroyView() {
super.onDestroyView();
Log.e("TAG", "onDestroyView被调用");
} @Override
public void onDetach() {
super.onDetach();
Log.e("TAG", "onDetach被调用");
}
}

MyFragment2.java

package com.hanqi.music_bofangqi;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class MyFragment2 extends Fragment { public MyFragment2()
{ }
//在Activity显示完成之后调用的
// 设置Fragment的视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { //手动创建视图
TextView textView = new TextView(getActivity()); textView.setText("这是手动生成的内容");
return textView;
//return inflater.inflate(R.layout.fragment_my_fragment2, container, false); } }