내 조각에 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) {
}
}
}
}