【问题标题】:Pass Attributes before CustomView inflation in Fragment based on holder Activity在基于持有者Activity的Fragment中在CustomView膨胀之前传递属性
【发布时间】:2015-05-25 14:49:24
【问题描述】:

我有一个片段,其中包含布局中的自定义/复合视图。

<app.view.ControlLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/controlLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/fragment_control_size"
    android:layout_marginBottom="@dimen/offset_distance"
    custom:horizontal="true" /> 

Fragment 已附加到布局中的不同活动,例如:

<fragment
        android:id="@+id/controlLayout"
        android:name="app.controllers.ControlFragment"
        android:layout_width="match_parent"
        android:layout_height="@dimen/fragment_control_size"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="@dimen/offset_distance"
        tools:layout="@layout/fragment_control" />

自定义视图具有以下属性:

<resources>
    <declare-styleable name="ControlLayout">
        <attr name="horizontal" format="boolean" />
        <attr name="callBtnVisibility" format="boolean" />
    </declare-styleable>
</resources>

根据我的控制片段附加到哪个 Activity,我想将 callBtnVisibility 属性传递给我的自定义视图。一种解决方案是在代码中处理登录而不使用属性:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_control, container, false);

        if (getActivity() instanceof MyActivity) {
            ((ControlLayout) view.findViewById(R.id.controlLayout)).hideCallButton();
        } 

我想知道我是否可以在不使用以下登录的情况下以某种方式管理它。这样在控件布局在片段中膨胀之前,我可以将属性传递给它,并处理自定义视图的构造函数或init方法中的逻辑:

public ControlLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {

        final TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.ControlLayout,
                0, defStyle);

        final boolean horizontal = a.getBoolean(R.styleable.ControlLayout_horizontal, false);
        final boolean callButtonIsVisible = a.getBoolean(
                R.styleable.ControlLayout_callBtnVisibility, true);

        a.recycle();

        if(callButtonIsVisible) {
            mCallButton = new Button(getContext());
            mCallButton.setId(R.id.button_call);
            ...

【问题讨论】:

  • 你可以将defStyle传递给构造函数,将它分配给一个成员变量,然后检查它在onCreateView()中的值。
  • 我应该在哪里通过它? ControlLayout 是一个在 Fragment 中膨胀的 customView。
  • 看来你可以把它传入每个使用ControlLayout的Fragment的构造函数中,然后根据传入的值调用hideCallButton(),或者不调用。
  • 片段构造函数不会被调用。正如我在问题 Fragment is part of Activity layout 中解释的那样。
  • 啊,好的。抱歉,我没听懂您的问题。

标签: android android-fragments android-activity android-custom-view


【解决方案1】:

在onAttach()之前调用的片段生命周期中的onInflated可以解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多