Android--MVP设计思想

声明:本文并非原创,整理于网络上的公开课内容

简单的例子

1. 定义一个Vu接口

  • 用来初始化view和返回view
/** * 描述:主要用来初始化view 和 返回当前的view * 作者 mjd * 日期:2015/12/6 19:56 */
public interface Vu {
    /** * 初始化view */
    void init(LayoutInflater inflater, ViewGroup container);
    /** * 返回一个根view */
    View getView();
}

2. 具体view的实现

/** * 描述:主要实现当前的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;
    }

}

3. BasePresenterActivity的编写

/** * 描述: * 作者 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() {

    }


}

4. 具体MainActivity的实现

public class MainActivity extends BasePresenterActivity<MainVu> {

    @Override
    protected Class<MainVu> getVuClass() {
        return MainVu.class;
    }

    @Override
    protected void onBindVu() {
        super.onBindVu();
        v.textView.setText("MVP设计思想");
    }

}

项目架构

  • activity的基类定义
  • fragment的基类定义
  • adapter的基类定义

BasePresenterActivity

/** * 描述: * 作者 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() {
    }
}

BasePresenterFragment

/** * 描述: * 作者 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();

}

BasePresenterAdapter

/** * 描述: * 作者 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();
}

项目实现

  • 主页面是FrameLayout布局,里面放一个Fragment
  • fragment中放一个ListView
  • listview的item放一个textView

MainActivity的实现

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();

    }
}

InPusListFragment的实现

/** * 描述: * 作者 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();
        }
    };

}

InPusListVu的实现

/** * 描述: * 作者 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;
    }

}

InPusListAdapter的实现

/** * 描述: * 作者 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);
    }


}

InPusItemVu的实现

/** * 描述: * 作者 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);
    }

}

VuCallBack的实现(点击item时的回调)

/** * 描述: * 作者 mjd * 日期:2015/12/6 21:44 */
public interface VuCallBack<T> {

    void execute(T result);
}

你可能感兴趣的:(mvp设计思想)