第一步
创建fragment
public class TopFragemnt extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_fragment_layout, container, false);
return view;
}
public void showTopFragmentMethod() {
Toast.makeText(getActivity(), "TopFragemnt的方法", Toast.LENGTH_SHORT).show();
}
/**
* 在fragment中调用activity中的方法
*/
private void getActivityMethod() {
FragmentActivity activity = (FragmentActivity) getActivity();
activity.showActivityMethod();
}
}
public class BottomFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_fragment_layout, container, false);
return view;
}
}
public class AnotherBottomFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_bottom_fragment_layout, container, false);
return view;
}
}
第二步
创建activity加载fragment,可以通过在布局文件中添加,也可以通过代码动态添加,详细见代码
public class FragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
replaceFragment(new BottomFragment());
findViewById(R.id.change).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new AnotherBottomFragment());
getFragmentMethod();
}
});
}
private void replaceFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameLayout, fragment);
transaction.addToBackStack(null);//在碎片中模拟返回栈。点击回退键时,回到上一个fragment不直接退出activity
transaction.commit();
}
/**
* 在activity中调用fragment的方法
*/
private void getFragmentMethod() {
TopFragemnt topFragemnt = (TopFragemnt) getSupportFragmentManager().findFragmentById(R.id.top_fragment);
topFragemnt.showTopFragmentMethod();
}
public void showActivityMethod() {
Toast.makeText(this, "FragmentActivity 方法", Toast.LENGTH_SHORT).show();
}
/**
* 不同fragment间的交互,
* 通过在一个fragment中先拿到activity的实例,然后在这个activity的实例中
* 提供另一个fragment的实例。
* @return
*/
public BottomFragment returnBottomFragment() {
BottomFragment bottomFragment = new BottomFragment();
return bottomFragment;
}
}
activity布局文件
<?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=".Three_chapter.FragmentActivity">
<fragment
android:id="@+id/top_fragment"
android:name="com.cecurs.helloandroid.Three_chapter.TopFragemnt"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
网友评论