在BaseFragment中创建ButterKnife.bind

时间:2017-12-20 13:10:43

标签: android butterknife

我有BaseFragment

public class BaseFragment extends Fragment {

    protected Unbinder unbinder;


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

在这个BaseFragment中我有Unbinder并调用unbind();在onDestroy之后。但是在子Fragment中创建它

@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;
    }

如何将unbinder创建移动到BaseFragment?

2 个答案:

答案 0 :(得分:3)

  

如何将unbinder创建移动到BaseFragment?

我宁愿保持原样,让子类负责在调用unbinder时初始化onCreateView,但是如果你真的想移动它并且onCreateView的实现没有不要改变,唯一缺少的信息是布局的id(在这种情况下为R.layout.catigories_fragment)不同的子类想要使用。解决方案可能是在BaseFragment

中声明一个getter
 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();
    }
}

答案 1 :(得分:0)

这个问题已经解决了两年了,我在这里分享另一个解决方案。该解决方案的优势在于,您只需标记需要绑定的活动或片段,绑定/取消绑定就会自动发生。

1。创建一个Fragment基类并添加一些帮助器。

abstract class BaseFragment : Fragment() {
    var unbinder: Unbinder? = null

    fun bind() {
        unbind()
        unbinder = ButterKnife.bind(this, view!!)
    }

    fun unbind() {
        unbinder?.unbind()
        unbinder = null
    }
}
  1. 声明一个接口以标识要绑定的活动/片段。
/**
* Marks an activity / fragment needs to be bound by [ButterKnife].
*/
interface Bindable
  1. 创建一个对象并注册“活动/片段”生命周期回调。
/**
 * Helper class to automatically inject fragments if they implement [Bindable].
 */
object AppInjector {

    fun init(application: App) {
        application
            .registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
                override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                    handleActivity(activity)
                }

                override fun onActivityStarted(activity: Activity) {

                }

                override fun onActivityResumed(activity: Activity) {

                }

                override fun onActivityPaused(activity: Activity) {

                }

                override fun onActivityStopped(activity: Activity) {

                }

                override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {

                }

                override fun onActivityDestroyed(activity: Activity) {

                }
            })
    }

    private fun handleActivity(activity: Activity) {
        if (activity is FragmentActivity) {
            activity.supportFragmentManager
                .registerFragmentLifecycleCallbacks(
                    object : FragmentManager.FragmentLifecycleCallbacks() {

                        override fun onFragmentViewCreated(
                            fm: FragmentManager,
                            f: Fragment,
                            v: View,
                            savedInstanceState: Bundle?
                        ) {
                            if (f is BaseFragment && f is Bindable) {
                                f.bind()
                            }
                        }

                        override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) {
                            if (f is BaseFragment && f is Bindable) {
                                f.unbind()
                            }
                        }
                    }, true
                )
        }
    }
}
  1. 在您的Application子类中注册这些回调。
class App : Application() {

    override fun onCreate() {
        super.onCreate()

        AppInjector.init(this)
    }
}
  1. 标记您的片段可绑定,并照常使用Butterknife
class WelcomeFragment : BaseFragment(), Bindable {

    @BindView(R.id.fragment_welcome_loginButton)
    lateinit var loginButton: Button

    @BindView(R.id.fragment_welcome_emailSignUpButton)
    lateinit var emailSignUpButton: SignUpButton

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_welcome, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        loginButton.setOnClickListener(
            Navigation.createNavigateOnClickListener(R.id.action_welcomeFragment_to_loginFragment)
        )

        emailSignUpButton.setOnClickListener(
            Navigation.createNavigateOnClickListener(R.id.action_welcomeFragment_to_signUpFragment)
        )
    }

}

我没有为活动而实现它,因为我不需要它,您可以自己实现。