Simple MVP design
1. First of all, I configured the MyApplication Context in order to facilitate call remember need to configure the name in the list node android: name = “. The application. The MyApplication. “”
public class MyApplication extends Application { public static Context MainContext; @Override public void onCreate() { super.onCreate(); MainContext = getApplicationContext(); }}Copy the code
2. Create an Activity that inherits FragmentActivity and emulates data
package com.oblivion.mvp_fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.os.Bundle; import com.oblivion.mvp_fragment.fragment.FirstFragment; import java.util.ArrayList; import java.util.List; public class MainActivity extends FragmentActivity { private List<Integer> mList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initFragment(); } /** * private void initData() {mList = new ArrayList<>(); for (int i = 0; i < 100; i++) { mList.add(i); }} / initialization fragments * * * * / private void initFragment () {FragmentManager manager = getSupportFragmentManager (); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.fl_main, FirstFragment.newInstance(mList)); transaction.commit(); }}Copy the code
3. The Adapter base classes
package com.oblivion.mvp_fragment.adapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.oblivion.mvp_fragment.holder.BasicHolder;
import com.oblivion.mvp_fragment.holder.ViewHolder;
import java.util.List;
/**
* Created by oblivion on 2016/11/18.
*/
public abstract class BasicAdapter<T> extends BaseAdapter {
private List<T> mList;
public BasicAdapter(List<T> mList) {
this.mList = mList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BasicHolder holder;
if (convertView == null) {
holder = initHolder();
convertView = holder.holderView;
convertView.setTag(holder);
} else {
holder = (BasicHolder) convertView.getTag();
}
holder.bindData(position);
return convertView;
}
public abstract BasicHolder initHolder();
}
Copy the code
4.Adapter base class inheritance
package com.oblivion.mvp_fragment.adapter; import android.app.Activity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.TextView; import com.oblivion.mvp_fragment.R; import com.oblivion.mvp_fragment.holder.BasicHolder; import com.oblivion.mvp_fragment.holder.ViewHolder; import java.util.List; /** * Created by oblivion on 2016/11/18. */ public class FirstFragmentAdapter extends BasicAdapter { protected List<Integer> mLists; public FirstFragmentAdapter(List<Integer> mLists) { super(mLists); this.mLists = mLists; } @Override public BasicHolder initHolder() { return new ViewHolder(mLists); }}Copy the code
5. Holder of the base class
package com.oblivion.mvp_fragment.holder;
import android.view.View;
import android.widget.TextView;
import com.oblivion.mvp_fragment.R;
import com.oblivion.mvp_fragment.application.MyApplication;
import java.util.List;
/**
* Created by oblivion on 2016/11/18.
*/
public abstract class BasicHolder<T> {
public View holderView;
public List<T> mLists;
public BasicHolder(List<T> mLists) {
this.mLists = mLists;
holderView = getInflateView();
}
public abstract View getInflateView();
public abstract void bindData(int position);
}
Copy the code
6. Implement ViewHolder for the Holder base class
package com.oblivion.mvp_fragment.holder; import android.view.View; import android.widget.TextView; import com.oblivion.mvp_fragment.R; import com.oblivion.mvp_fragment.application.MyApplication; import java.util.List; /** * Created by oblivion on 2016/11/18. */ public class ViewHolder extends BasicHolder<Integer> { public TextView tv; public ViewHolder(List<Integer> mLists) { super(mLists); } @Override public View getInflateView() { View view = View.inflate(MyApplication.MainContext, R.layout.item_firstfragment, null); tv = (TextView) view.findViewById(R.id.tv_item); return view; } @Override public void bindData(int position) { tv.setText(mLists.get(position) + ""); }}Copy the code
7.FirstFragment
package com.oblivion.mvp_fragment.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import com.oblivion.mvp_fragment.R; import com.oblivion.mvp_fragment.adapter.FirstFragmentAdapter; import com.oblivion.mvp_fragment.presenter.Presenter; import com.oblivion.mvp_fragment.view.IShowView; import java.util.List; /** * Created by oblivion on 2016/11/18. */ public class FirstFragment extends Fragment implements IShowView { private static List<Integer> mLists; private Presenter presenter; private ListView listView; public static Fragment newInstance(List<Integer> mList) { mLists = mList; return new FirstFragment(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_first, null); listView = (ListView) view.findViewById(R.id.lv_first); presenter = new Presenter(this); presenter.doLogic(); return view; } @override public void showNext() {} @override public void showSuccess(int data) {// mLists. Add (data); listView.setAdapter(new FirstFragmentAdapter(mLists)); } @Override public void showError() { } }Copy the code
8. Network connection interface Model
package com.oblivion.mvp_fragment.model;
/**
* Created by oblivion on 2016/11/18.
*/
public interface IModelGetData {
String getData();
}
Copy the code
9. Interface implementation of Model
package com.oblivion.mvp_fragment.model.impl; import android.os.SystemClock; import com.oblivion.mvp_fragment.model.IModelGetData; /** * Created by oblivion on 2016/11/18. */ public class IModelGetDataImpl implements IModelGetData { @Override public String getData() { SystemClock.sleep(2000); Return "Flirt, don't disturb..." ; }}Copy the code
10. The View of the interface
package com.oblivion.mvp_fragment.view; /** * Created by oblivion on 2016/11/18. */ public interface IShowView {void showNext(); void showSuccess(int data); void showError(); }Copy the code
11. Logical implementation of Presenter
package com.oblivion.mvp_fragment.presenter; import com.oblivion.mvp_fragment.model.IModelGetData; import com.oblivion.mvp_fragment.model.impl.IModelGetDataImpl; import com.oblivion.mvp_fragment.view.IShowView; /** * Created by oblivion on 2016/11/18. */ public class Presenter { private IModelGetData modelGetData; private IShowView showView; public Presenter(IShowView showView) { this.showView = showView; modelGetData = new IModelGetDataImpl(); } public void doLogic() { String data = modelGetData.getData(); if (data ! = null && data.length() > 0) { showView.showSuccess(1000); }}}Copy the code