2017-05-03 15 views
2

bindView我有这样的情况:Butterknife如何从我的项目不同的意见

@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; 
} 

,我需要绑定一个观点,即在activity_main.xml布局。 我以为我可以使用一个接口直接在MainActivity中禁用可见性,但我也知道是否有可能使用Butterknife绑定这个视图,因为在MainActivity中我也有这个问题:

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); 

} 

有没有办法做到这一点,或者我需要使用findViewById()方法?

感谢

回答

2

根据从Butter Knife Library

的公报文件它们包括findById方法,其简化仍然需要找到一个ViewActivity,或Dialog意见代码。它使用泛型来推断返回类型并自动执行演员。

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); 

ButterKnife.findById添加静态导入,享受更多乐趣。

来源:http://jakewharton.github.io/butterknife/

+0

所以我可以使用经典@BindView为他人的正常意见和ButterKnife.findById? – ste9206

+1

是:D就是这样! –

+0

太好了:D非常感谢你! – ste9206