開發一個好專案:八、建立view

duanhao發表於2021-09-09

前一篇文章: 框架地址 希望大家能多多star一下

接著上一篇文章繼續
建立一個View(view有兩種,目前可知道的是Fragment和Activity)繼承
目前View 有兩個基礎類,當然我也在這篇文章裡提過了 連結
基礎類裡有幾個方法是公共用的,我就提出去了。

建立Fragment的View類 繼承 TBaseFragment 實現 HomeContract.View
工廠方法

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

show()和hidden() 是 fragment 隱藏顯示時候呼叫的方法
onAttach是初始化判斷當前的activity容器是否實現了OnFragmentInteractionListener方法
setPresenter將mPresenter賦值

程式碼如下

public class HomeFragment extends TBaseFragment implements HomeContract.View{

    public HomeFragment() {

    }

    HomeContract.Presenter mPresenter;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int layoutViewId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    MatchAdapter matchAdapter;
    RecyclerView home_recycle;
    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        ActivityUtils.getFragment(getChildFragmentManager(),R.id.home_user, UserTitleFragment.newInstance());
        home_recycle = findView(R.id.home_recycle);
        home_recycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        matchAdapter = new MatchAdapter(getActivity(),new ArrayList());
        home_recycle.setAdapter(matchAdapter);
        mPresenter.getMatch();
    }

    @Override
    protected void show() {
        SFont sFont = setRightButtonFont(R.string.plus);
        setTitle(getResources().getString(R.string.takkyuu_home));

        sFont.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddActivity.start(getActivity());
            }
        });
    }

    @Override
    protected void hidden() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void showMatch(List matchModels) {
        matchAdapter.addCommentModel(matchModels);
    }

    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        super.setPresenter(presenter);
        mPresenter = presenter;
    }
}

如何使用這個fragment呢?
在你的actvivity裡用ActivityUtils 這個是dileber框架裡的載入fragment方法
如下程式碼

HomeFragment homeFragment = ActivityUtils.getFragment(getSupportFragmentManager(),R.id.homeFrame,HomeFragment.newInstance());
        new HomePresenter(homeFragment,SysRepository.getInstance());

activity的View,基礎類也是在 連結

繼承 TBaseActivity 實現
start() 跳轉當前activity的方法
UToolBar 的使用在之前的文章介紹過 連結

presenter = new LoginPresenter(this, UserRepository.getInstance());
則是使用LoginPresenter的方法

public class LoginActivity extends TBaseActivity implements LoginContract.View,View.OnClickListener{

    public static void start(Context context,Intent intent){
        if(intent == null){
            intent = new Intent();
        }
        intent.setClass(context,LoginActivity.class);
        context.startActivity(intent);
    }

    EditText login_name,login_pass;
    LoginContract.Presenter mPresenter;

    @Override
    protected void startView(Bundle savedInstanceState) {

    }

    @Override
    protected int layoutViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        presenter = new LoginPresenter(this, UserRepository.getInstance());

        UToolBar uToolBar = new UToolBar();
        uToolBar.setTitleId(R.string.takkyuu_login);
        uToolBar.setNeedNavigate(false);
        setToolBar(R.id.toolbar,uToolBar);
        login_name = getView(R.id.login_name);
        login_pass = getView(R.id.login_pass);
        UUi.setOnClickListener(this,getView(R.id.login_login),getView(R.id.login_register));

    }

    @Override
    public void toHome() {
        HomeActivity.start(this);
        finish();
    }

    @Override
    public UserModel getUser(){
        UserModel userModel = new UserModel();
        userModel.setUsername(login_name.getText().toString().trim());
        userModel.setUserpass(login_pass.getText().toString().trim());
        return userModel;
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter)  {
        mPresenter = presenter;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getUser();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_login:
                mPresenter.checkUser();
                break;
            case R.id.login_register:
                RegisterActivity.start(LoginActivity.this);
                break;
        }

    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1343/viewspace-2798251/,如需轉載,請註明出處,否則將追究法律責任。

相關文章