Butterknife as bindView from different views
in my project i have this case:
@BindView(R.id.viewpager)
ViewPager viewPager;
TabLayout tabLayout;
AddParkingFragmentListener listener;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_add_parking, container, false);
ButterKnife.bind(this, inflatedView);
tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setVisibility(View.VISIBLE);
return inflatedView;
}
where i need to bind the view which is in the layout activity_main.xml
. I thought I could use the interface to turn off visibility directly in MainActivity, but I would also know if there is an option to bind this view using Butterknife, because in MainActivity I also have this problem:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = navigationView.getHeaderView(0);
profile = (ImageView) view.findViewById(R.id.imgPro);
//this views are in the navigation view, how to bind using butterknife?
logo = (ImageView) view.findViewById(R.id.logo);
email = (TextView) findViewById(R.id.email_profile);
ButterKnife.bind(this);
}
Is there a way to do this, or do I need to use the findViewById () method?
thank
source to share
According to the official documentation from Butter Knife Library
They've included methods findById
that simplify code that still needs to find views on View
, Activity
or Dialog
. It uses generics to deduce the return type and does the listing automatically.
View view = LayoutInflater.from(context).inflate(R.layout.thing, null);
TextView firstName = ButterKnife.findById(view, R.id.first_name);
TextView lastName = ButterKnife.findById(view, R.id.last_name);
ImageView photo = ButterKnife.findById(view, R.id.photo);
Add static imports for ButterKnife.findById
and have even more fun.
source to share