如何使用butterknife绑定动态视图

时间:2016-04-26 13:36:35

标签: android fragment butterknife

我正在使用父片段的布局在android和我的片段中创建多个动态视图,在子片段中我正在创建动态视图,但我无法使用Butterknife绑定这些动态视图。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
this is the on create 
// Inflate the layout for this fragment
        View view = super.onCreateView(inflater, container, savedInstanceState); this.container.addView(inflater.inflate(R.layout.overall_sport_performance_row, null), 5);

我在overall_sport_performance布局中有一个textview,我想使用butterknife

1 个答案:

答案 0 :(得分:0)

我不太了解您的解决方案,如果您的片段视图为overall_spot_performance.xml,请执行以下操作:

@BindView(R.id.your_text_view_id)
protected TextView textView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = View.inflate(getContext(), R.layout.overall_sport_performance_row, null);
    Butterknife.bind(this, view);

    return view;
}

无需动态绑定TextView。但是,如果仍需要动态绑定它,则可以使用findById方法。我没有测试它,但应该这样工作:

View dynamicView = inflater.inflate(R.layout.overall_sport_performance_row, null);
this.container.addView(dynamicView, 5);

TextView textView = ButterKnife.findById(dynamicView, R.id.your_text_view_id);