错误:数据绑定不存在 - Android

时间:2017-07-13 13:43:35

标签: android data-binding android-databinding

我在我的项目中使用data Binding,但是让我发出错误信息:

CodePen

以下是我的模型CProgressBar.java

public class CProgressBar extends BaseObservable {
    private boolean visibility;

    @Bindable
    public boolean isVisibility() {
        return visibility;
    }

    public void setVisibility(boolean visibility) {
        this.visibility = visibility;
        notifyPropertyChanged(BR.visibility);
    }
}

这是我的activity_start.xml

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

    <data>

        <variable
            name="showNews"
            type="ir.jmostafa.appportalmostafa.start.models.ShowNews" />

        <variable
            name="mVisible"
            type="ir.jmostafa.appportalmostafa.start.models.CProgressBar" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="ir.jmostafa.appportalmostafa.start.StartActivity">

        <com.daimajia.slider.library.SliderLayout
            android:id="@+id/slider_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            custom:auto_cycle="true"
            custom:indicator_visibility="visible"
            custom:pager_animation="Accordion"
            custom:pager_animation_span="1100" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerStartActivity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/slider_image"
            android:scrollbars="vertical" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/recyclerStartActivity">

            <ProgressBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:visibility="@{@bool/mVisible.visibility}" />

            <TextView
                android:id="@+id/txtFirstNews"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@{showNews.firstNewsTitle}" />

            <TextView
                android:id="@+id/txtSecondNews"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txtFirstNews"
                android:layout_centerInParent="true"
                android:text="@{showNews.seconNewsTitle}" />
        </RelativeLayout>

    </RelativeLayout>
</layout>

我在StartActvity.java写道:

activityStartBinding = DataBindingUtil.setContentView(this, R.layout.activity_start);
...
CProgressBar cProgressBar = new CProgressBar();
        cProgressBar.setVisibility(true);
        activityStartBinding.setMVisible(true);

这是我的AdapterStartActivity.java

public class AdapterStartActivity extends RecyclerView.Adapter<AdapterStartActivity.ViewHolder> {
    private ListItemActivityStartBinding binding;
    private List<String> listTitle;
    private Context context;

    public AdapterStartActivity(Context context, List<String> listTitle) {
        this.listTitle = listTitle;
        this.context = context;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View v) {
            super(v);
        }
    }

    @Override
    public AdapterStartActivity.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.list_item_activity_start, parent, false);
        binding = DataBindingUtil.bind(view);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder Vholder, int position) {
        binding.setListItems(listTitle.get(position));
        binding.setClickListener(this);
        binding.getRoot().setTag(position);
    }

    @Override
    public int getItemCount() {
        return listTitle.size();
    }

    public void onCardClick(View view, String listItems) {
        Log.i("SADASSDASDAS", " A " + listItems + " B " + view.getTag());
    }
}

这是recyclerView的列表项:

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

    <data>

        <variable
            name="listItems"
            type="String" />

        <variable
            name="clickListener"
            type="ir.jmostafa.appportalmostafa.adapters.AdapterStartActivity" />
    </data>

    <android.support.v7.widget.CardView
        android:id="@+id/card_lstItemStart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:onClick="@{(v)->clickListener.onCardClick(v, listItems)}"
        card_view:cardCornerRadius="7dp"
        card_view:cardElevation="7dp"
        card_view:cardMaxElevation="7dp"
        card_view:contentPadding="0dp">

        <TextView
            android:id="@+id/txtTitles"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:padding="10dp"
            android:text="@{listItems}"
            android:textColor="#000000"
            android:textSize="20dp"
            android:textStyle="bold" />
    </android.support.v7.widget.CardView>
</layout>

1 个答案:

答案 0 :(得分:0)

您的知名度值应为VISIBLEINVISIBLEGONE。在那种情况下

android:visibility="@{mVisible.visibility ? View.VISIBLE : View.GONE}"

并在xml中导入View

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