公司几款app设置界面都有用户头像,功能一样:网络请求获取头像更新,点击头像弹出:拍照or相册,选择照片更新头像。
现在都是写在各自项目中,大量重复代码,现在需要重构,老大说把这个View写成通用控件(不是整个界面),但是点击View进入拍照或者相册,选择返回照片是在Activity的onActivityResult里处理,我在View里面无法重写这个方法啊 怎么弄???
阿神2017-04-18 10:49:49
Of course this question is about convenience, but we must consider the current framework of the App:
Use callbacks as mentioned above. But note that because the callbacks are referenced by static variables, generally the callbacks are anonymous inner classes, which may cause memory leaks.
Use event bus. It also implements the middle layer and transmits the results through the event bus.
Recommendation:
Since it is refactoring, you can consider allowing all controls to have the function of obtaining onActivityResult results. Define an interface,
interface onActivityResult {
boolean onResult();
}
Custom controls that need to listen for callbacks need to implement this interface. In BaseActivity, record these controls and pass them to these controls when results are returned:
class BaseActivity {
List<onActivityResult> resultViewList = new ArrayList();
void onCreate() {
ResultView view = ...;
resultViewList.add(view);
}
onActivityResult() {
for (onActivityResult view : resultViewList) {
boolean ret = view.onResult();
if (ret) {
break;
}
}
}
}
This completely solves the problem that the control needs to get the Activity result.
The above is a rough code. In fact, there are also considerations: the problem of repeated requestCode and all Views need to be saved.
怪我咯2017-04-18 10:49:49
What you said above is good. Define an interface and use interface callback to do it. For other common views, just implement this interface and you’re done
大家讲道理2017-04-18 10:49:49
Our approach is to write the photo taking and photo album as a Module, then add a click event to the component that sets the avatar, click to jump to the processing interface, and then return. The setting component is not included in the Module and is set by the user of the Module.
Another way is to use event monitoring as mentioned above. After selecting the album picture or taking a photo to confirm the picture, trigger this listener.
I suggest you ask your boss if you are sure to write the entrance View in the Module
伊谢尔伦2017-04-18 10:49:49
Build a local mavenwarehouse and encapsulate this control, pop-up window, and jump function. Then upload it directly to the local mavenwarehouse, and then depend on it to the project through Gradle
Build the local maven warehouse, configure Gradle, and upload your own library to the warehouse
Generally, this can be done for libraries with basic functions, and it can be maintained by one person
迷茫2017-04-18 10:49:49
Use onActivityResult
没问题呀,哔哩哔哩就是这样处理的 boxing;
不想让调用者处理 onActivityResult
,可以写个中间 Activity
处理完 onActivityResult
to pass the result to the caller through callback;
or customize a camera
黄舟2017-04-18 10:49:49
For this kind of logic that involves other interfaces, I usually write a separate module, use the interface when calling, the implementation layer uses an intermediate activity to complete the selection/processing of images, and finally returns through a callback.
You don’t even need to write these things yourself, there are a lot of ready-made wheels on GitHub, just find an ImagePicker and use it.
巴扎黑2017-04-18 10:49:49
Thank you everyone, this answer is basically what I mean. My question is not clear. My refactoring is not to create a universal avatar control. What I want is to put this avatar view into the layout of the business activity, and then the caller does not need to do anything else. What network requests to update the avatar, upload the avatar, select the photo...all are encapsulated, instead of calling back to him to handle it himself, because the avatar related interfaces of these apps in our company are common