Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未输入ViewHolder中的OnClickListener_Java_Android_Android Recyclerview_Onclicklistener_Android Viewholder - Fatal编程技术网

Java 未输入ViewHolder中的OnClickListener

Java 未输入ViewHolder中的OnClickListener,java,android,android-recyclerview,onclicklistener,android-viewholder,Java,Android,Android Recyclerview,Onclicklistener,Android Viewholder,我有一个“回收者”视图,显示用户在我的应用程序中发布的所有帖子。我希望用户能够点击其中一个帖子,并被带到一个新的片段,在那里他们只能看到他们选择的帖子 当我运行代码并单击其中一篇文章时,OnCLickListener甚至不会被触发 我曾尝试在public void OnClick上添加断点,但从未触发。我曾尝试将OnClick添加到onBindViewHolder,但我知道这不是一个好主意 public class PostAdapter extends RecyclerView.Adapter

我有一个“回收者”视图,显示用户在我的应用程序中发布的所有帖子。我希望用户能够点击其中一个帖子,并被带到一个新的片段,在那里他们只能看到他们选择的帖子

当我运行代码并单击其中一篇文章时,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();
}
公共类PostAdapter扩展了RecyclerView.Adapter{
公开名单张贴名单;
私人语境;
私有静态OnItemClickListener OnItemClickListener;
公共邮政适配器(){
}
公共接口侦听器{
虚线单击(视图v,内部位置);
}
公共无效更新列表(列表){
postList=列表;
notifyDataSetChanged();
}
公共静态类PostViewHolder扩展了RecyclerView.ViewHolder{
TextView postName、PostSerName、postDate、postTime、postContent、postId;
后视支架(视图v){
超级(五);
this.postName=(TextView)itemView.findViewById(R.id.name);
this.posteSerName=(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(新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
int position=PostViewHolder.super.getAdapterPosition();
onItemClick listener.onItemClick(v,位置);
System.out.println(“甚至在这里都做不到”);
}
});
}
}
公共PostAdapter(列表postList、上下文上下文){
this.postList=postList;
this.context=上下文;
}
@凌驾
public PostAdapter.PostViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图=布局更平坦。从(上下文)。充气(R.layout.all\u posts\u布局,父级,false);
返回新的PostAdapter.PostViewHolder(视图);
}
@凌驾
公共无效onBindViewHolder(PostViewHolder,内部位置){
Post Post=postList.get(位置);
holder.posterName.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());
}
@凌驾
public int getItemCount(){
返回此.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>



您需要在该视图的onCreateViewHolder中设置它。也可以在构造函数中初始化它。

根据此更新代码

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

    PostViewHolder(View v) {
        super(v);

        //init view
        v.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        onItemClickListener.onItemClick(v, getAdapterPosition());
    }
}

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

一切看起来都很好,可能xml中的视图具有clickable=false或disabled=true之类的属性。在某些情况下,click事件无法很好地调度到recycler视图,可能是recyclerview父级配置错误。例如,当recycler视图放置在错误配置的Coordinator布局中时,单击事件调度可能会出现问题。您好,感谢您的回复。我已使用与此特定片段相关的xml更新了我的问题。我没有任何属性,如clickable=false或disabled=true。我还试图改变我的布局,看看它们的位置是否与搞乱onclick有关,但我运气不好!添加了一个答案谢谢回复!我已经这样做了,但现在我只在日志猫中看到了这一点,当我单击其中一项时:D/ViewRootImpl:viewPostMeInputStage进程指针0 D/ViewRootImpl:viewPostMeInputStage processPointer 1 D/ViewRootImpl:ViewPostImeInputStage进程指针0 D/ViewRootImpl:ViewPostImeInputStage进程指针1Hi,我尝试了此操作,但在logcat:V/FA:Inactivity中收到这些错误,正在断开与服务的连接D/ViewRootImpl:ViewPostImeInputStage进程指针0 D/ViewRootImpl:ViewPostImeInputStage进程指针1Y您无法在卡布局中添加ScrollView。从卡布局中删除scroll view将起作用。您无法使用RecyclerView添加ScrollView。因为两者都是垂直滚动。
public static class PostViewHolder extends RecyclerView.ViewHolder, View.OnClickListener {
    TextView postName, postUsername, postDate, postTime, postContent, postId;

    PostViewHolder(View v) {
        super(v);

        //init view
        v.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        onItemClickListener.onItemClick(v, getAdapterPosition());
    }
}
**=> You just need to change onBindViewHolder() method code
=> Add click listener on this method.
=> Your code should be like this.**

@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()));

 holder.itemView.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");

            }
        });
}