2016-08-25 22 views
3

自分のフラグメントにAppCompatSpinnerを使用しましたが、私のレイアウトにsetOnItemSelectedListener()を使用します。私はここでandroidデータバインディングのカスタムXML属性

https://developer.android.com/topic/libraries/data-binding/index.html?hl=en#custom_setters

からチュートリアルセクションを使用しようとしましたが、それは、単純なアクションを行うための完全な例を提供していません。そして、私もここ

android databinding in custom controls

から答えを探して、私はまだ焦げ茶色がそれを行う方法を理解しています。 私はXML属性に存在していないいくつかの属性と結合する単純なカスタムを行うための完全な例を持っていると思いますが、それはUIコントロールここ

に有用であることはここで

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:apps="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
    > 

    <data> 

     <import type="android.view.View"/> 

     <variable 
      name="handler" 
      type="com.my.OldHandlerInterface"/> 
    </data> 

    <merge 
     tools:showIn="@layout/fragment_stock_replacement"> 


     <android.support.v7.widget.CardView 
      android:id="@+id/exist_eqpt_card" 
      style="@style/sccardview" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.29" 
      android:visibility="@{oldObj.updateOld_mode ? View.VISIBLE : View.GONE}" 
      > 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="10dp" 
       android:orientation="vertical"> 

       <android.support.v7.widget.AppCompatSpinner 
        android:id="@+id/spn_status" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="true" 
        android:layout_below="@+id/chk_installed" 
        apps:adapter="@{statusAdapter}"/> 
      </RelativeLayout> 

     </android.support.v7.widget.CardView> 
     <!--</LinearLayout>--> 

    </merge> 
</layout> 

がある私のxmlです私のフラグメント

public class ReplacementFragment extends QRScanFragment { 
    ../ 
    @BindingAdapter("app:setOnItemSelectedListener") 
    public static void setOnItemSelectedListener(AppCompatSpinner view, int pos) { 
     //do sth 
    } 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     binding = DataBindingUtil.inflate(inflater, R.layout.binding, container, false); 
     String[] status = new String[]{"Spare", "Lost", "Damage", "Faulty"}; 
     statusAdapter = new StatusAdapter(getActivity(), status); 
     binding.setHandler(new Handler()); 
     View view = binding.getRoot(); 
     AppCompatSpinner lAppCompatSpinner = (AppCompatSpinner) view.findViewById(R.id.spn_status); 
     lAppCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      } 
     } 
    } 
} 

答えて

7

あなたはOnItemSelectedListenerに割り当てる特別なことをする必要はありません。

<android.support.v7.widget.AppCompatSpinner 
    android:id="@+id/spn_status" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/chk_installed" 
    android:onItemSelectedListener="@{myItemSelectedListener}" 
    apps:adapter="@{statusAdapter}"/> 

上記はのレイアウトのmyItemSelectedListener変数を前提としています。

あなただけonItemSelectedまたはonNothingSelectedを使用したい場合、あなたはすでにあなたのレイアウトで属性を使用することができます。

<android.support.v7.widget.AppCompatSpinner 
    android:id="@+id/spn_status" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/chk_installed" 
    android:onItemSelected="@{handler::onItemSelected}" 
    apps:adapter="@{statusAdapter}"/> 

これはhandlerのクラスにメソッドを前提としています

public class Handler { 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     //... 
    } 
} 

あなたは

android:onItemSelected="@{(p, v, pos, id) -> handler.onItemSelected(v, pos)}" 

ここでは、

public class Handler { 
    public void onItemSelected(View view, int position) { 
     //... 
    } 
} 

は、これらすべてのケースでは、あなたが binding.setHandler(...)呼び出しで上記のやっているのと同様に、 onCreateViewにハンドラまたはリスナーを割り当てる必要があります。■クラスにはメソッドがあります。 lAppCompatSpinner.setOnItemSelectedListener(...)に電話する必要はありません。これは、バインディングの一部として行われるためです。

関連する問題