mvp architecture of android, code directly

When I first came into contact with the structure of mvp, a lot of searches on the Internet were theoretical. It was a headache to knock on it. Then I watched a wave of videos. It felt like a bright future, but the feeling of the screen was still a bit inappropriate. After watching the source code of other people in git, I changed it again. So I have this version now.  
Open and dry

  • This is the general interface of V, that is, view.
public interface MvpView {
    public interface ManView{
        //Method of displaying dialog
        void showLoading();
        //Method of displaying data
        void showView(List<ManBean> picList);
    }
    public interface GirlView{
        void showLoading();
        void showView(List<GirlBean> picList);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • This is the master interface of M, that is, model.
public interface MvpModel {
    public interface ManModel{
        //Load data here
        void loadView(FinishLoadListener listener);
        //When the data is loaded, call back the method
        interface FinishLoadListener{
            void onComplete(List<ManBean> picList);
        }
    }
    public interface GirlModel{
        void loadView(FinishLoadListener listener);
        interface FinishLoadListener{
            void onComplete(List<GirlBean> girlBean);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Above is the interface we will implement later. Every method has comments. There should be no problem. Let's see how to implement it.

//The name is not very good. Don't get confused.
public class GirlRealizeModel implements MvpModel.GirlModel{

    @Override
    public void loadView(final FinishLoadListener listener) {

       //Here, the data of the network is loaded, and after loading, the interface of loading is called. The data loaded from the network is thrown in, and then processed by p of our mvp. Because the list data is written in the method of the parent class here, and the list data is returned here. We can also write it as a class. Generally, the json data is stored in the entity class.
       List<GirlBean> list=new ArrayList<>();
        listener.onComplete(list);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Okay, let's implement GirlView

public class GirlActivity extends Activity implements MvpView.GirlView{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scenery);
        //new goes out of our controller and calls the fetch method
        new GirlPresenter(this).fetch();
    }

    @Override
    public void showLoading() {
        //Call before loading data, display dialog boxes, all of them are here.
    }

    @Override
    public void showView(List<GirlBean> picList) {
        //Loaded data are here, we can add data to listview, then how to pass it here, look at our mvp controller p below.
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Write so much that our p Presenter hasn't appeared yet, and then it's time for him to exert control. With data, our persenter has to control how the data is displayed.

public class GirlPresenter {
    private MvpModel.GirlModel mModel=new GirlRealizeModel();
    private MvpView.GirlView mView;

    //Construction method
    public GirlPresenter(MvpView.GirlView view){
        mView=view;
    }

    //Here is the method for activity invocation. Here you can see that this persenter controls m loading data and v displaying different data.
    public void fetch(){
        mView.showLoading();
        mModel.loadView(new MvpModel.GirlModel.FinishLoadListener() {
            @Override
            public void onComplete(List<GirlBean> picList) {
                mView.showView(picList);
            }
        });
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

So far, our mvp code is finished. You will say that there is a man interface in the interface. I will not write it here. Let's try and impress you. Maybe you will say why we need to write multiple interfaces. One is not enough. At first, I wrote an interface. Later, I found that the data returned are not the same, some are entity classes, and some are list data. So I wrote a number of interfaces (we have better ways to inform Ha), when the landlord wrote a blog, he caught a bad cold, so I wrote pseudo-code (this is not an excuse), but to tell the truth, June cold, really feel shameful ah, we are also working at the same time to pay more attention to the body, after all, the body is the cost.

Links to the original text: http://blog.csdn.net/qq_35661441/article/details/73928560

Keywords: network git JSON

Added by mfallon on Sun, 19 May 2019 16:34:15 +0300