点击与butterknife android的自定义项目

时间:2017-04-10 11:14:08

标签: android butterknife

我想将点击添加到我的自定义项目中:

public class ContactItem extends FrameLayout {

    @BindView(R.id.itemHeader)
    TextView textLabel;
    @BindView(R.id.itemValue)
    TextView textValue;
    @BindView(R.id.imageIcon)
    ImageView icon;
    @BindView(R.id.mainLayout)
    RelativeLayout mainLayout;

    private String label = null;

    public ContactItem(Context context) {
        super(context);

        init(context, null);
    }

    public ContactItem(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context, attrs);
    }

    public ContactItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs,
                    R.styleable.textLabel);

            label = a.getString(R.styleable.textLabel_text_attr);

        }

        addView(inflate(context, R.layout.item_contact, null));

        ButterKnife.bind(this);

    }

    public TextView getTextLabel() {
         return  textLabel;
    }

    public TextView getTextValue(){
        return textValue;
    }

    public ImageView getIcon(){
        return icon;
    }

    public void setTextLabelText(int text){
        textLabel.setText(text);
    }

    public void setTextValueText(String text){
        textValue.setText(text);
    }

    public void setIconRes(int res){
        icon.setImageResource(res);
    }

}

在我的活动中,我添加了几种此类型的物品。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/color_white">

        <TextView
            android:id="@+id/Header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingLeft="15dp"
            android:paddingTop="20dp"
            android:textAllCaps="true"
            android:textStyle="bold"
            android:background="@color/background"/>

        <views.items.ContactItem
            android:id="@+id/contactTelefon"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"/>
        <views.items.ContactItem
            android:id="@+id/contactEmail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"/>
    </LinearLayout>

现在,如果我点击项目contactEmail我想发送电子邮件,如果contactTelefon我想打电话。我有功能,但onClick不起作用。有什么想法吗?

@OnClick(R.id.contactTelefon)
public void clickOnCall(){
   presenter.callFunction());
}

2 个答案:

答案 0 :(得分:0)

我不确定这是ButterKnife的问题,但您可以尝试将android:duplicateParentState="true"添加到ContactItem。这背后的想法是,在子布局获得完全相同的事件之前,您的顶层布局正在消耗click事件。

另一种方法是将clicklistener添加到LinearLayout,例如

@OnClick(R.id.linearLayout)
public void clickOnCall(View v) {
    switch (v.getId()) {
    case R.id.contactTelefon:
       presenter.callFunction()); break;
    } 
}

答案 1 :(得分:0)

您应该在托管ButterKnife.bind()的父视图上执行ContactItem。自定义类中的bind()是不同的绑定。

因此,如果这是一项活动,请将ButterKnife.bind(this)放入该活动中,然后您的@OnClick()将按预期工作。

相关问题