前言
Fragment与Fragment主要用接口回调的方式进行交互,简单介绍一下本文的实现流程。首先一个MainActivity,两个Fragment(HomeFragment,MyFragment),两个Fragment依附与MainActivity,点击按钮要将HomeFragment输入框里的值展示在MyFragment页面:
1.定义一个接口类 FragmentListener
2.在MainActivity实现接口
3.在HomeFragment调用接口,传入EditText的输入内容
4.在MainActivity接收到值,调用MyFragment方法,传入值
5.在MyFragment展示传过来的值
先上效果
定义一个接口类 FragmentListener
interface FragmentListener {
void setText(String text);
}
Activity代码
<?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:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/main_viewpage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/main_tablayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements FragmentListener {
private TabLayout mTabLayout;
private ViewPager mViewPage;
private ArrayList<String> mList = new ArrayList<>();
private ArrayList<Fragment> mFragmentList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabLayout = findViewById(R.id.main_tablayout);
mViewPage = findViewById(R.id.main_viewpage);
//添加标题
mList.add("首页");
mList.add("我的");
mFragmentList.add(new HomeFragment());
mFragmentList.add(new MyFragment());
//设置ViewPage适配器
mViewPage.setAdapter(new MyViewPageAdapter(getSupportFragmentManager()));
//设置Tablayout联动
mTabLayout.setupWithViewPager(mViewPage);
}
@Override
public void setText(String text) {
//获取MyFragment
MyFragment fragment = (MyFragment) mFragmentList.get(1);
//调用MyFragment中的setText传值
if (text != null) {
fragment.getText(text);
}
}
//TabLayout适配器
private class MyViewPageAdapter extends FragmentStatePagerAdapter {
public MyViewPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mList.get(position);
}
}
}
HomeFragment代码
<?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:orientation="vertical"
tools:context=".HomeFragment">
<EditText
android:id="@+id/home_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:layout_marginRight="15dp" />
<Button
android:id="@+id/home_bt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
android:text="发送" />
</LinearLayout>
public class HomeFragment extends Fragment {
private FragmentListener listener;
private View inflate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
inflate = inflater.inflate(R.layout.fragment_home, container, false);
//初始化控件
Button mBt = inflate.findViewById(R.id.home_bt);
EditText mEdit = inflate.findViewById(R.id.home_edit);
//点击按钮获取输入框内容调用接口方法传值
mBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = mEdit.getText().toString().trim();
if (listener != null && text != null) {
listener.setText(text);
}
}
});
return inflate;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
listener = (FragmentListener) context;
}
}
MyFragment代码
<?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:orientation="vertical"
tools:context=".MyFragment">
<TextView
android:id="@+id/my_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="接受值"
android:textColor="@color/black"
android:textSize="16sp" />
</LinearLayout>
public class MyFragment extends Fragment {
private View inflate;
private TextView mText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
inflate = inflater.inflate(R.layout.fragment_my, container, false);
//初始化控件
mText = inflate.findViewById(R.id.my_text);
return inflate;
}
//获取到HomeFragment输入框的值赋值
public void getText(String text) {
mText.setText(text);
}
}