2017-05-03 48 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