RecyclerVIew onItemClickListener不工作

java android android-recyclerview adapter onitemclicklistener

703 观看

6回复

3 作者的声誉

我正在使用RecyclerView并尝试使用Interface对每个recyclcerview项目使用click监听器

这是我的Activity类:

public class LegacyHomeActivity extends ActivityBaseDrawer {
    private List<LegacyVideo> legacyVideoList = null;
    private List<Video> videoList = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private LegacyModeHomeAdapter adapter;

    @Override
    public void onNetworkStateChanged(boolean connected) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isLegacyMode = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_legacy_home);
        if (getSupportActionBar() != null) {
            setTitle(getString(R.string.footy_legacy_home));
        }
        ImageView legacy_live_score_imageView = (ImageView) findViewById(R.id.legacy_live_score_imageView);
        legacy_live_score_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LegacyHomeActivity.this, LegacyLiveScoreActivity.class));
            }
        });
        ImageView legacy_highlight_imageView = (ImageView) findViewById(R.id.legacy_highlight_imageView);
        legacy_highlight_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LegacyHomeActivity.this, LegacyHighlightsActivity.class));
            }
        });
        ImageView legacy_news_imageView = (ImageView) findViewById(R.id.legacy_news_imageView);
        legacy_news_imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LegacyHomeActivity.this, LegacyNewsActivity.class));
            }
        });
        mRecyclerView = (RecyclerView) findViewById(R.id.legacy_mood_recyclerview);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
        adapter = new LegacyModeHomeAdapter(getApplicationContext(), itemClickListener);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setHasFixedSize(true);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeResources(R.color.ThemeColor,
                R.color.ThemeColor, R.color.ThemeColor);
        swipeRefreshLayout
                .setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

                    @Override
                    public void onRefresh() {
                        getLegacyVideoList();
                    }
                });

        circularProgressView = (CircularProgressView) findViewById(R.id.circularProgressView);
        showLoadingProgress();
        getLegacyVideoList();
    }

    LegacyVideoClickListener itemClickListener = new LegacyVideoClickListener() {
        @Override
        public void onItemClickListener(int position) {
            Video video = videoList.get(position);
            VideoPlayerUtil.initVideoPLayer(video, LegacyHomeActivity.this);
        }
    };

    private void getLegacyVideoList() {
        FootyLightAPI api = new FootyLightAPI(getApplicationContext(), mResponseListener);
        api.getLegacyHomeVideoList();
    }

    private final FootyLightAPI.ResponseListener mResponseListener = new FootyLightAPI.ResponseListener() {
        @Override
        public void getResponse(int apiId, final String response) {
            switch (apiId) {
                case R.integer.ApiLegacyVideoList:
                    Log.e("Legacy  ............", "Response:................" + response);
                    try {
                        final ObjectMapper mapper = new ObjectMapper();
                        legacyVideoList = mapper.readValue(response, new TypeReference<List<LegacyVideo>>() {
                        });
                        if (legacyVideoList != null && legacyVideoList.size() > 0) {
                            videoList.clear();
                            for (LegacyVideo video : legacyVideoList) {
                                Log.e("Legacy video", "" + video.getTitle());
                                Video videoItem = new Video();
                                if (video.getItemType().equalsIgnoreCase("video")) {
                                    videoItem.setTitle(video.getTitle());
                                    videoItem.setLink(video.getLink());
                                    videoItem.setThumb(video.getThumb());
                                    videoItem.setContentType(video.getContentType());
                                    videoItem.setContentUrl(video.getContentUrl());
                                    videoItem.setDmcaLabel(video.getDmcaLabel());
                                    videoItem.setDmcaContent(video.getDmcaContent());
                                    videoItem.setDmcaDisclaimer(video.getDmcaDisclaimer());
                                    videoItem.setDisplayType(video.getDisplayType());
                                    videoList.add(videoItem);
                                }

                            }
                        }
                        Log.e("VideoList", "........." + videoList.size());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showVideoAdapter();
                                hideLoadingProgress();

                            }
                        });


                    } catch (Exception e) {

                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void getError(int apiId, int errorCode) {
        }

        @Override
        public void getResponse(String trackingStr, int apiId, String response) {

        }

        @Override
        public void getError(String trackingStr, int apiId, int errorCode) {

        }
    };

    public void showVideoAdapter() {
        if (adapter != null) {
            adapter.setVideos(videoList);
        }
    }
}

这是我的适配器类

public class LegacyModeHomeAdapter extends RecyclerView.Adapter<LegacyModeHomeAdapter.ItemViewHolder> {
    private List<Video> videoList;
    private Context mContext;
    private LegacyVideoClickListener onVideosRVItemClickListener;

    private LayoutInflater getLayoutInflater() {
        return LayoutInflater.from(mContext);
    }

    public LegacyModeHomeAdapter(Context mContext, LegacyVideoClickListener onVideosRVItemClickListener) {
        this.mContext = mContext;
        this.onVideosRVItemClickListener = onVideosRVItemClickListener;
    }

    public void setVideos(List<Video> videosList) {
        this.videoList = videosList;
        notifyDataSetChanged();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.layout_legacy_mode_home_video_item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        Video video = videoList.get(position);
        Picasso.with(mContext).load(video.getThumb()).into(holder.categoryImage);
        holder.categoryName.setText(video.getTitle());
        holder.title_image_btn.setText(video.getDisplayType());
        holder.VideosItemClickListener(position, onVideosRVItemClickListener);
    }

    @Override
    public int getItemCount() {
        if (videoList != null) {
            return videoList.size();
        }
        return 0;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView categoryName;
        private ImageView categoryImage;
        private Button title_image_btn;

        public ItemViewHolder(View itemView) {
            super(itemView);
            categoryName = (TextView) itemView.findViewById(R.id.categoryName);
            categoryImage = (ImageView) itemView.findViewById(R.id.categoryImage);
            title_image_btn = (Button) itemView.findViewById(R.id.title_image_btn);
        }

        public void VideosItemClickListener(final int position, final LegacyVideoClickListener videosRVItemClickListener) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    videosRVItemClickListener.onItemClickListener(position);
                }
            });
        }

    }
}

最后我将这个界面用于监听器:

public interface LegacyVideoClickListener {
    public void onItemClickListener(int position);
}

这个程序适用于我应用程序的所有其他部分,但不适用于此情况。我不知道问题出在哪里。请帮我 。

作者: Md Aslam Hossin 的来源 发布者: 2017 年 4 月 18 日

回应 6


-1

90 作者的声誉

@Override
public void onBindViewHolder(RecyclerViewAdapter.ItemViewHolder holder, int position) {
        holder.name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

}

试试这样吧

作者: AKSHAY MANAGOOLI 发布者: 2017 年 4 月 18 日

0

1360 作者的声誉

检查此样本

可以使用自定义界面获取OnCLick

interface Listener {
    void performOperation(int Position);
}

在你的适配器

private Listener mListener;

在您的适配器中,像这样在构造函数中初始化列表器

mListener = (Listener) context;

在bindViewHolder方法中

holder.yourView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mListener.performOperation(position);
        }
    });

现在在你的Activity中实现这样的接口

public class LegacyHomeActivity extends ActivityBaseDrawer implements listner {

@Override
public void performOperation(int Position) {
    //Do whatever you want to do with this position
}

}
作者: AbhayBohra 发布者: 2017 年 4 月 18 日

0

141 作者的声誉

在ItemViewHolder构造函数中传递onVideosRVItemClickListener。

@Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.layout_legacy_mode_home_video_item, parent, false);
        return new ItemViewHolder(view, onVideosRVItemClickListener);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        Video video = videoList.get(position);
        Picasso.with(mContext).load(video.getThumb()).into(holder.categoryImage);
        holder.categoryName.setText(video.getTitle());
        holder.title_image_btn.setText(video.getDisplayType());

    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView categoryName;
        private ImageView categoryImage;
        private Button title_image_btn;

        public ItemViewHolder(View itemView, final LegacyVideoClickListener videosRVItemClickListener) {
            super(itemView);
            categoryName = (TextView) itemView.findViewById(R.id.categoryName);
            categoryImage = (ImageView) itemView.findViewById(R.id.categoryImage);
            title_image_btn = (Button) itemView.findViewById(R.id.title_image_btn);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    videosRVItemClickListener.onItemClickListener(getAdapterPosition());
                }
            });
        }

    }
作者: mpals 发布者: 2017 年 4 月 18 日

0

1529 作者的声誉

决定

你这个方法:

@Override 
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final String element = mDataset[position];

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //your code 
            }
        });
    }
作者: Jay Smith 发布者: 2017 年 4 月 18 日

0

763 作者的声誉

你的接口适配器类应该是这样的

public class LegacyModeHomeAdapter extends RecyclerView.Adapter<LegacyModeHomeAdapter.ItemViewHolder> {
    private List<Video> videoList;
    private Context mContext;
    public static LegacyVideoClickListener onVideosRVItemClickListener;

    private LayoutInflater getLayoutInflater() {
        return LayoutInflater.from(mContext);
    }

    public LegacyModeHomeAdapter(Context mContext, LegacyVideoClickListener onVideosRVItemClickListener) {
        this.mContext = mContext;
        this.onVideosRVItemClickListener = onVideosRVItemClickListener;
    }

    public void setVideos(List<Video> videosList) {
        this.videoList = videosList;
        notifyDataSetChanged();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = getLayoutInflater().inflate(R.layout.layout_legacy_mode_home_video_item, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        Video video = videoList.get(position);
   Picasso.with(mContext).load(video.getThumb()).into(holder.categoryImage);
        holder.categoryName.setText(video.getTitle());
        holder.title_image_btn.setText(video.getDisplayType());
        holder.VideosItemClickListener(position, onVideosRVItemClickListener);
    }

    @Override
    public int getItemCount() {
        if (videoList != null) {
            return videoList.size();
        }
        return 0;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView categoryName;
        private ImageView categoryImage;
        private Button title_image_btn;

        public ItemViewHolder(View itemView) {
            super(itemView);
            categoryName = (TextView) itemView.findViewById(R.id.categoryName);
            categoryImage = (ImageView) itemView.findViewById(R.id.categoryImage);
            title_image_btn = (Button) itemView.findViewById(R.id.title_image_btn);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            onVideosRVItemClickListener.onItemClickListener(getPosition(), v);
        }
    }
    public void setOnItemClickListener(LegacyVideoClickListener clickListener) {
            LegacyModeHomeAdapter.onVideosRVItemClickListener = clickListener;
    }
    public interface LegacyVideoClickListener {
        public void onItemClickListener(int position, View v);
    }
}

你的适配器用法类应该是这样的

adapter.setOnItemClickListener(new LegacyModeHomeAdapter.LegacyVideoClickListener() {
        @Override
        public void onItemClick(int position, View v) {
            //Your code
        }
    });

使用这种界面设置方式,您可以准确地实现onclick侦听器功能。如果您还有问题,请告诉我。

作者: Bethan 发布者: 2017 年 4 月 18 日

0

1685 作者的声誉

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public Button btMyButton;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
               // your implementation. In order to get position you can use `getAdapterPosition()` method               
       }
    }
作者: CodeCameo 发布者: 2017 年 4 月 18 日
32x32