Fragment - Android
本篇文章记录的是Android应用中的Fragment.Fragment必须委托在activity中才能运行.
基本使用
1.新建类(MeFragment),继承自Fragment
.
package com.example.myapplication.fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class MeFragment extends Fragment {
private String startupArgument;
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (view == null) {
view = inflater.inflate(R.layout.me, null);
}
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
startupArgument = bundle.getString("startup");
Log.e("ysd", "onCreate: me 当前传递的参数是 => " + startupArgument);
}
}
}
2.创建布局xml.在布局的xml中,添加fragment组件(必须指定ID).使用name与Java类进行绑定(MyFragment).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pink"
android:orientation="vertical">
<fragment
android:id="@+id/fragment"
android:name="com.example.myapplication.fragment.MeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
消息通信
1.Activity传递消息给Fragment
参数传递:
Boundle,在实例化Fragement时,通过putArguments设置.在Fragment内,通过this.getArguments.
Fragment frag = new MeFragment();
Bundle bundle = new Bundle();
bundle.putString("startup", "这是me参数");
frag.setArguments(bundle);
参数获取:
每次切换,会触发onCreate生命周期,在onCreate函数中,获取参数.
package com.example.myapplication.fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class MeFragment extends Fragment {
private String startupArgument;
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (view == null) {
view = inflater.inflate(R.layout.me, null);
}
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
startupArgument = bundle.getString("startup");
Log.e("ysd", "onCreate: me 当前传递的参数是 => " + startupArgument);
}
}
}
动态切换
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransition transition = fragmentManager.beginTransition();
// 此处指定fragment显示的区域
transition.replace(R.id.frameLayout,fragment);
transition.addToBackStack(null); // 增加到回退栈.
transtion.commit();
生命周期
onCreate
事件点击.
onCreateView
创建view,点击事件
root = inflater.inflate(R.layout.fragment1,container,false)
return root
完整代码
主布局:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pink"
android:orientation="vertical">
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_recommand"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="推荐" />
<Button
android:id="@+id/btn_me"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="我的" />
</LinearLayout>
</LinearLayout>
MainActivity:
package com.example.myapplication;
import static com.example.myapplication.R.id.btn_me;
import static com.example.myapplication.R.id.btn_recommand;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.example.myapplication.fragment.MeFragment;
import com.example.myapplication.fragment.RecommandFragment;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private final String TAG = "ysd";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(btn_me).setOnClickListener(this);
findViewById(btn_recommand).setOnClickListener(this);
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onPostResume() {
super.onPostResume();
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == btn_me) {
Fragment frag = new MeFragment();
Bundle bundle = new Bundle();
bundle.putString("startup", "这是me参数");
frag.setArguments(bundle);
replaceFragment(frag);
return;
}
if (id == btn_recommand) {
Fragment frag = new RecommandFragment();
Bundle bundle = new Bundle();
bundle.putString("startup", "这是recommand参数");
frag.setArguments(bundle);
replaceFragment(frag);
}
}
private void replaceFragment(Fragment frag) {
final FragmentManager supportFragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
// 该参数指定视图.
fragmentTransaction.replace(R.id.framelayout, frag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
个人页
布局:
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="个人页面"
android:textSize="36sp" />
</LinearLayout>
fragment:
package com.example.myapplication.fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class MeFragment extends Fragment {
private String startupArgument;
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (view == null) {
view = inflater.inflate(R.layout.me, null);
}
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
startupArgument = bundle.getString("startup");
Log.e("ysd", "onCreate: me 当前传递的参数是 => " + startupArgument);
}
}
}
推荐页
布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="推荐页面"
android:textSize="36sp" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment:
package com.example.myapplication.fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class RecommandFragment extends Fragment {
private String startupArgument;
private View view;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
startupArgument = bundle.getString("startup");
}
Log.e("ysd", "onCreate: 当前获得的参数是 " + startupArgument);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (view == null) {
view = inflater.inflate(R.layout.recommand, null);
}
return view;
}
}