1

Библиотека ButterKnife уже подключена к проекту и работает, но как подключить к этому фрагменту.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    inflater.inflate(R.layout.profession_list_fragment, null);

    ButterKnife.bind(this, ???);

    serviceIntent = new Intent(getActivity(), ServiceLoad.class);
    setConnection();

    loadData ();

    return super.onCreateView(inflater, container, savedInstanceState);
}

Зарание спасибо.

1 ответ 1

3

Выдержка с документации

Fragments have a different view lifecycle than activities. When binding a fragment in onCreateView, set the views to null in onDestroyView. Butter Knife returns an Unbinder instance when you call bind to do this for you. Call its unbind method in the appropriate lifecycle callback.

public class FancyFragment extends Fragment {
  @BindView(R.id.button1) Button button1;
  @BindView(R.id.button2) Button button2;
  private Unbinder unbinder;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    unbinder = ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
  }

  @Override public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }
}
3
  • Это когда мы возвращаем view, спасибо я умею читать, Вы могли заметить это по знакам вопроса здесь .bind(this, ???). Но как Вы видите у меня в коде я не view возвращаю. 11 фев 2018 в 20:19
  • Почему не возвращается view? Но если вы точно уверены, что именно так и нужно, то есть как минимум 2 варианта: 1) View view = super.onCreateView(inflater, container, savedInstanceState); ButterKnife.bind(this, view); return view; 2) Код из ответа использовать в BaseFragment, если у вас используется наследование для данного класса 11 фев 2018 в 20:21
  • Не знал что так можно View view = super... Спасибо. 11 фев 2018 в 20:24

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.