在 BaseFragment 中创建 ButterKnife.bind

Create ButterKnife.bind in BaseFragment

我有 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;
    }

如何将解绑定创建移动到 BaseFragment?

How can I move unbinder creation to BaseFragment?

我宁愿保持原样,让子类负责在调用onCreateView时初始化unbinder,但如果你真的想移动它并实现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();
    }
}

这个问题已经过去两年了,我在这里分享另一个解决方案。这种方案的好处是你只需要标记需要绑定的activity或者fragment,绑定/解绑会自动进行。

1.Create 一个 Fragment 基础 class 并添加一些助手。

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

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

    fun unbind() {
        unbinder?.unbind()
        unbinder = null
    }
}
  1. 声明一个接口来识别要绑定的Activity/片段。
/**
* Marks an activity / fragment needs to be bound by [ButterKnife].
*/
interface Bindable
  1. 创建对象并注册 Activity/Fragment 生命周期回调。
/**
 * 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 subclass.
  2. 中注册这些回调
class App : Application() {

    override fun onCreate() {
        super.onCreate()

        AppInjector.init(this)
    }
}
  1. 标记您的 Fragment 是可绑定的并照常使用 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)
        )
    }

}

我没有为activity实现这个,因为我不需要它,你可以自己实现它。