Using ButterKnife library with 2 different views in 1 fragment

折月煮酒 提交于 2019-12-05 00:13:19

问题


I'm currently trying to use the ButterKnife library for Android to handle some boilerplate code (https://github.com/JakeWharton/butterknife)

I've got it set up, and got it semi-working, but i've run into a problem i can't fix :/

I have a Fragment which contains 1 single ListView element, and to this ListView i'm attaching a footer view which contains several elements.

This is a code snippet from my fragment using the library:

public class UsersFragment extends Fragment {
    @InjectView(R.id.users_listview_id) AutoUpdatingListView list;

    private View footerView;
    @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;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.user_fragment, container, false);
    footerView = ((LayoutInflater) getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.add_user_footer_view, null, false);

    ButterKnife.inject(this, rootView);

    list.setAdapter(adapter);
    }
}

The above code does NOT work, as i can only inject with 1 view, and if i try with both of them (2x inject's), the second inject overrides the first one, leaving me with null values.

I can't seem to specify which view contains which elements like i can with the regular findViewById().

To clarify, this is my non-ButterKnife code (which works 100%):

    list = (AutoUpdatingListView) rootView.findViewById(R.id.users_listview_id);
    firstnameEditText = (BootstrapEditText) footerView.findViewById(R.id.add_user_footer_firstname_edittext_id);
    middlenameEditText = (BootstrapEditText) footerView.findViewById(R.id.add_user_footer_middlename_edittext_id);
    lastnameEditText = (BootstrapEditText) footerView.findViewById(R.id.add_user_footer_lastname_edittext_id);

As you can see above, i can specify either "rootView" or "footerView" as the target of the findViewById.

Anyone able to help out with this issue? As it's driving me nuts at the moment..


回答1:


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.



来源:https://stackoverflow.com/questions/24921876/using-butterknife-library-with-2-different-views-in-1-fragment

工具导航Map

JSON相关