我目前正在尝试使用Android的ButterKnife库来处理一些样板代码(https://github.com/JakeWharton/butterknife)
我已经设置好了,并让它半工作,但我遇到了一个我无法解决的问题:/
我有一个片段,其中包含1个单独的ListView元素,对于这个ListView,我附加了一个包含多个元素的页脚视图。
这是我的片段中使用该库的代码片段:
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);
}
}
上面的代码不起作用,因为我只能注入1个视图,如果我尝试同时使用它们(2x注入),第二个注入将覆盖第一个,留下我的空值。
我似乎无法指定哪个视图包含我可以使用常规findViewById()的哪些元素。
澄清一下,这是我的非ButterKnife代码(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);
如上所示,我可以指定“rootView”或“footerView”作为findViewById的目标。
任何人都可以帮忙解决这个问题吗?因为它让我疯狂......
答案 0 :(得分:15)
简单的解决方案是将所有页脚视图放在视图持有者类中,然后对不同的对象执行注入,同时保持片段内的页脚视图易于访问。
@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;
}
顺便说一下,使用空父级来扩充视图是considered bad practice。