如何在android中的MPV中使用“onClickListener”?

问题描述 投票:0回答:1

我使用mvp架构。

我有setupViews()方法,其中按钮和...定义如何在“演示者”中使用setOnclickListener for Button,并在“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. ... );或者你需要做任何更好的解决方案

android mvp
1个回答
0
投票

在您的演示者界面中键入一个方法:

interface Presenter extends BasePresenter<View> {

void getNewsList();

void onButtonClicked();

}

而且在你的HomePresenter课程中使用它:

@Override
public void onButtonClicked(){
// continue logic here 
}

如果您不覆盖它,该类将显示错误

© www.soinside.com 2019 - 2024. All rights reserved.