问题描述
我使用CardView布局行RecyclerView。但是我在附加OnClickListener到布局中面临的问题。我使用以下布局
I am using CardView layout as Row for RecyclerView. However I am facing issue in attaching OnClickListener to the layout.I am using following layout
<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
Following are my adapter and 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从来没有发射。
But this onClick is never fired.
不过,如果我取代CardView与LinearLayout中每一件事情能正常工作。我不知道什么是这里的问题。有人可以帮我在这?
However, if I replace CardView with LinearLayout every thing works fine.I am not sure what is the issue here. Can somebody help me in this?
感谢。
推荐答案
其实实现的onClick很简单,我不知道自定义点击处理器(IClickHandler)的执行情况。
Actually implementing onClick is very straightforward and I am not sure of the implementation of a custom click handler (IClickHandler).
首先查看持有人应该只是:
First of all View holder should be just:
static class SampleViewHolder extends RecyclerView.ViewHolder {
TextView name;
public SampleViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
}
}
然后在SampleAdapter
And then in 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
}
});
}
这篇关于问题CardView和OnClickListener在RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!