我有一个RecyclerView
适配器。我还有其他RecyclerView
适配器,点击它们没有任何问题。但是,当我单击其中一个RecyclerView
项时,toast消息不会显示。为什么呢?
public class AttendanceAdapter extends RecyclerView.Adapter {
//...
//other overridden factory methods of RecyclerView Adapter and //Constructors and data setting Methods
//...
public class AttendanceListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView rollNo, name;
ImageView isPreset;
public AttendanceListViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
// ...
// Layout Items initialization
// ...
}
@Override
public void onClick(View v) {
Toast.makeText(mContext,"reyclerViewClicked",Toast.LENGTH_LONG).show();
}
}
}
编辑:
我的itemView XML看起来像这样..
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10"
android:padding="5dp">
<TextView
android:id="@+id/roll_no_in_attendance_single_item"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="2"
android:textColor="@color/colorTeal"
android:textSize="18sp"
android:gravity="center"
android:fontFamily="sans-serif-condensed"
android:text="1" />
<TextView
android:id="@+id/name_in_attendance_single_item"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="6"
android:textColor="@color/colorTeal"
android:textSize="18sp"
android:gravity="center"
android:fontFamily="sans-serif-condensed"
android:text="1" />
<ImageView
android:layout_margin="10dp"
android:id="@+id/present_or_absent_toggle_in_attendance_single_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@mipmap/ic_check_circle_black_24dp"
android:tint="@color/colorTeal"
android:layout_weight="2"
android:text="1" />
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
答案 0 :(得分:3)
解决:我在每个项目中使用ScrollView犯了一个错误(不知不觉),
不得使用Scroll View进入
RecyclerView
项目,因为
RecyclerView
自己滚动。
答案 1 :(得分:0)
尝试通过以下方法实现为RecyclerView
项目点击创建实用程序类。
public class ItemClickSupport {
private final RecyclerView mRecyclerView;
private OnItemClickListener mOnItemClickListener;
private OnItemLongClickListener mOnItemLongClickListener;
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
}
}
};
private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mOnItemLongClickListener != null) {
RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
}
return false;
}
};
private RecyclerView.OnChildAttachStateChangeListener mAttachListener
= new RecyclerView.OnChildAttachStateChangeListener() {
@Override
public void onChildViewAttachedToWindow(View view) {
if (mOnItemClickListener != null) {
view.setOnClickListener(mOnClickListener);
}
if (mOnItemLongClickListener != null) {
view.setOnLongClickListener(mOnLongClickListener);
}
}
@Override
public void onChildViewDetachedFromWindow(View view) {
}
};
private ItemClickSupport(RecyclerView recyclerView) {
mRecyclerView = recyclerView;
mRecyclerView.setTag(R.id.item_click_support, this);
mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
}
public static ItemClickSupport addTo(RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support == null) {
support = new ItemClickSupport(view);
}
return support;
}
public static ItemClickSupport removeFrom(RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support != null) {
support.detach(view);
}
return support;
}
public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
return this;
}
public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
return this;
}
private void detach(RecyclerView view) {
view.removeOnChildAttachStateChangeListener(mAttachListener);
view.setTag(R.id.item_click_support, null);
}
public interface OnItemClickListener {
void onItemClicked(RecyclerView recyclerView, int position, View v);
}
public interface OnItemLongClickListener {
boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
}
}
在activity
或fragment
。
ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
// your code here
Toast.makeText(mContext,"reyclerViewClicked",Toast.LENGTH_LONG).show();
}
});
如此blog所示。
答案 2 :(得分:0)
假设您有item_view.xml
个文件。您的主要布局是LinearLayout
。只需为其定义 id 即可。
public class AttendanceListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
LinearLayout rootView;
TextView rollNo, name;
ImageView isPreset;
public AttendanceListViewHolder(View itemView) {
super(itemView);
rootView = (LinearLayout)itemView.findViewById(R.id.root_view);
rootView.setClickable(true);
rootView.setOnClickListener(this);
// ...
// Layout Items initialization
// ...
}
@Override
public void onClick(View v) {
Toast.makeText(mContext,"reyclerViewClicked",Toast.LENGTH_LONG).show();
}
}