1. 程式人生 > >MVP框架搭建(簡單)

MVP框架搭建(簡單)

View層

介面

import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

/**

  • 作者:xujiahui

  • 時間:2018/10/22

  • 作用:IDetegate(介面) */ public interface IDetegate { //初始資料 void initData();

    void create(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle);

    View rooyView();

    void initContext(Context context); }

實現類

import android.content.Context; import android.os.Bundle; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

import com.umeng.soexample.R;

/**

  • 作者:xujiahui

  • 時間:2018/10/22

  • 作用:AppIDetegate(實現類) */ public abstract class AppIDetegate implements IDetegate { private View rootView; private Context mcontext; private SparseArray views = new SparseArray<>(); //初始化資料 @Override public void initData() {

    } //獲取檢視 @Override public void create(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) { rootView = inflater.inflate(getLayout(), null, false); }

    protected abstract int getLayout(); //獲取控制元件 public T get(int id) { T view = (T) views.get(id); if (view == null) { view = rootView.findViewById(id); views.put(id, view); } return view; } //點選事件 public void setClick(View.OnClickListener clickListener, int… ids) { if (ids == null) { return; } for (int id : ids) { rootView.findViewById(id).setOnClickListener(clickListener); } }

    @Override public View rooyView() { return rootView; } //獲取上下文 @Override public void initContext(Context context) { this.mcontext = context; } }

BaseActivity(父類)

import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity;

import com.umeng.soexample.mvp.view.AppIDetegate;

/**

  • 作者:xujiahui

  • 時間:2018/10/22

  • 作用:BaseActivity(activity父類) */ public abstract class BaseActivity extends AppCompatActivity { private T appIDetegate;

    public BaseActivity() { try { appIDetegate = getlayout().newInstance(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } }

    protected abstract Class getlayout();

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); appIDetegate.create(getLayoutInflater(), null, savedInstanceState); appIDetegate.initContext(this); appIDetegate.initData(); setContentView(appIDetegate.rooyView()); } }

BaseFragment(父類)

import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

import com.umeng.soexample.mvp.view.AppIDetegate;

/**

  • 作者:xujiahui

  • 時間:2018/10/22

  • 作用:BaseFragment(Franment父類) */ public abstract class BaseFragment extends Fragment { private T appIDetegate;

    public BaseFragment() { try { appIDetegate = getlayout().newInstance(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (java.lang.InstantiationException e) { e.printStackTrace(); }

    }

    protected abstract Class getlayout();

    @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { appIDetegate.initContext(getActivity()); appIDetegate.create(getLayoutInflater(), null, savedInstanceState); return appIDetegate.rooyView(); }

    @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); appIDetegate.initData(); } }