1.base
BaseActivity
public abstract class BaseActivity>extends AppCompatActivity { protected P mPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPresenter=createPresenter(); mPresenter.attachView((V)this); } /** * activity/view会持有presenter层的引用,presenter会引用model层的引用。model加载的数据极有可能是网络数据,且极 * 有可能是异步子线程加载的数据。加载这样的数据要耗时,一旦用户点击回退推出了当前的view,则view就被释放了, * 但是当model层加载数据完成后会回掉监听器,他会拿着view的引用去访问一块已经被释放了的view内存。 * 这样是没有必要的,是浪费资源的,导致了内存的泄漏。 */ @Override protected void onDestroy() { super.onDestroy(); mPresenter.detachView(); } public abstract P createPresenter();}
BasePresenter
public abstract class BasePresenter{ protected WeakReference mViewRef;//当内存不足时释放内存 public void attachView(V view){ mViewRef=new WeakReference (view); } public void detachView(){ if(mViewRef!=null){ mViewRef.clear(); mViewRef=null; } } protected V getView(){ return mViewRef.get(); }}
2.Activity
UserActivity
public class UserActivity extends BaseActivityimplements UserView{ private ListView listview; private UserAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview= (ListView) findViewById(R.id.list_view); //入口 mPresenter.fetch(); } @Override public UserPresenter createPresenter() { return new UserPresenter(); } @Override public void showLoading() { Toast.makeText(getApplicationContext(),"正在加载...",Toast.LENGTH_LONG).show(); } @Override public void showDatas(List datas) { mAdapter=new UserAdapter(getApplicationContext(), datas); listview.setAdapter(mAdapter); }}
3.view
UserView
public interface UserView { void showLoading(); void showDatas(Listdatas);}
4.presenter
UserPresenter
public class UserPresenter extends BasePresenter{ UserModel userModel=new UserModelImpl(); public void fetch(){ getView().showLoading(); if(userModel!=null){ userModel.loadDatas(new UserModel.DataOnLoadingListener() { @Override public void onComplete(List datas) { getView().showDatas(datas); } }); } }}
5.model
UserModel
public interface UserModel { void loadDatas(DataOnLoadingListener listener); interface DataOnLoadingListener{ void onComplete(Listdatas); }}
UserModelImpl
public class UserModelImpl implements UserModel { @Override public void loadDatas(DataOnLoadingListener listener) { Listdatas= DataUtil.getInstance().getData(); listener.onComplete(datas); }}
6.其他的无关的
UserAdapter
public class UserAdapter extends BaseAdapter { private Context context; private Listdatas; public UserAdapter(Context context,List datas){ this.context=context; this.datas=datas; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView=View.inflate(context, R.layout.item, null); } ViewHolder holder=ViewHolder.getHolder(convertView); holder.tv.setText(datas.get(position).getName()); return convertView; } static class ViewHolder{ TextView tv; public ViewHolder(View convertView){ tv=(TextView) convertView.findViewById(R.id.textView); } public static ViewHolder getHolder(View convertView){ ViewHolder holder=(ViewHolder) convertView.getTag(); if(holder==null){ holder = new ViewHolder(convertView); convertView.setTag(holder); } return holder; } }}
User
public class User { private String name; public User(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
DataUtil
public class DataUtil { private static DataUtil dataUtil=null; public static DataUtil getInstance(){ if(dataUtil==null){ dataUtil=new DataUtil(); } return dataUtil; } public ListgetData(){ List datas = new ArrayList<>(); for (int i = 0; i < 20; i++) { User user = new User("" + i); datas.add(user); } return datas; }}