2016-08-25 3 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를 사용하려는 경우, 당신은 이미 레이아웃의 속성을 사용할 수는 :

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

당신에게 :

<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의 클래스의 메소드를 가정

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

여기서 핸들러 '

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

이 모든 경우에, 당신은 당신이 binding.setHandler(...) 통화를 위하고있는 그대로의 onCreateView에 핸들러 또는 리스너를 할당해야합니다 : S 클래스는 메소드가 있습니다. lAppCompatSpinner.setOnItemSelectedListener(...)으로 전화 할 필요가 없으므로 바인딩의 일부로 처리됩니다.

관련 문제