java - 未输入 ViewHolder 中的 OnClickListener

标签 java android android-recyclerview onclicklistener android-viewholder

我有一个回收站 View ,显示用户在我的应用中发布的所有帖子。我希望用户能够单击其中一个帖子并转到一个新的 fragment ,他们只能在其中看到他们选择的帖子。

当我运行我的代码并单击其中一个帖子时,甚至没有触发 OnCLickListener。

我尝试在 public void OnClick 上添加断点,但它从未被触发。我已尝试将 OnClick 添加到 onBindViewHolder,但我知道这不是一个好主意。

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
public List<Post> postList;
private Context context;
private static OnItemClickListener onItemClickListener;

public PostAdapter(){
}

public interface OnItemClickListener{
    void onItemClick(View v, int position);
}

public void updateList(List<Post> list){
    postList = list;
    notifyDataSetChanged();
}

public static class PostViewHolder extends RecyclerView.ViewHolder{
    TextView postName, postUsername, postDate, postTime, postContent, postId;

    PostViewHolder(View v) {
        super(v);
        this.postName = (TextView) itemView.findViewById(R.id.name);
        this.postUsername = (TextView) itemView.findViewById(R.id.username);
        this.postDate = (TextView) itemView.findViewById(R.id.date);
        this.postTime = (TextView) itemView.findViewById(R.id.time);
        this.postContent = (TextView) itemView.findViewById(R.id.content);
        this.postId = (TextView) itemView.findViewById(R.id.postId);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = PostViewHolder.super.getAdapterPosition();
                onItemClickListener.onItemClick(v, position);
                System.out.println("doesn't even make it here");

            }
        });
    }
}

public PostAdapter(List<Post> postList, Context context){
    this.postList = postList;
    this.context = context;
}

@Override
public PostAdapter.PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View view = LayoutInflater.from(context).inflate(R.layout.all_posts_layout, parent, false);
    return new PostAdapter.PostViewHolder(view);
}

@Override
public void onBindViewHolder(PostViewHolder holder, int position){
    Post post = postList.get(position);
    holder.postUsername.setText(String.valueOf(post.getUsername()));
    holder.postName.setText(String.valueOf(post.getName()));
    holder.postDate.setText(String.valueOf(post.getDate()));
    holder.postTime.setText(String.valueOf(post.getTime()));
}

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

非常感谢任何帮助!

<---card layout for each individual post---->

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:orientation="vertical">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="130dp">

        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="2dp"
            android:elevation="60dp">


            <RelativeLayout
                android:id="@+id/search_term_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="1dp">

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:textStyle="bold"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="italic"
                    android:layout_marginLeft="10dp"
                    android:layout_toRightOf="@id/name"
                    android:text="Username"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/content"
                    android:layout_alignParentEnd="true"
                    android:layout_marginEnd="56dp"
                    android:text="Date"
                    android:textSize="10sp" />

                <TextView
                    android:id="@+id/time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/username"
                    android:layout_toRightOf="@+id/username"
                    android:layout_alignParentEnd="true"
                    android:paddingLeft="8dp"
                    android:text="Time"
                    android:textSize="10sp" />

                <TextView
                    android:id="@+id/content"
                    android:layout_width="315dp"
                    android:layout_height="48dp"
                    android:layout_below="@+id/name"
                    android:layout_alignStart="@+id/name"
                    android:text="Content"
                    android:textSize="18sp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/postId"
                    android:text="ID"
                    android:layout_below="@id/content"
                    />

            </RelativeLayout>
        </android.support.v7.widget.CardView>
        </ScrollView>
    </LinearLayout>

<---回收者 View 显示所有帖子------>


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/filterBtn"
                android:text="Filter"
                android:background="@drawable/button"
                android:layout_margin="2dp"
                android:textColor="@color/common_google_signin_btn_text_dark_default"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/update_post_view"
                android:layout_width="match_parent"
                android:layout_marginTop="50dp"
                android:padding="10dp"
                android:layout_height="match_parent" />
    </RelativeLayout>

最佳答案

似乎您的 ScrollView 消耗了点击事件。尝试将其删除或替换为 NestedScrollView

关于java - 未输入 ViewHolder 中的 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56022527/

相关文章:

java - Android RecyclerView添加和删除项目

android - 如何使用kotlin在recyclerview中附加xml文件?

java - 如何使用map的依赖注入(inject)来测试Spring boot应用程序配置类?

java - 为什么 hasNextInt() 在这里返回 false?

java - 我自己的 android 库的单元测试框架

java - 在应用程序的 Assets 目录中在 Android 中创建符号链接(symbolic link)

android - 进程 'command ' node'' 以非零退出值 1 linux 完成,同时生成签名的 apk

java - 无法使用通配符泛型类型向 Java 集合添加值

Android Studio - 如何压缩对齐 apk

android - 如何在 RecyclerView 中的最后一个元素之后创建一个空白空间?