介绍
fragment, 简称碎片,在android3中提出的,有一套自己的API,最低兼容是1.6,fragment有以下特点:
a. Fragment 是依赖Activity的不能独立存在;
b.一个Activity里可以有多个Fragment;
c.一个Fragment可以被多个Activity重用;
d.Fragment有自己的生命周期,并向它传值;
e.我们能在Activity运行时动态的条件或者删除Fragment;
代码
创建一个动态fragment 并将Activity中的一个string和一个int传入Fragment中
MainActivity.java
package com.example.demo_fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout layout;
Button button;
FragmentManager fragmentManager;
int index;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.creat_fragment);
layout = findViewById(R.id.line1);
index = 100;
name = "WangChenChen";
fragmentManager = getSupportFragmentManager();
/*按钮触发函数*/
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*触发fragment后对主界面布局进行设置,据左占整个界面的1/3,注意id一个是上一层的id一个是本层id*/
layout.setLayoutParams(new LinearLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth()/3,
getWindowManager().getDefaultDisplay().getHeight()));
FragmentTransaction transaction = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
MyFragment myFragment = new MyFragment();
/*往bundle中添加数据*/
bundle.putInt("index",index);
bundle.putString("test1", name);
/*把数据设置到Fragment中*/
myFragment.setArguments(bundle);
/*这里的id是表示创建的fragment是在这个id所在的layout下面*/
transaction.replace(R.id.layout,myFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
activity_main.xml
<?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:id="@+id/layout"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/creat_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CreateFragment"/>
</LinearLayout>
</LinearLayout>
Myfragment.java
package com.example.demo_fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.sql.SQLOutput;
public class MyFragment extends Fragment {
LinearLayout layout;
Button button;
TextView textViewIndex;
TextView textViewName;
FragmentManager manager;
private Context mContext;
MainActivity mainActivity;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
layout = getActivity().findViewById(R.id.line1);
/*获得根视图*/
View view = inflater.inflate(R.layout.layout1,container,false);
button =view.findViewById(R.id.button2);
textViewIndex = view.findViewById(R.id.index);
textViewName = view.findViewById(R.id.name);
/*从bundle取到index, 通过index拿到名字和detail,写入textView*/
Bundle bundle = this.getArguments();
int index = bundle.getInt("index");
String test1 = bundle.getString("test1");
System.out.println("index :"+index);
System.out.println("name :"+test1);
textViewIndex.setText(String.valueOf(index));
textViewName.setText(test1.toString());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.remove(MyFragment.this);
transaction.commit();
maxScreen();
}
});
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 成功创建一个fragment后顺序是先运行onCreateView函数加载界面,
*在运行onCreate函数,如果没有getActivity()会找不到Activity;
* */
this.mContext = getActivity();
}
@Override
public void onDestroyView() {
super.onDestroyView();
maxScreen();
}
private void maxScreen()
{
/*当退出时,获取主界面全屏显示*/
layout.setLayoutParams(new LinearLayout.LayoutParams(getActivity().getWindowManager().getDefaultDisplay().getWidth(),
getActivity().getWindowManager().getDefaultDisplay().getHeight()));
}
}
layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/line_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/index"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="61dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="40dp"
android:text="隐藏" />
</LinearLayout>
</LinearLayout>
结果:
上面都进行了详细的解释,本章关于创建动态Fragment与传递数据结束;
源码地址:链接:https://pan.baidu.com/s/127zCSfRBHSIaiGPYRuiphg
提取码:qf4f
E-mail:jsntwangchenchen@outlook.com