Home  >  Q&A  >  body text

java - android代码重构:如何把app设置里的头像UI做成通用的?

公司几款app设置界面都有用户头像,功能一样:网络请求获取头像更新,点击头像弹出:拍照or相册,选择照片更新头像。

现在都是写在各自项目中,大量重复代码,现在需要重构,老大说把这个View写成通用控件(不是整个界面),但是点击View进入拍照或者相册,选择返回照片是在Activity的onActivityResult里处理,我在View里面无法重写这个方法啊 怎么弄???

高洛峰高洛峰2744 days ago630

reply all(8)I'll reply

  • 阿神

    阿神2017-04-18 10:49:49

    Of course this question is about convenience, but we must consider the current framework of the App:

    1. 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.

    2. Use event bus. It also implements the middle layer and transmits the results through the event bus.

    3. 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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:49:49

    Just process it in the form of listener.

    reply
    0
  • 怪我咯

    怪我咯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

    reply
    0
  • 大家讲道理

    大家讲道理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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦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

    reply
    0
  • 迷茫

    迷茫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

    reply
    0
  • 黄舟

    黄舟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.

    reply
    0
  • 巴扎黑

    巴扎黑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

    reply
    0
  • Cancelreply