Android MVP架构标准,用于加载具有Android资源的Model类的UI

前端之家收集整理的这篇文章主要介绍了Android MVP架构标准,用于加载具有Android资源的Model类的UI前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在应用程序中跟随MVP架构.我的HomeActivity包含一个滑动面板,列表图标具有选择器,在选择滑动面板项目时,图标状态已更改,我没有使用任何列表选择器.

I am keeping a model class NavItemData for populating the
navigation drawer and using a class SlidingPanelItemSelector that
extends StateListDrawable generates the appropriate selector for the
sliding panel icon.

在MVP架构中,我们有一个演示者类与模型进行通信,并生成视图的输入.在我的情况下,如果我使用演示者获取Sliding Panel的数据,我正在从演示者调用一个类,使用android上下文这是一个很好的方法,或者我们有任何替代解决方案严格遵循MVP架构?

Currently i am using a ViewBinderUtils class
and injected it directly to the activity class and gets the list of
data for Sliding Panel. Is it following Mvp Architcture?

SlidingPanelItemSelector.class

public class SlidingPanelItemSelector extends StateListDrawable {
    private Context mContext;

    public SlidingPanelItemSelector(Context mContext){
        this.mContext = mContext;
    }

    public StateListDrawable getHomeSelector(){
    StateListDrawable stateListDrawable = new StateListDrawable();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            stateListDrawable.addState(new int[]{android.R.attr.state_pressed},mContext.getDrawable(R.drawable.ic_nav_home_active));
            stateListDrawable.addState(new int[]{},mContext.getDrawable(R.drawable.ic_nav_home));
        }else{
            stateListDrawable.addState(new int[]{android.R.attr.state_pressed},mContext.getResources().getDrawable(R.drawable.ic_nav_home_active));
            stateListDrawable.addState(new int[]{},mContext.getResources().getDrawable(R.drawable.ic_nav_home));
        }

        return stateListDrawable;
    }
}

ViewBinderUtils.class

public class ViewDataBinderUtils {
    Context mContext;
    @Inject
    public ViewDataBinderUtils(@ActivityContext Context mContext) {
        this.mContext = mContext;
    }
    public List<SlidingPanelData> getListData(String [] titles){
        List<SlidingPanelData> items = new ArrayList<>();
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getHomeSelector(),titles[0],true));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getConfigurationSelector(),titles[1],false    ));
    items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getConfigurationSelector(),titles[2],false));
        items.add(new SlidingPanelData(true));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getQuoteSelector(),titles[3],false));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getEquipmentInventorySelector(),titles[4],false));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getCustomerSelector(),titles[5],false));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getQuoterSelector(),titles[6],false));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getSalesProgramsSelector(),titles[7],false));
        items.add(new SlidingPanelData( new SlidingPanelItemSelector(mContext).getCreditAppsSelector(),titles[8],false));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getRetailOffeRSSelector(),titles[9],false));
        items.add(new SlidingPanelData(true));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getPayOffeRSSelector(),titles[10],true));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getAlertsSelector(),titles[11],true));
        items.add(new SlidingPanelData(true));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getTermofUseSelector(),titles[12],false));
        items.add(new SlidingPanelData(new SlidingPanelItemSelector(mContext).getLegalInfoSelector(),false));
        return items;
    }
}

解决方法

演示者应与上下文的东西隔离,因为应该知道的上下文的唯一部分是View(V)部分.
我不太了解这个课程的目标,但是在通用的方式你应该遵循这个逻辑

if am using the presenter for getting the data for Sliding Panel i am
calling a class from presenter that using android context

创建一个负责管理View(V)与Presenter(P)之间的通信的接口.

Communication.java

public interface Communication {
    void showLoading();
    void hideLoading();
    void setSlidingData(String [] titles);
}

你的视图应该实现这个接口通信,并且有一个Presenter的参考.如果您需要使用Interactor(I)的上下文,则应该有一个管理此类的类(在我的情况下为RequestHolder).

View.java

public View implements Communication{
    private Presenter mPresenter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
           // your view already implements the Comunication interface
           mPresenter = new Presenter(this);
        }
        (...)
       private void getData(){
         mPresenter.getData(new RequestHolder(getApplicationContext()));
       }

       @Override
       public void setSlidingData(String [] titles){
          List<SlidingPanelData> items = new ArrayList<>();
          items.add(new SlidingPanelData(new SlidingPanelItemSelector(getApplicationContext()).getHomeSelector(),true));
       }
    }

在演示者有你的界面的参考

Presenter.java

private Communication mView;

public Presenter(Communication view) {
    mView = view;
}

/** event to receive data from the model **/
public void onEvent(GetDataMessage event){
   mView.setSlidingData(event.getData());
} 

public void getData(final RequestHolder holder){
   new GetDataInteractor(holder);
}

RequestHolder.java

// you can put the data important to the interactor
public RequestHolder(final Context context,int accountId) {
    super(context); 
}
//getters and setters

通过这种方式,您可以访问交互器中的上下文,而不需要混合概念

总共,

> View是唯一提供上下文的视图
>视图引用了Presenter
>演示者“与会者”与界面之间的视图

在具体情况下,为什么不创建需要在View部分中的上下文的列表,并填充List items = new ArrayList?();在你的主持人的一部分?与此你保持一切孤立

猜你在找的Android相关文章