ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-在ViewHolder中未输入OnClickListener

2019-12-10 23:27:08  阅读:185  来源: 互联网

标签:android-viewholder android-recyclerview onclicklistener java android


我有一个回收站视图,显示用户在我的应用程序中发布的所有帖子.我希望用户能够单击其中一个帖子,并带到一个新片段,在该片段中,他们只能看到他们选择的帖子.

当我运行代码并单击其中一篇文章时,甚至不会触发OnCLickListener.

我曾尝试在公共无效的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>

< ---显示所有帖子的回收者视图------>


    <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消耗了click事件.尝试将其删除或替换为NestedScrollView

标签:android-viewholder,android-recyclerview,onclicklistener,java,android
来源: https://codeday.me/bug/20191210/2104778.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有