如何从演示者访问Fragment Manager(或托管活动)

时间:2018-09-07 16:13:56

标签: android mvp android-mvp

我需要从演示者访问片段管理器(或活动)。我该如何访问?演示者本身是从另一个演示者中调用的,而另一个演示者又是从片段中调用的。如何从演示者访问片段管理器或托管活动?我需要实现一个接口吗?预先感谢。

public class EPGTitleRowPresenter extends Presenter {

private final static String TAG = EPGTitleRowPresenter.class.getSimpleName();
private Context mContext;

private static int sSelectedBackgroundColor;
private static int sDefaultBackgroundColor;

public EPGTitleRowPresenter(Context context) {
    mContext = context;

}

private View.OnFocusChangeListener mOnFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            updateEPGTitleRowBackgroundColor(v, true);
        }
        else {
            updateEPGTitleRowBackgroundColor(v, false);
        }
    }
};

private View.OnClickListener mOnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        VerticalGridView vgv = (VerticalGridView) v.getParent();
        int pos = vgv.getSelectedPosition();

        if (pos > 0) {
            //TODO: mark programme for auto-switch
        }
        else {
            //TODO: switch to channel

        }
    }
};

private static void updateEPGTitleRowBackgroundColor(View view, boolean selected) {
    int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
    // Both background colors should be set because the view's background is temporarily visible
    // during animations.
    view.setBackgroundColor(color);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    EPGTitleRowView view = new EPGTitleRowView(parent.getContext()) {
        @Override
        public void setSelected(boolean selected) {
            Log.i(TAG, "setSelected called");
            super.setSelected(selected);
        }
    };
    view.setMinimumWidth(parent.getWidth());

    sDefaultBackgroundColor =
            ContextCompat.getColor(parent.getContext(), android.R.color.transparent);

    sSelectedBackgroundColor =
            Color.parseColor("#66ffe680");

    view.setFocusable(true);
    view.setFocusableInTouchMode(true);
    updateEPGTitleRowBackgroundColor(view, false);

    view.setOnClickListener(mOnClickListener);
    view.setOnFocusChangeListener(mOnFocusChangeListener);

    return new ViewHolder(view);
}

@CallSuper
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
    final Programme prog = (Programme) item;
    ViewHolder vh = (ViewHolder) viewHolder;

    vh.mProgrammeTitle.setText(prog.getTitle().get(0));

    Date startDateTime = new Date(prog.getStart_utc() * 1000l);
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    String strStartTime = sdf.format(startDateTime);
    vh.mProgrammeStartTime.setText(strStartTime);
}


public class ViewHolder extends Presenter.ViewHolder {
    public EPGTitleRowView mRootView;
    public TextView mProgrammeTitle;
    public TextView mProgrammeStartTime;

    public ViewHolder(View view) {
        super(view);
        mRootView = (EPGTitleRowView) view;
        mProgrammeTitle = (TextView) view.findViewById(R.id.programme_title);
        mProgrammeStartTime = (TextView) view.findViewById(R.id.programme_start_time);
    }
}


public class EPGTitleRowView extends FrameLayout {

    public EPGTitleRowView(Context context) {
        super(context);

        setFocusable(true);
        setFocusableInTouchMode(true);

        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.epg_title_row, this);
    }
}

}

1 个答案:

答案 0 :(得分:1)

如果您正在使用演示者,那么我假设您正在使用MVP模式。在这种情况下,最好使代码尽可能远离Android组件(例如fragmentManager),以实现更好的分离。

我的建议是在View中调用一个方法(假设该视图是一个Activity),并且主持人必须知道该View,因此您可以使用该视图的接口(可能正在使用) )

类似这样的东西:

public interface MyView {
    void methodToUseFragmentManager();
}

演示者需要意识到这一点,因此看起来可能像这样:

MyView view:
public EPGTitleRowPresenter (MyView view){
    this.view = view;
}

void methodToUpdateView(){
     view.methodToUseFragmentManager();
}

最后在您的Activity上实现该接口:

public class MyActivity extends AppCompatActivity implements MyView{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new EPGTitleRowPresenter(this);
    }

    @Override
    void methodToUseFragmentManager(){
        // Here you can access the FragmentManager
    }
}

我还注意到,您的演示者获得了Context,以使如上所述的事物脱钩,这不是一个好主意。每当您需要访问上下文时,请在视图中调用方法

编辑

如果演示者超过1位,则假设您有2位演示者:parentPresenter和childPresenter,即孩子的EPGTitleRowPresenter

所以你父母的礼物应该像这样:

MyView view:
EPGTitleRowPresenter childPresenter;
public ParentPresenter (MyView view){
    this.view = view;
    childPresenter = new EPGTitleRowPresenter(view);
}

和孩子(EPGTitleRowPresenter)将与原始答案相同。

您的活动将如下所示:

public class MyActivity extends AppCompatActivity implements MyView{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new ParentPresenter(this);
    }

    @Override
    void methodToUseFragmentManager(){
        // Here you can access the FragmentManager
    }
}