Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

子view的点击事件怎么设置,绑定的是bean,不是viewmodel? #15

Open
yovenny opened this issue Feb 27, 2018 · 8 comments

Comments

@yovenny
Copy link

yovenny commented Feb 27, 2018

image

@fashare2015
Copy link
Owner

@BindingAdapter({"vm", "data"})

vm 和 data 两者都绑定了。

内部实现有点乱,最后会调用到最上边那个 bind(...) 。

image

@yovenny
Copy link
Author

yovenny commented Feb 28, 2018

没听太懂,是把Vm也传到xml里面吗?,但是有个问题,这个Vm没有具体某个itemview的dataBean,点击事件可能要判断数据,

我现在的做法,datas传dats,不知道可不可行,试下先

@yovenny
Copy link
Author

yovenny commented Feb 28, 2018

datas《bean》传datas《itemViewmodel》

@fashare2015
Copy link
Owner

vm 里有 data 的呀。所以xml里只用传入vm就行了。data 和 onItemClick 都在 vm 里了。

ListVM.kt

open class ListVM<T>{
    open var data = ObservableArrayList<T>()

    open val onItemClick: OnItemClickListener<T>? = null

    open val headerData: Any? = null
}

g_two_way_list.xml

<layout ...>
    <data>
        <variable name="listVM" type="com.fashare.databinding.TwoWayListVM"/>
    </data>

    <android.support.v7.widget.RecyclerView
        ...
        bind:vm="@{listVM}"
        bind:data="@{listVM.data}"/>

</layout>

@yovenny
Copy link
Author

yovenny commented Feb 28, 2018

 protected void setListener(final ViewHolder viewHolder, int viewType, final int position) {
        if (!isEnabled(viewType)) return;
        viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(viewHolder, mDatas.get(position), position);
                }
            }
        });

        viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                    return mOnItemClickListener.onItemLongClick(viewHolder, mDatas.get(position), position);
                }
                return false;
            }
        });
    }

但是我看listener调用是设置给convertview的,我想给itemview里面的图片,按钮,之类的设置事件。

这个在这怎么应用?

@fashare2015
Copy link
Owner

终于听懂你在说啥了。。这个目前还不支持,😂。支持的话也好弄,RecyclerViewAdapter.java里面setAdapter()那里暴露一个接口出来。

话说,这个项目还不是很完善,不要在自己项目里用。

@yovenny
Copy link
Author

yovenny commented Feb 28, 2018

😂没有,我自己搞搞demo的。

@fashare2015
Copy link
Owner

暂时没时间维护这个。随便一提的是,adapter 模块是基于鸿洋大大的 baseAdapter,我只针对 databinding 封装了一层。

代码量不多,推荐你去看一下那个库。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants