我使用CardView布局作为RecyclerView的Row.但是,我在将OnClickListener附加到布局时遇到问题.
我正在使用以下布局
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/unit_5"
android:clickable="true"
android:longClickable="true"
card_view:cardCornerRadius="@dimen/unit_5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/spinnerWidth"
android:background="@drawable/row_item_background"
android:clickable="true"
android:orientation="vertical"
android:padding="@dimen/dialog_left_padding">
<!-- Other items -->
</LinearLayout>
</android.support.v7.widget.CardView>
以下是我的适配器和ViewHolder
private class SampleAdapter extends RecyclerView.Adapter<SampleViewHolder> {
ArrayList<Item> arrayList;
private LayoutInflater inflater;
public SampleAdapter(Context context) {
inflater = LayoutInflater.from(context);
this.arrayList = new ArrayList<>();
arrayList.addAll(items);
}
@Override
public SampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.layout1, parent, false);
SampleViewHolder holder = new SampleViewHolder(view);
holder.setClickHandler(handler);
return holder;
}
@Override
public void onBindViewHolder(SampleViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return arrayList.size();
}
}
static class SampleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
TextView name;
IClickHandler clickHandler;
public IClickHandler getClickHandler() {
return clickHandler;
}
public void setClickHandler(IClickHandler clickHandler) {
this.clickHandler = clickHandler;
}
public SampleViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView
.findViewById(R.id.name);
itemView.setonClickListener(this);
itemView.setonLongClickListener(this);
}
@Override
public void onClick(View view) {
if (getClickHandler() != null) {
getClickHandler().onItemClicked(view, getLayoutPosition());
}
}
@Override
public boolean onLongClick(View view) {
if (getClickHandler() != null) {
getClickHandler().onItemLongClicked(view, getLayoutPosition());
}
return true;
}
}
但是这个onClick永远不会被解雇.
但是,如果我用LinearLayout替换CardView,每件事都可以.
我不知道这里有什么问题.有人可以帮助我吗?
谢谢.
解决方法:
实际上实现onClick非常简单,我不确定自定义点击处理程序(IClickHandler)的实现.
首先,View持有人应该只是:
static class SampleViewHolder extends RecyclerView.ViewHolder {
TextView name;
public SampleViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
}
}
然后在SampleAdapter中
@Override
public SampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.layout1, parent, false);
SampleViewHolder holder = new SampleViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(SampleViewHolder holder, int position) {
final Item item = arrayList.get(position);
holder.name.setonClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//notice I implemented onClickListener here
// so I can associate this click with final Item item
}
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。