java 您如何在回收站视图中使用 OnClickListener?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29505930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:23:29  来源:igfitidea点击:

How do you use an OnClickListener in a recycler view?

javaandroidandroid-studioonclicklistenerandroid-recyclerview

提问by brandin

What i'm basically trying to do is make the objects that show up in the recycler view clickable to a certain TextView id because i'm making a program that shows an album cover and its title next to it in a list. I need to be able to click on each one of the boxes that the recycler view makes and have a TextView pop up with the other information (author, published date, hit songs, etc) when its clicked on and then a back button (if possible) to go back to the album list. I've been looking at this for hours and cant figure out how to make an OnclickListener work for it. If you know how or have any suggestions id be glad to hear them. Thank you!

我基本上要做的是使回收器视图中显示的对象可点击到某个 TextView id,因为我正在制作一个程序,在列表中显示专辑封面及其旁边的标题。我需要能够点击回收者视图制作的每个框,并在点击时弹出一个带有其他信息(作者、发布日期、热门歌曲等)的 TextView,然后是一个后退按钮(如果可能)返回专辑列表。我已经研究了几个小时,但无法弄清楚如何让 OnclickListener 为它工作。如果您知道如何或有任何建议,我很高兴听到他们。谢谢!

package com.albumlist.albumlist;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private AlbumData[] itemsData;

    public MyAdapter(AlbumData[] itemsData){
        this.itemsData = itemsData;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView txtViewTitle;
        private ImageView imgViewIcon;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            itemLayoutView.setOnClickListener(this);
            txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.album_title);
            imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.album_icon);
        }

        @Override
        public void onClick(View v) {

        }
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {

        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.data_layout, null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
        viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
    }

    @Override
    public int getItemCount() {
        return itemsData.length;
    }

}

回答by AlleyOOP

The concept is well summed up by Xaver Kapellerin the comments. If you are looking for a simple way to manage your RecyclerViewinteraction similar to the traditional interactions of a ListView, check out BigNerdRanch's recyclerview-multiselectlibrary on GitHub. They have a sample app that you can explore, which implements series of OnClickListenersand OnLongClickListenerswith added capabilities of multi-selection.

Xaver Kapeller在评论中很好地总结了这个概念。如果您正在寻找一种RecyclerView类似于 传统交互的简单方法来管理您的交互ListView,请查看GitHub 上的BigNerdRanch 的 recyclerview-multiselect库。他们有一个您可以探索的示例应用程序,它实现了一系列OnClickListenersOnLongClickListeners增加了多选功能。

Here's a snippet of how BigNerdRanch implements listeners in a Fragmentacross an Adapterand a ViewHolder, which in this case is actually an extension of the library's own SwappingHolder.

以下是如何BigNerdRanch工具听众在一个片段Fragment跨越AdapterViewHolder,在这种情况下,实际上是图书馆自身的延伸SwappingHolder.

public CrimeHolder(View itemView) {
        super(itemView, mMultiSelector);

        mTitleTextView = (TextView) itemView.findViewById(R.id.crime_list_item_titleTextView);
        mDateTextView = (TextView) itemView.findViewById(R.id.crime_list_item_dateTextView);
        mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.crime_list_item_solvedCheckBox);
        itemView.setOnClickListener(this);
        itemView.setLongClickable(true);
        itemView.setOnLongClickListener(this);
    }

    public void bindCrime(Crime crime) {
        mCrime = crime;
        mTitleTextView.setText(crime.getTitle());
        mDateTextView.setText(crime.getDate().toString());
        mSolvedCheckBox.setChecked(crime.isSolved());
    }

    @Override
    public void onClick(View v) {

        if (mCrime == null) {
            return;
        }
        if (!mMultiSelector.tapSelection(this)) {
            selectCrime(mCrime);
        }

    }


    @Override
    public boolean onLongClick(View v) {

        ((AppCompatActivity) getActivity()).startSupportActionMode(mDeleteMode);
        mMultiSelector.setSelected(this, true);
        return true;
    }


}


private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup parent, int pos) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_crime, parent, false);
        return new CrimeHolder(view);
    }

    @Override
    public void onBindViewHolder(CrimeHolder holder, int pos) {
        Crime crime = mCrimes.get(pos);
        holder.bindCrime(crime);
        Log.d(TAG, "binding crime" + crime + "at position" + pos);
    }

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