如何为 Header 使用 Butter Knife 和为 ListView Android 使用页脚

How to use Butter Knife For Header And Footer for ListView Android

我将 Header 和页脚添加到我的列表视图中,如下所示:

lv = (ListView) findViewById(R.id.lv_process_person)
            View header = getLayoutInflater().inflate(R.layout.activity_input_forward_department_header, null);
            View footer = getLayoutInflater().inflate(R.layout.activity_input_forward_department_footer, null);
            lv.addHeaderView(header);
            lv.addFooterView(footer);

我在 header 中使用视图,如下所示:

btnForward = (TextView) header.findViewById(R.id.button_forward);
    btnForward.setText("SetText");

这些代码如何使用ButterKnife Liblary?我研究但没有结果。谢谢你,对不起我的英语。

您可以使用与 ButterKnife 文档中的示例类似的内容:

You can also perform binding on arbitrary objects by supplying your own view root.

public class FancyFragment extends Fragment {
  @BindView(R.id.button1) Button button1;
  @BindView(R.id.button2) Button button2;

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
  }
}

作为第一个参数传递给 ButterKnife.bind() 的 class 必须具有用 @BindView 注释的字段。最直接的方法是创建一个名为 ListViewHeader 的自定义 View class 或更适合您使用的名称。

这是我的 code.Use 带有静态内部 class 的视图持有者模式的一部分。

public class AudioOrderActivity extends AppCompatActivity {

    public static final String TAG = AudioOrderActivity.class.getSimpleName();
    @BindView(R2.id.ib_back)
    RelativeLayout ibBack;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.toolBar)
    RelativeLayout toolBar;
    private HeaderViews mHeaderViews;
    private FooterView mFooterViews;
    private View mHeader;
    private View mFooter;

    static class HeaderViews {
        ImageView ivPauseOrStart;
        @BindView(R2.id.iv_previous)
        ImageView ivPrevious;
        @BindView(R2.id.iv_next)
        ImageView ivNext;

        public HeaderViews(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class FooterView {
        @BindView(R2.id.tv_order_num)
        TextView tvOrderNum;
        @BindView(R2.id.tv_order_transaction_time)
        TextView tvOrderTransactionTime;

        public FooterView(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop_fragment_audioorder);
    }

    @Override
    protected void init() {
        mHeader = LayoutInflater.from(mContext).inflate(R.layout.shop_layout_head_audio_order_recycler, null);
        mFooter = LayoutInflater.from(mContext).inflate(R.layout.shop_layout_order_detail, null);
        mHeaderViews = new HeaderViews(mHeader);
        mFooterViews = new FooterView(mFooter);
        ButterKnife.bind(headerViews, header);
        ButterKnife.bind(footerViews, footer);
    }