前一篇文章:开发一个好项目:七、创建Contract类,连接presenter和view 框架地址 dileber android框架 希望大家能多多star一下
接着上一篇文章继续
创建一个View(view有两种,目前可知道的是Fragment和Activity)继承
目前View 有两个基础类,当然我也在这篇文章里提过了 链接android开发(如何开发一个可以维护的好项目):一 、定义基类
基础类里有几个方法是公共用的,我就提出去了。
创建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<MatchModel>());
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<MatchModel> 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,基础类也是在 链接android开发(如何开发一个可以维护的好项目):一 、定义基类
继承 TBaseActivity 实现
start() 跳转当前activity的方法
UToolBar 的使用在之前的文章介绍过 链接 开发一个好项目:二、actvity简便跳转,创建菜单按钮,正确使用fragment
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;
}
}
}
热门评论
赞自己