OnClick 用于片段中的 2/3 回收器视图(其本身托管/放置在另一个片段上)不起作用

Posted

技术标签:

【中文标题】OnClick 用于片段中的 2/3 回收器视图(其本身托管/放置在另一个片段上)不起作用【英文标题】:OnClick for 2/3 recycler views in a fragment (which itself is hosted/placed on another fragment) not working 【发布时间】:2020-12-20 00:42:06 【问题描述】:

我在一个片段中有三个回收器视图(它本身托管/放置在另一个片段上),第二个和第三个片段的 onClick 不起作用(第一个工作得很好),我尝试了一切......在这里是我的代码:

第二个 Recycler View 适配器类: 最近的歌曲适配器.java

    package com.myapp.beatify;
    
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.bumptech.glide.Glide;
    
    import java.util.List;
    
    public class RecentSongsAdapter extends RecyclerView.Adapter<RecentSongsAdapter.MyViewHolder> 
        private List<CreateSong> mRecentSongList;
        private OnItemClickListener mListener;
    
        public interface OnItemClickListener 
            void onItemClick(int position);
        
    
        public void setOnItemClickListener(OnItemClickListener listener) 
            mListener = listener;
        
    
        public RecentSongsAdapter(List<CreateSong> mRecentSongList) 
            this.mRecentSongList = mRecentSongList;
        
    
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
            View view;
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.create_recent_songs, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(view, mListener);
            return myViewHolder;
        
    
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) 
            Glide.with(holder.imageView.getContext()).load(mRecentSongList.get(position).getImgURL()).into(holder.imageView);
            holder.textView.setText(mRecentSongList.get(position).getTxt());
        
    
        @Override
        public int getItemCount() 
            return mRecentSongList.size();
        
    
        public static class MyViewHolder extends RecyclerView.ViewHolder 
            public ImageView imageView;
            public TextView textView;
    
            public MyViewHolder(@NonNull View itemView, final OnItemClickListener listener) 
                super(itemView);
                this.imageView = itemView.findViewById(R.id.recent_img);
                this.textView = itemView.findViewById(R.id.recent_txt);
    
                itemView.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View view) 
                        if (listener != null) 
                            int position = getAdapterPosition();
    
                            if (position == RecyclerView.NO_POSITION) 
                                listener.onItemClick(position);
                            
                        
                    
                );
            
        
    
    

它的 xml 文件: create_recent_songs.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/create_recent_RV"
    android:layout_
    android:layout_
    android:layout_margin="5dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="5dp">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:padding="5dp">

        <ImageView
            android:id="@+id/recent_img"
            android:layout_
            android:layout_
            android:src="@drawable/app_logo" />

        <TextView
            android:id="@+id/recent_txt"
            android:layout_
            android:layout_
            android:fontFamily="@font/artifika"
            android:gravity="center"
            android:text="Shape Of You"
            android:textSize="15sp"
            android:textStyle="bold" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

第三个 Recycler View 适配器类: OurPicksAdapter.java

package com.myapp.beatify;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class OurPicksAdapter extends RecyclerView.Adapter<OurPicksAdapter.MyViewHolder> 
    private List<CreateSong> mOurPicksList;
    private OnItemClickListener mListener;

    public interface OnItemClickListener 
        void onItemClick(int position);
    

    public void setOnItemClickListener(OnItemClickListener listener) 
        mListener = listener;
    

    public OurPicksAdapter(List<CreateSong> mSongList) 
        this.mOurPicksList = mSongList;
    

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.create_our_picks, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view, mListener);
        return myViewHolder;
    

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) 
//        holder.imageView.setImageResource(mSongList.get(position).getImgURL());
        Glide.with(holder.imageView.getContext()).load(mOurPicksList.get(position).getImgURL()).into(holder.imageView);
        holder.textView.setText(mOurPicksList.get(position).getTxt());
    

    @Override
    public int getItemCount() 
        return mOurPicksList.size();
    

    public static class MyViewHolder extends RecyclerView.ViewHolder 
        public ImageView imageView;
        public TextView textView;

        public MyViewHolder(@NonNull View itemView, final OnItemClickListener listener) 
            super(itemView);
            this.imageView = itemView.findViewById(R.id.our_img);
            this.textView = itemView.findViewById(R.id.our_txt);

            itemView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View view) 
                    if (listener != null) 
                        int position = getAdapterPosition();

                        if (position == RecyclerView.NO_POSITION) 
                            listener.onItemClick(position);
                        
                    
                
            );
        
    

它的 xml 文件: create_our_picks.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/create_our_RV"
    android:layout_
    android:layout_
    android:layout_margin="5dp"
    app:cardCornerRadius="5dp"
    app:cardElevation="5dp">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="vertical"
        android:padding="5dp">

        <ImageView
            android:id="@+id/our_img"
            android:layout_
            android:layout_
            android:src="@drawable/app_logo" />

        <TextView
            android:id="@+id/our_txt"
            android:layout_
            android:layout_
            android:fontFamily="@font/artifika"
            android:gravity="center"
            android:text="Shape Of You"
            android:textSize="15sp"
            android:textStyle="bold" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

【问题讨论】:

你能在你实例化适配器的地方添加代码吗?你用断点调试了吗? 实际上我无法添加代码,因为它不适合这里,好吧,如果我发送一个 github 链接,你会看一下代码吗?另外告诉我应该在哪里添加断点和调试 @KevinRobatel 你想让我把实例化适配器的代码发送给我吗?我的意思是在 pastebin 或一些代码共享站点中? codepile.net/pile/rG8PR0GW,这是我实例化我的适配器@KevinRobatel的代码,(忽略我用来加载图像的巨大网址) 【参考方案1】:

我看了一下https://www.codepile.net/pile/rG8PR0GW,也许您应该将oAdapter.setOnItemClickListener 调用移到ourRecyclerView.setAdapter 之前。

像这样:

    private void createUserRecentRecyclerView() 
        recentRecyclerView = view.findViewById(R.id.user_recent_RV);
        recentRecyclerView.setHasFixedSize(true);

        rAdapter = new RecentSongsAdapter(recentList);
        rAdapter.setOnItemClickListener(
                new RecentSongsAdapter.OnItemClickListener() 
                    @Override
                    public void onItemClick(int position) 
                        Log.e("Info", "Connected");
                        Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
                    
                );

        recentRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        recentRecyclerView.setAdapter(rAdapter);
    

【讨论】:

会试试这个 更新:- 仍然无法工作 :(,您在代码中看到任何其他错误吗?顺便说一下,我的第一个回收站视图正在工作,这是代码:codepile.net/pile/rLWzxWPl 及其 xml文件:codepile.net/pile/XKl7qaz7 xml 代码到我的片段所在的位置:codepile.net/pile/WAp0EKAE

以上是关于OnClick 用于片段中的 2/3 回收器视图(其本身托管/放置在另一个片段上)不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何将Intent用于回收者视图中的片段? [关闭]

视图重新添加到视图寻呼机中的片段

如何让片段中的多个视图调用片段类中声明的相同 onClick 函数?

共享元素返回过渡不适用于片段中的 recyclerview 和 cardview

从firebase检索图像以在片段中的回收器视图时出错

片段内 Listview 中动态视图的 onClick 侦听器