2014-09-02 32 views
1

我有以下的線性佈局如何以MvxLinearLayout的模板項目按鈕響應點擊

  <MvxLinearLayout 
       android:layout_alignParentTop="true" 
       android:layout_toLeftOf="@+id/add_skill" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:showDividers="middle" 
       android:divider="?android:dividerHorizontal" 
       local:MvxBind="ItemsSource Skills" 
       local:MvxItemTemplate="@layout/itemnameddelete" /> 

ItemNamedDelete.axml是:

<?xml version="1.0" encoding="utf-8"?> 
<MvxLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:local="http://schemas.android.com/apk/res-auto" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeightSmall" 
android:baselineAligned="false" 
android:showDividers="middle" 
android:divider="?android:dividerVertical" 
android:dividerPadding="8dp"> 
<TextView 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical" 
    android:textAppearance="?android:textAppearanceMedium" 
    android:text="Text" 
    local:MvxBind="Text ." /> 
<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:gravity="right" 
    style="?android:borderlessButtonStyle" 
    android:src="@android:drawable/ic_menu_delete" /> 

如何指定當用戶發生了什麼點擊ImageButton?我希望這個可重用,所以我希望我可以指定從MvxLinearLayout的點擊綁定。我嘗試了「Click MyCommand」,但是當我點擊TextView時會觸發。 ItemClick不起作用。

回答

0

我知道你可以做的是在android:id="@+id/YourID"之前在layout-XML文件中聲明一個id,並將其用作附加onClick()事件的參考。

下面是一個更詳細的例子:

<?xml version="1.0" encoding="utf-8"?> 
<MvxLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:local="http://schemas.android.com/apk/res-auto" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeightSmall" 
android:baselineAligned="false" 
android:showDividers="middle" 
android:divider="?android:dividerVertical" 
android:dividerPadding="8dp"> 
<TextView 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center_vertical" 
    android:textAppearance="?android:textAppearanceMedium" 
    android:text="Text" 
    local:MvxBind="Text ." /> 
<ImageButton 
    andorid:id="@+id/YourImageButton" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:gravity="right" 
    style="?android:borderlessButtonStyle" 
    android:src="@android:drawable/ic_menu_delete" /> 

,並在相關的Java activityClass的onCreated方法使用findViewById功能。

findViewById(R.id.YourImageButton).setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         //Here goes your code ... 
        } 
       }); 

而是一個類的匿名實例,你可以重用某些東西。那麼關係就是你定義的id。

更新當然

中ervery項目得到另一個ID,因此您的線性佈局有一個ID,你的圖像按鈕,你的TextView a.s.o. ...

+0

但是,當另一個項目添加到線性佈局時,這是如何工作的? – Jeremy 2014-09-02 20:04:04