RecycleView点击事件一直没反应

adapter里面点击的接口是这样的

public class SentenceRecycleAdapter extends RecyclerView.Adapter<SentenceRecycleAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Sentence> data;
    private RecyclerView mRecyclerView;
    private OnItemClickListener mItemClickListener;
    private OnItemLongClickListener mLongClickListener;

    //第一步:设置回调接口
    public interface OnItemClickListener{
        void onClick(View view, int position);
    }

    public interface OnItemLongClickListener{
        void onClick(View view, int position);
    }

    //第二部:写公共方法
    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        mItemClickListener = OnItemClickListener;
    }

    public OnItemClickListener getmItemClickListener() {
        return mItemClickListener;
    }

    //长按
    public void setOnItemLongClickListener(OnItemLongClickListener OnItemLongClickListener) {
        mLongClickListener = OnItemLongClickListener;
    }

    public OnItemLongClickListener getmLongClickListener() {
        return mLongClickListener;
    }

    public SentenceRecycleAdapter(ArrayList<Sentence> data) {
        this.data = data;
    }
    public SentenceRecycleAdapter(Context context,ArrayList<Sentence> data) {
        this.context=context;
        this.data = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sentenceitem_layout,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        Sentence sentence=data.get(position);
        holder.content.setText(sentence.getContent());
        holder.date.setText(sentence.getDate());
        if(TextUtils.isEmpty(sentence.getFrom_works()))
            holder.from.setText("原创");
        else
            holder.from.setText("出自:"+sentence.getFrom_works()+"   "+sentence.getFrom_role());



        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClickListener.onClick(view, position);
                }
            });
        }
        if(mLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                        mLongClickListener.onClick(view,position);
                    return true;
                }
            });
        }

    }
@Override
    public int getItemCount() {
        return data.size();
    }



    public class ViewHolder extends RecyclerView.ViewHolder {
        //将列表项布局中的控件定义成属性
        TextView content;
        TextView date;
        TextView from;

        public ViewHolder(View view) {
            super(view);//view:列表项子视图
            content = view.findViewById(R.id.content);
            date = view.findViewById(R.id.date);
            from = view.findViewById(R.id.from);
        }
    }

    @NonNull
    @Override
    public String toString() {
        return super.toString();
    }
}

主活动是这样的

mAdapter = new SentenceRecycleAdapter(this,data);

        mAdapter.setOnItemClickListener(new SentenceRecycleAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Toast.makeText(MainActivity.this, "这是条目:" + data.get(position), Toast.LENGTH_SHORT).show();
            }
        });
        mAdapter.setOnItemLongClickListener(new SentenceRecycleAdapter.OnItemLongClickListener() {
            @Override
            public void onClick(View view, int position) {
                Toast.makeText(MainActivity.this, "长按-这是条目:" + data.get(position), Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutFrozen(true);

难道是我的ItemView的根布局用的GardView的问题吗?

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="8dp"
    card_view:cardCornerRadius="10dp"
    android:background="#EBF8FF"
    android:clickable="true"
    android:longClickable="true">
<!--    android:longClickable="true">-->
    <!--    android:onClick="myItemClick"-->

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp">
        <TextView
            android:id="@+id/date"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:text="TextView" />
        <TextView
            android:id="@+id/from"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:singleLine="true"/>
    </LinearLayout>
</androidx.cardview.widget.CardView>

RecycleView的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:descendantFocusability="blocksDescendants"
    android:id="@+id/Recycleview"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <view
        class="androidx.recyclerview.widget.RecyclerView"
        android:id="@+id/SRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"></view>

</RelativeLayout>

我想着我的clickable也没有重复设置啊,也不要会消费点击事件啊,初学者实在是头裂开。难道接口回调不是这样写的嘛。setLayoutFrozen(true)我也试了啥用没有,也看不懂。猛男落泪。

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐