声明:本文并非原创,整理于网络上的公开课内容
/** * 描述:主要用来初始化view 和 返回当前的view * 作者 mjd * 日期:2015/12/6 19:56 */
public interface Vu {
/** * 初始化view */
void init(LayoutInflater inflater, ViewGroup container);
/** * 返回一个根view */
View getView();
}
/** * 描述:主要实现当前的view * 作者 mjd * 日期:2015/12/6 20:08 */
public class MainVu implements Vu {
public View view;
public TextView textView;
@Override
public void init(LayoutInflater inflater, ViewGroup container) {
view = inflater.inflate(R.layout.activity_main, container, false);
textView = (TextView) view.findViewById(R.id.text_view);
}
@Override
public View getView() {
return view;
}
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 19:53 */
public abstract class BasePresenterActivity<V extends Vu> extends Activity {
protected V v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
v = (V) getVuClass().newInstance();
//初始化view
v.init(getLayoutInflater(), null);
setContentView(v.getView());
//绑定数据
onBindVu();
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 返回当前的view的对象 */
protected abstract Class<V> getVuClass();
/** * 绑定数据 */
protected void onBindVu() {
}
}
public class MainActivity extends BasePresenterActivity<MainVu> {
@Override
protected Class<MainVu> getVuClass() {
return MainVu.class;
}
@Override
protected void onBindVu() {
super.onBindVu();
v.textView.setText("MVP设计思想");
}
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 19:53 */
public abstract class BasePresenterActivity<V extends Vu> extends Activity {
protected V v;
protected FragmentManager fm;
protected EventBus bus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
fm = getFragmentManager();
//获取到eventBus 主要用来解耦
bus = EventBus.getDefault();
v = getVuClass().newInstance();
//初始化view
v.init(getLayoutInflater(), null);
setContentView(v.getView());
//绑定数据
onBindVu();
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 绑定数据 */
protected void onBindVu() {
}
/** * 返回当前的view的对象 */
protected abstract Class<V> getVuClass();
@Override
protected final void onResume() {
afterOnResume();
super.onResume();
}
@Override
protected final void onPause() {
beforeOnPause();
super.onPause();
}
public void afterOnResume() {
}
public void beforeOnPause() {
}
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 21:00 */
public abstract class BasePresenterFragment<V extends Vu> extends Fragment {
protected V v;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = null;
try {
v = (V) getVuClass().newInstance();
//初始化view
v.init(inflater, container);
//绑定数据
onBindVu();
view = v.getView();
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
@Override
public void onDestroyView() {
onDestroyVu();
v = null;
super.onDestroyView();
}
public void onDestroyVu() {
}
/** * 绑定view */
public void onBindVu() {
}
/** * 获取到view的对象 */
public abstract Class<V> getVuClass();
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 20:41 */
public abstract class BasePresenterAdapter<V extends Vu> extends BaseAdapter {
protected V v;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//首先获取到打气筒
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
v = (V) getVuClass().newInstance();
//初始化view
v.init(inflater, parent);
//获取到当前的view
convertView = v.getView();
//设置tag
convertView.setTag(v);
} catch (Exception e) {
e.printStackTrace();
}
} else {
v = (V) convertView.getTag();
}
if (convertView != null) {
//绑定数据
onBindListItem(position);
}
return convertView;
}
protected abstract void onBindListItem(int position);
protected abstract Class<V> getVuClass();
}
public class MainActivity extends BasePresenterActivity<MainVu> {
@Override
protected Class<MainVu> getVuClass() {
return MainVu.class;
}
@Override
protected void onBindVu() {
super.onBindVu();
fm.beginTransaction().replace(v.getContentId(), InPusListFragment.newInstance()).commit();
}
/** * 处理业务逻辑 */
@Override
public void afterOnResume() {
super.afterOnResume();
}
/** * 处理业务逻辑 */
@Override
public void beforeOnPause() {
super.beforeOnPause();
}
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 21:11 */
public class InPusListFragment extends BasePresenterFragment<InPusListVu> {
@Override
public Class<InPusListVu> getVuClass() {
return InPusListVu.class;
}
@Override
public void onBindVu() {
super.onBindVu();
v.setListAdapter(adapter);
v.selectCallBack(selectCallBack);
}
/** * 实例化当前对象 */
public static InPusListFragment newInstance() {
return new InPusListFragment();
}
InPusListAdapter adapter = new InPusListAdapter();
VuCallBack<Integer> selectCallBack = new VuCallBack<Integer>() {
@Override
public void execute(Integer result) {
Toast.makeText(getActivity(), adapter.getTitle(result), 0).show();
}
};
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 21:37 */
public class InPusListVu implements Vu {
public View view;
public ListView listView;
public VuCallBack<Integer> selectCallBack;
@Override
public void init(LayoutInflater inflater, ViewGroup container) {
view = inflater.inflate(R.layout.list_view, container, false);
listView = (ListView) view.findViewById(R.id.list_view);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (selectCallBack != null) {
selectCallBack.execute(position);
}
}
});
}
@Override
public View getView() {
return view;
}
public void setListAdapter(ListAdapter adapter) {
listView.setAdapter(adapter);
}
public void selectCallBack(VuCallBack<Integer> selectCallBack) {
this.selectCallBack = selectCallBack;
}
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 21:48 */
public class InPusListAdapter extends BasePresenterAdapter<InPusItemVu> {
//为了方便,这里的数据在bean里面静态添加了
List<String> titles = new ArrayList<String>(Ipus.VALUES_MAP.keySet());
@Override
protected void onBindListItem(int position) {
String title = titles.get(position);
v.setTitle(title);
}
@Override
protected Class<InPusItemVu> getVuClass() {
return InPusItemVu.class;
}
@Override
public int getCount() {
return titles.size();
}
@Override
public Object getItem(int position) {
return titles.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public String getTitle(int position) {
return (String) getItem(position);
}
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 22:14 */
public class InPusItemVu implements Vu {
public View view;
public TextView textView;
@Override
public void init(LayoutInflater inflater, ViewGroup container) {
view = inflater.inflate(R.layout.ipus_item, container, false);
textView = (TextView) view.findViewById(R.id.title);
}
@Override
public View getView() {
return view;
}
public void setTitle(String title) {
textView.setText(title);
}
}
/** * 描述: * 作者 mjd * 日期:2015/12/6 21:44 */
public interface VuCallBack<T> {
void execute(T result);
}