零零碎碎的东西总是记不长久,仅仅学习别人的文章也只是他人咀嚼后留下的残渣。无意中发现了这个每日一道面试题,想了想如果只是简单地去思考,那么不仅会收效甚微,甚至难一点的题目自己可能都懒得去想,坚持不下来。所以不如把每一次的思考、理解以及别人的见解记录下来。不仅加深自己的理解,更要激励自己坚持下去。
参数传递
如果是在建立Fragment时需要Activity中的一些数据作为初始化,那么可以通过Activity中的setArguments(bundle)方法传递参数,在Fragment中通过getArguments获得Bundle对象来解析参数。
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Fragment firstFragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putString("data", "数据");
//显示Fragment
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment, firstFragment).show(firstFragment).commit();
}
}
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
Bundle bundle = getArguments();
String data = bundle.getString("data");
return view;
}
}
数据传递、事件监听
如果是创建后需要进行通信,那可以通过接口回调、广播、eventbus来进行。这些都可以是双向的,只是看你怎么设置。以下例子皆监听Fragment中的事件。
接口回调
在Fragment中
public class FirstFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
button = view.findViewById(R.id.bt_fragment);
button.setOnClickListener(v -> mListener.onFragmentInteraction("接口发送消息啦"));
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
//绑定Activity
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
//解绑activity时设为null
@Override
public void onDetach() {
super.onDetach();
mListener = null;
//自定义一个接口
public interface OnFragmentInteractionListener {
void onFragmentInteraction(String str);
}
}
在Activity中实现OnFragmentInteractionListener接口重写onFragmentInteraction方法就可以了
public class MainActivity extends BaseActivity implements FirstFragment.OnFragmentInteractionListener {
@Override
public void onFragmentInteraction(String str) {
Toast.makeText(this, "接口收到通知啦", Toast.LENGTH_SHORT).show();
}
}
广播
在Activity中注册广播
public class MainActivity extends BaseActivity implements FirstFragment.OnFragmentInteractionListener {
//处理事件
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action){
case BROADCAST_ACTION:
Toast.makeText(context, "收到通知啦", Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//注册广播
IntentFilter filter = new IntentFilter();
filter.addAction("com.goldenhamster.myboradcastreceiver");
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
//记得取消注册
unregisterReceiver(receiver);
}
}
在Fragment中发送广播
public class FirstFragment extends Fragment {
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
button = view.findViewById(R.id.bt_fragment);
button.setOnClickListener(v -> getActivity().sendBroadcast(new Intent("com.goldenhamster.myboradcastreceiver")));
return view;
}
}
EventBus框架
设置信息类
public class EventUtil {
private String mes;
public EventUtil(String mes){
this.mes = mes;
}
public String getMes() {
return mes;
}
}
在Activity中注册监听EventBus
public class MainActivity extends BaseActivity{
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
//使用注解标记事件处理方法
@Subscribe
public void onEvent(EventUtil event){
if(event.getMes().equals("data")){
Toast.makeText(context, "收到通知啦", Toast.LENGTH_SHORT).show();
}
}
}
在Fragment中发送消息
public class FirstFragment extends Fragment {
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
button = view.findViewById(R.id.bt_fragment);
button.setOnClickListener(v -> EventBus.getDefault().post(new EventUtil("data")));
return view;
}
}