ViewModel修改数据,布局无法识别

时间:2016-11-11 16:05:07

标签: java android mvvm

我有一个名为seen

的属性的EpisodeViewModel

如果用户点击该项目,我想将Bool变量seen更改为true。

在episode_item.xml中,我使用此src

定义了ImageView

src='@{viewModel.seen ? @drawable/ic_check : @drawable/ic_check_false}'

但如果我更改了父级SeasonArrayAdapter的seen值,则没有任何反应。

我想,如果我更改变量,UI会识别这个新值。

以下是一些代码段: episode_item.xml片段:

<CardView...>
    <ImageView
                    android:layout_width="34dp"
                    android:layout_height="34dp"
                    android:id="@+id/episode_seen"
                    android:src='@{viewModel.seen ? @drawable/ic_check : @drawable/ic_check_false}'/>
</CardView>

EpisodeViewModel:

public class EpisodeViewModel extends BaseObservable {
    private TvEpisode episode;
    private Context context;
    private Boolean seen;

    public EpisodeViewModel(Context context, TvEpisode episode) {
        this.episode = episode;
        this.context = context;

        this.seen = false;
    }

    public String getTitle() {
        return this.episode.getName();
    }

    public void setSeen(Boolean seen) {
        this.seen = seen;
    }

    public Boolean getSeen() {
        return this.seen;
    }
}

父级SeasonArrayAdapter函数:

recyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(this.context.getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View v, int pos) {
                    final TvEpisode episode = adapter.getItemAt(pos);

                    EpisodeItemBinding binding = (EpisodeItemBinding)DataBindingUtil.getBinding(v);

                    EpisodeViewModel viewModel = binding.getViewModel();
                    viewModel.setSeen(true);
                    ....

至少是RecyclerView Helper:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
            return true;
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());
        if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
}

我是否需要实现INotifyPropertyChanged接口?或者是否已经包含在BaseObservable类中?

0 个答案:

没有答案