2017-12-20 4 views
0

に私はUnbinderを持っていると)(アンバインドを呼び出し、このBaseFragmentでBaseFragmentはButterKnife.bind BaseFragment

public class BaseFragment extends Fragment { 

    protected Unbinder unbinder; 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     unbinder.unbind(); 
    } 
} 

を持って作成します。 onDestroyの後に。しかし子供で作成してください。フラグメント

@Override 
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.catigories_fragment, container, false); 
     unbinder = ButterKnife.bind(this, view); 
     init(); 
     return view; 
    } 

どのようにアンバインダーの作成をBaseFragmentに移動できますか?

答えて

1

どのようにアンバインダーの作成をBaseFragmentに移動できますか?

呼び出されたときにonCreateView私はむしろ、サブクラスにunbinderを初期化する責任をさせることとして、それを維持するだろうが、あなたが本当にそれを移動したいと場合onCreateViewの実装は、唯一欠けている部分を変更しません。情報は、異なるサブクラスが使用したいレイアウトのIDです(この場合はR.layout.catigories_fragment)。解決策は、あなたのBaseFragment

protected int getLayoutId() { 
    return 0; 
} 

にゲッターを宣言し、inflater.inflateのためにそれを使用することができます。例えば。

@Override 
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = null; 
    if (getLayoutId() != 0) { 
     view = inflater.inflate(getLayoutId(), container, false); 
     unbinder = ButterKnife.bind(this, view); 
     init(); 
    } 
    return view; 
} 

とonDestroy

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if (unbinder != null) { 
     unbinder.unbind(); 
    } 
} 
+0

私の事より簡単に利用、保護Unbinderのunbinder。各子供に作成する)Fragment))) – ip696

+0

うん、それは '私はUnbinderの作成をBaseFragmentに移すことができますか? 'と尋ねました。 – Blackbelt

+0

はい。もちろん、これはまさに私が尋ねたものです。答えをありがとう!答えの直後に私の実装は悪くないと決めました。アンバインダーの作成をベースフラグメントに移すことは価値があると思いますか? – ip696

関連する問題