架构师成长之路-基于android fragment通信的面向…
2018-07-03 01:03:08来源:博客园 阅读 ()
前言
一 常用的Activity和Fragment几种通信方式
二 万能接口
public abstract class Function { /** 方法名 */ public String mFunctionName ; public Function (String funName){ this.mFunctionName = funName; } }
2 建立无(有)参数无(有)四类
/** * 无参无返回值 * <h3>Description</h3> * TODO * <h3>Author</h3> luzhenbang * <h3>Date</h3> 2018/1/4 16:13 * <h3>Copyright</h3> Copyright (c)2018 Shenzhen TL Co., Ltd. Inc. All rights reserved. */ public abstract class FunctionNoParamNoResult extends Function { public FunctionNoParamNoResult(String funName) { super(funName); } public abstract void function(); }
/** * 有参无返回值 * <h3>Description</h3> * TODO * <h3>Author</h3> luzhenbang * <h3>Date</h3> 2018/1/4 16:13 * <h3>Copyright</h3> Copyright (c)2018 Shenzhen TL Co., Ltd. Inc. All rights reserved. */ public abstract class FunctionWithParamOnly<Param> extends Function { public FunctionWithParamOnly(String funName) { super(funName); } public abstract void function(Param param); }
/** * 有参有返回值 * <h3>Description</h3> * TODO * <h3>Author</h3> luzhenbang * <h3>Date</h3> 2018/1/4 16:13 * <h3>Copyright</h3> Copyright (c)2018 Shenzhen TL Co., Ltd. Inc. All rights reserved. */ public abstract class FunctionWithParamWithResult<Result,Param> extends Function { public FunctionWithParamWithResult(String funName) { super(funName); } public abstract Result function(Param param); }
/** * 无参有返回值 * <h3>Description</h3> * TODO * <h3>Author</h3> luzhenbang * <h3>Date</h3> 2018/1/4 16:13 * <h3>Copyright</h3> Copyright (c)2018 Shenzhen TL Co., Ltd. Inc. All rights reserved. */ public abstract class FunctionWithResultOnly<Result> extends Function { public FunctionWithResultOnly(String funName) { super(funName); } public abstract Result function(); }
3 建立管理器类
public class FunctionManager { private static FunctionManager instance = null; public static final String TAG = FunctionManager.class.getSimpleName() + "-------->"; /**容器,用来存储方法名字 key 对应的方法名 value 对应的是 参数返回对象*/ private HashMap<String,FunctionWithParamWithResult> mFunctionWithParamWithResultHashMap = null; private HashMap<String,FunctionWithParamOnly> mFunctionWithParamsOnlyHashMap = null; private HashMap<String,FunctionWithResultOnly> mFunctionWithResultOnlyHashMap = null; private HashMap<String,FunctionNoParamNoResult> mFunctionNoParamNoResultHashMap = null; private FunctionManager() { mFunctionNoParamNoResultHashMap = new HashMap<>(); mFunctionWithParamWithResultHashMap = new HashMap<>(); mFunctionWithParamsOnlyHashMap = new HashMap<>(); mFunctionWithResultOnlyHashMap = new HashMap<>(); } public static FunctionManager getInstance() { if (null == instance){ instance = new FunctionManager(); } return instance; } /** * 添加无参无返回值的方法 * @return */ public FunctionManager addFunction(FunctionNoParamNoResult function){ mFunctionNoParamNoResultHashMap.put(function.mFunctionName,function); return this; } /** * 添加有返回值的方法 * @return */ public FunctionManager addFunction(FunctionWithResultOnly function){ mFunctionWithResultOnlyHashMap.put(function.mFunctionName,function); return this; } /** * 添加有参数的方法 * @return */ public FunctionManager addFunction(FunctionWithParamOnly function){ mFunctionWithParamsOnlyHashMap.put(function.mFunctionName,function); return this; } /** * 添加有参有返回值的方法 * @return */ public FunctionManager addFunction(FunctionWithParamWithResult function){ mFunctionWithParamWithResultHashMap.put(function.mFunctionName,function); return this; } /** * 调用无返回值无参数的方法 * @param funName */ public void invokeNoAll (String funName) throws NullPointerException { if (TextUtils.isEmpty(funName)){ Log.e(TAG,"funName is null !"); }else { if ( null != mFunctionNoParamNoResultHashMap){ FunctionNoParamNoResult function = mFunctionNoParamNoResultHashMap.get(funName); if (null != function){ function.function(); }else { Log.e(TAG,"function is null !"); } }else { throw new NullPointerException("mFunctionNoParamNoResultHashMap can not be null ,please first init FunctionManager !"); } } } /** * 调用有参数的方法 * @param funName */ public <Param> void invokeWithParamOnly (String funName,Param param) throws NullPointerException { if (TextUtils.isEmpty(funName)){ Log.e(TAG,"funName is null !"); }else { if ( null != mFunctionWithParamsOnlyHashMap){ FunctionWithParamOnly<Param> function = mFunctionWithParamsOnlyHashMap.get(funName); if (null != function){ function.function(param); }else { Log.e(TAG,"function is null !"); } }else { throw new NullPointerException("mFunctionWithParamsOnlyHashMap can not be null ,please first init FunctionManager !"); } } } /** * 调用有返回值的方法 * @param funName */ public <Result> Result invokeWithResultOnly (String funName, Class<Result> c) throws NullPointerException { if (TextUtils.isEmpty(funName)){ Log.e(TAG,"funName is null !"); }else { if ( null != mFunctionWithResultOnlyHashMap){ FunctionWithResultOnly function = mFunctionWithResultOnlyHashMap.get(funName); if (null != function){ if ( null != c){ return c.cast(function.function()); }else { return (Result) function.function(); } }else { Log.e(TAG,"function is null !"); } }else { throw new NullPointerException("mFunctionWithParamsOnlyHashMap can not be null ,please first init FunctionManager !"); } } return null; } /** * 调用有参数有返回值的方法 * @param funName */ public <Result,Param> Result invokeWithAll (String funName, Class<Result> c,Param param) throws NullPointerException { if (TextUtils.isEmpty(funName)){ Log.e(TAG,"funName is null !"); }else { if ( null != mFunctionWithParamWithResultHashMap){ FunctionWithParamWithResult<Result,Param> function = mFunctionWithParamWithResultHashMap.get(funName); if (null != function){ if ( null != c){ return c.cast(function.function(param)); }else { return function.function(param); } }else { Log.e(TAG,"function is null !"); } }else { throw new NullPointerException("mFunctionWithParamsOnlyHashMap can not be null ,please first init FunctionManager !"); } } return null; } }
4 在Activity写一个方法
public void setFunctionForFragment(String tag){ if (TextUtils.isEmpty(tag)){ Log.e(MainActivity.class.getSimpleName(),"tag is null !"); return; } BaseFragment fragment = (BaseFragment) fm.findFragmentByTag(tag); FunctionManager functionManager = FunctionManager.getInstance(); functionManager.addFunction(new FunctionNoParamNoResult(FunctionNoParamNoResult) { @Override public void function() { Toast.makeText(MainActivity.this, "无参无返回值", Toast.LENGTH_SHORT).show(); } }); functionManager.addFunction(new FunctionWithResultOnly<String>(FunctionWithResultOnly) { @Override public String function() { return "无参有返回值"; } }); functionManager.addFunction(new FunctionWithParamOnly<String>(FunctionWithParamOnly) { @Override public void function(String o) { Toast.makeText(MainActivity.this, o, Toast.LENGTH_SHORT).show(); } }); functionManager.addFunction(new FunctionWithParamWithResult<String,String>(FunctionWithParamWithResult) { @Override public String function(String o) { return o; } }); fragment.setFunctionManager(functionManager); }
5 编辑一个Fragment基类(实用Fragment的时候直接集成该类就可以了),并绑定上面建立的接口
public class BaseFragment extends Fragment{ public FunctionManager mFunctionManager; private MainActivity mainActivity; public void setFunctionManager(FunctionManager mFunctionManager) { this.mFunctionManager = mFunctionManager; } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof MainActivity) { mainActivity = (MainActivity) context; mainActivity.setFunctionForFragment(getTag()); } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mainActivity = null; } }
6 继承BaseFragment
public class NoParamNoResultFragment extends BaseFragment { private Handler mHandler; public NoParamNoResultFragment(Handler handler) { // Required empty public constructor this.mHandler = handler; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_no_param_no_result, container, false); view.findViewById(R.id.txt_handle).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Message message = mHandler.obtainMessage(); message.what = 0x123; message.obj = "handle 通信"; mHandler.sendMessage(message); } }); view.findViewById(R.id.txt_noALl).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mFunctionManager.invokeNoAll(MainActivity.FunctionNoParamNoResult); } }); view.findViewById(R.id.txt_result).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // mFunctionManager.invokeNoAll(MainActivity.FunctionNoParamNoResult); String result = mFunctionManager.invokeWithResultOnly(MainActivity.FunctionWithResultOnly,String.class); Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show(); } }); view.findViewById(R.id.txt_param).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mFunctionManager.invokeWithParamOnly(MainActivity.FunctionWithParamOnly,"有参无返回值"); } }); view.findViewById(R.id.txt_withAll).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String result = mFunctionManager.invokeWithAll(MainActivity.FunctionWithParamWithResult,String.class,"有参有返回值"); Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show(); } }); return view; } }
说明,上述在点击控件的时候,会触发在Activity添加的方法,这样就可以实现Activity与Fragment实时通信,近而实现Fragment之间的通信
https://gitee.com/lzbgit/AppInterface
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:用java数组模拟登录和注册功能
- 老板让你抗住千万级流量,如何做架构设计? 2020-06-11
- 聊聊微服务架构及分布式事务解决方案! 2020-06-10
- 来看看阿里架构师Java 代码打日志姿势!你也是这样写的吗 2020-06-08
- 【从单体架构到分布式架构】(二)请求增多,单点变集群(1) 2020-06-07
- 好兄弟仅用3年,就做到了架构师的位置,真心羡慕!经验分享 2020-06-06
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash