与Butterknife绑定以在android中动态添加视图 [英] Bind with Butterknife to dynamically added view in android

查看:81
本文介绍了与Butterknife绑定以在android中动态添加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何绑定Layout中存在的视图,该视图通过ButterKnife动态添加到父视图中.

How Can I bind the views present inside the Layout which is dynamically added to the parent view with ButterKnife.

我有一个LinearLayout之类的容器.我有一个自定义布局,其中包含两个按钮,称此布局为 childview 在活动中,我已将 childview 成功添加到父级LinearLayout 容器.

I have a LinearLayout say container. And I have a custom layout which contains two buttons say this layout as childview In activity I added the childview successfully to the parent LinearLayout container.

这就是我如何膨胀自定义视图并将其添加到LinearLayout中的方法

This is how I did to inflate the custom view and added to the LinearLayout

bubbleView = inflater.inflate(R.layout.child, null);
systemChatLayoutContainer.addView(bubbleView);

现在,我想绑定布局中存在的Button视图并在布局内的按钮出现时添加执行一些操作.

Now I want to bind the Button views present inside the child layout and add perform some action when the buttons present inside the child layout.

这是child.xml,可在单击按钮时动态添加到父容器中.

This is child.xml which is dynamically added to the parent container on Button click.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/btnCreateAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_selector_green"
        android:gravity="center"
        android:padding="@dimen/_13sdp"
        android:text="Create an account"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/btnJstCheckingRate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_selector_blue"
        android:gravity="center"
        android:padding="@dimen/_13sdp"
        android:text="I'm just checking rates"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/btnIhaveAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/transparent_btn_selector"
        android:gravity="center"
        android:padding="@dimen/_20sdp"
        android:text="I've got an account"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        android:textColor="@color/white" />
</LinearLayout>

推荐答案

您可以使用 ViewHolder 将视图与子布局中存在的Butterknife绑定,因此添加内部类 BubbleViewHolder

You can bind views with ButterKnife present inside the child layout using ViewHolder, so add the inner class BubbleViewHolder

class BubbleViewHolder {
    BubbleViewHolder(View view) {
        ButterKnife.bind(this, view);
    }

    @OnClick(R.id.button_id)
    void onMyButtonClicked(Button myButton) {
        // Do your stuff here
    }
}

膨胀后构造 BubbleViewHolder

View bubbleView = inflater.inflate(R.layout.child, null);
new BubbleViewHolder(bubbleView);
systemChatLayoutContainer.addView(bubbleView);

这篇关于与Butterknife绑定以在android中动态添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆