我使用mvp
体系结构。
我有setupViews()方法,其中定义了按钮和...
我如何在{presenter“中的Button中使用setOnclickListener
,并在” activity“中的SetupViews
中调用它。
我不想在“视图”中直接使用它,我想告诉演示者单击已完成,演示者将完成工作。
我的界面:
public interface HomeContract {
interface View extends BaseView {
void showNews(List<News> newsList);
void showError(String error);
}
interface Presenter extends BasePresenter<View> {
void getNewsList();
} }
演示者类:
public class HomePresenter implements HomeContract.Presenter {
private HomeContract.View view;
private NewsDataSourse newsDataSourse;
CompositeDisposable compositeDisposable = new CompositeDisposable();
public HomePresenter(NewsDataSourse newsDataSourse) {
this.newsDataSourse = newsDataSourse;
}
@Override
public void getNewsList() {
newsDataSourse.getNews().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<News>>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onSuccess(List<News> news) {
view.showNews(news);
}
@Override
public void onError(Throwable e) {
view.showError(e.toString());
}
});
}
@Override
public void attachView(HomeContract.View view) {
this.view = view;
getNewsList();
}
@Override
public void detachView() {
this.view = null;
if (compositeDisposable != null && compositeDisposable.size() > 0) {
compositeDisposable.clear();
}
}}
我的观点:
public class HomeActivity extends BaseActivity implements HomeContract.View {
private HomeContract.Presenter presenter;
private NewsRepository newsRepository = new NewsRepository();
private RecyclerView recyclerView;
private RecyclerAdapter recyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
presenter = new HomePresenter(newsRepository);
}
@Override
public void setupViews() {
Button btn_Ok = (Button) findViewById(R.id.btn_Ok);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getViewContext(), LinearLayout.VERTICAL, false));
// btn_Ok.setOnClickListener();
}
@Override
public void showNews(List<News> newsList) {
setupViews();
recyclerView.setAdapter(new RecyclerAdapter(newsList, getViewContext()));
if (newsList.size() > 0) {
Toast.makeText(getViewContext(), "ok", Toast.LENGTH_SHORT).show();
Toast.makeText(getViewContext(), newsList.get(0).getName(), Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getViewContext(), "not Ok", Toast.LENGTH_SHORT).show();
}
@Override
public void showError(String error) {
Toast.makeText(getViewContext(), error, Toast.LENGTH_SHORT).show();
}
@Override
public Context getViewContext() {
return getApplicationContext();
}
@Override
protected void onStart() {
super.onStart();
presenter.attachView(this);
}
@Override
protected void onStop() {
super.onStop();
presenter.detachView();
}
}
我想在视野中使用btn_Ok.setOnClickListener(presenter.
... );
或需要其他更好的解决方案
答案 0 :(得分:0)
在演示者界面中输入方法:
interface Presenter extends BasePresenter<View> {
void getNewsList();
void onButtonClicked();
}
比在您的HomePresenter
类中使用它:
@Override
public void onButtonClicked(){
// continue logic here
}
如果不覆盖它,则该类将显示错误