在android数据绑定中使用递增和递减处理程序绑定按钮

时间:2016-05-11 10:53:00

标签: android onclicklistener runtimeexception android-handler android-databinding

我正在尝试使用处理程序在TextView中实现数量的增加和减少,但总是抛出相同的错误,请帮助我理解我缺少获得预期结果的位置。

错误:任务':app:compileDebugJavaWithJavac'执行失败。

  

java.lang.RuntimeException:发现数据绑定错误。   **** /数据绑定错误****消息:标识符必须具有XML文件中的用户定义类型。视图缺少它   file:C:\ Users \ Tushar Rai \ Desktop \ Demo \ Demo \ app \ src \ main \ res \ layout \ fragment_list.xml   **** \数据绑定错误****

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

    <variable
        name="quantity"
        type="int"/>

    <variable
        name="Handlers"
        type="com.demo.www.demo.ui.Fragment.Handlers"/>

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentEnd="true"
        android:paddingTop="16dp"
        android:paddingBottom="16dp">


        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/decrease"
            android:onClick="@{Handlers.decrement(view, 0)}" />

        <TextView
            android:layout_width="32dp"
            android:layout_height="wrap_content"
            app:quantity="@{quantity}"
            android:textAlignment="center" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/increase"
            android:onClick="@{Handlers.increment(view, 10)}" />

    </LinearLayout>

public class Fragment扩展Fragment {

FirebaseRecyclerAdapter adapter;
Firebase mFirebaseRef = new Firebase("https://demo.firebaseio.com/").child("list");

public Fragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View rootView = inflater.inflate(R.layout.recycler_view, container, false);
    final RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);

    adapter = new FirebaseRecyclerAdapter<List, ViewHolder>(List.class, R.layout.fragment_list,
            ViewHolder.class, mFirebaseRef) {
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, List list, int i) {

            FragmentBinding binding = viewHolder.getBinding();
            binding.setList(list);
            Handlers handlers = new Handlers();
            binding.setHandlers(handlers);


        }
    };
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), null));
    return rootView;
}

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public FragmentBinding binding;

    public ViewHolder(View itemView) {
        super(itemView);
        binding = DataBindingUtil.bind(itemView);
        itemView.setOnClickListener(this);
    }

    public FragmentBinding getBinding() {
        return binding;
    }
    @Override
    public void onClick(View v) {


    }
}
@BindingAdapter("imageUrl")
public static void setImage(ImageView imageView, String imageUrl) {
    Picasso.with(imageView.getContext()).load(imageUrl).into(imageView);
}
@BindingAdapter("quantity")
public static void setQuantityText(TextView view, int quantity) {
    view.setText(String.valueOf(quantity));
}
public class Handlers {

    public void decrement(View view, int min) {
        FragmentBinding binding = DataBindingUtil.findBinding(view);
        binding.setQuantity(Math.min(min, binding.getQuantity() - 1));
    }
    public void increment(View view, int max) {
        FragmentBinding binding = DataBindingUtil.findBinding(view);
        binding.setQuantity(Math.max(max, binding.getQuantity() + 1));
    }

}

}

1 个答案:

答案 0 :(得分:0)

我认为问题出现在你的增量和递减方法。您不能重载这些方法参数。

你需要写一下:

public class Handlers {

public void decrement(View view) {
    FragmentBinding binding = DataBindingUtil.findBinding(view);
    binding.setQuantity(Math.min(min, binding.getQuantity() - 1));
}
public void increment(View view) {
    FragmentBinding binding = DataBindingUtil.findBinding(view);
    binding.setQuantity(Math.max(max, binding.getQuantity() + 1));
}

}

在xml中:

android:onClick="@{Handlers.increment}"

请参阅相关链接:DataBinding