Using ButterKnife library with 2 different views in 1 fragment

痴心易碎 提交于 2019-12-03 15:58:21
Krylez

The simple solution is to put all your footer views in a view holder class and then perform the injections on different objects, while keeping the footer views easily accessible within the fragment.

@InjectView(R.id.users_listview_id) AutoUpdatingListView list;

private View footerView;
public class FooterViewHolder {
    @InjectView(R.id.add_user_footer_firstname_edittext_id) BootstrapEditText firstnameEditText;
    @InjectView(R.id.add_user_footer_middlename_edittext_id) BootstrapEditText middlenameEditText;
    @InjectView(R.id.add_user_footer_lastname_edittext_id) BootstrapEditText lastnameEditText;
    @InjectView(R.id.add_user_footer_cancel_button_id) BootstrapButton cancelButton;
    @InjectView(R.id.add_user_footer_okay_button_id) BootstrapButton okayButton;
    @InjectView(R.id.add_user_footer_facebook_button) BootstrapCircleThumbnail facebookButton;
    @InjectView(R.id.add_user_footer_google_plus_button) BootstrapCircleThumbnail googlePlusButton;
    @InjectView(R.id.add_user_footer_twitter_button) BootstrapCircleThumbnail twitterButton;
    @InjectView(R.id.add_user_footer_linkedin_button) BootstrapCircleThumbnail linkedInButton;

}
private FooterViewHolder footerViewHolder;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.user_fragment, container, false);
    footerView = inflater.inflate(R.layout.add_user_footer_view, container, false);
    footerViewHolder = new FooterViewHolder();

    ButterKnife.inject(this, rootView);
    ButterKnife.inject(footerViewHolder, footerView);

    list.setAdapter(adapter);

    return rootView;
}

BTW, it's considered bad practice to inflate views with a null parent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!