简体   繁体   English

单击我的 RecyclerView 列表中的项目时如何显示 onClick 消息?

[英]How to show an onClick message when clicking on an item of my RecyclerView list?

I am actually working on a group project and I want to develop a functionnality for our application.我实际上正在处理一个小组项目,我想为我们的应用程序开发一个功能。 My goal is to have a list of several items with their images and when I click on an Item of that list I want to have a text pop in the midle of the screen related to that particular item.我的目标是列出几个带有图像的项目,当我单击该列表中的一个项目时,我希望在与该特定项目相关的屏幕中间弹出一个文本。 I'm afraid I might be using the wrong technical Tools to do so.恐怕我可能使用了错误的技术工具来做到这一点。 I am actually using a csv file for the list details, an adapter and a viewHolder for the list.我实际上使用了一个 csv 文件作为列表的详细信息,一个适配器和一个 viewHolder 用于列表。 Since I have no idea on what is wrong and what to do I link a big part of my code so you can check how I did until now.由于我不知道出了什么问题以及该怎么做,因此我链接了我的大部分代码,以便您可以查看我到目前为止的表现。 I can also give you my xml files if you need to check them out, a really big thanks in advance to all the answers and time spent on my problem如果您需要检查它们,我也可以给您我的 xml 文件,非常感谢所有的答案和在我的问题上花费的时间

I already managed to have my list of items with the title and the picture (text from csv file) of each list item but I'm stuck on how to show a specific text for each ViewHolder.我已经设法让我的项目列表带有每个列表项的标题和图片(来自 csv 文件的文本),但我一直在思考如何为每个 ViewHolder 显示特定文本。

this is my Adapter这是我的适配器

public class Adapter extends RecyclerView.Adapter<ViewHolder> {

    List<Departement> list;
    Activity activity;

    public Adapter(List<Departement> list, Activity activity) {
        this.list = list;
        this.activity = activity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int itemType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.departement,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        Departement departement = list.get(position);
        viewHolder.bind(departement, activity);
    }

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

}

my ViewHolder我的 ViewHolder

public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView textViewView;
    private ImageView imageView;

    public ViewHolder(View itemView) {
        super(itemView);

        textViewView = (TextView) itemView.findViewById(R.id.text);
        imageView = (ImageView) itemView.findViewById(R.id.image);
    }

    public void bind(Departement departement, Activity activity){
        textViewView.setText(departement.getText());
        String uri = departement.getImageUrl();
        int imageResource = activity.getResources().getIdentifier(uri, null, activity.getPackageName());
        Drawable res = activity.getResources().getDrawable(imageResource);
        imageView.setImageDrawable(res);
    }
}

each item of the list is a Departement列表中的每一项都是一个部门

public class Departement {
    private String text;
    private String imageUrl;

    public Departement(String text, String imageUrl) {
        this.text = text;
        this.imageUrl = imageUrl;
    }

    public String getText() {
        return text;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
}

and finally my fragment最后是我的片段

public class FragmentEspecesProches extends Fragment {

    public final static char SEPARATOR=',';

    private RecyclerView recyclerView;

    private List<Departement> departementsList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_especes_proches, container, false);

        ajouterDepartements();

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new GridLayoutManager(this,2));

        recyclerView.setAdapter(new Adapter(departementsList, getActivity()));


        return view;
    }

    private void ajouterDepartements() {

        ArrayList<String> lines = new ArrayList<>();
        ArrayList<String[]> data = new ArrayList<>();
        String sep = new Character(SEPARATOR).toString();

        lines = UtilitaireResultat.readFile(getActivity().getResources().openRawResource(R.raw.departement));

        for(String line : lines){
            String[] oneData = line.split(sep);
            data.add(oneData);
        }

        for(int i=0 ; i<data.size() ; i++){
            String[] tabStr = data.get(i);
            departementsList.add( new Departement( tabStr[2]+" - "+tabStr[3] ,"@drawable/"+tabStr[5] ));
        }


    }

}

you can implement item click listener like this您可以像这样实现项目点击侦听器

   public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView tvName;
        public TextView tvHometown;
        private Context context;

        public ViewHolder(Context context, View itemView) {
            super(itemView);
            this.tvName = (TextView) itemView.findViewById(R.id.tvName);
            this.tvHometown = (TextView) itemView.findViewById(R.id.tvHometown);
            // Store the context
            this.context = context;
            // Attach a click listener to the entire row view
            itemView.setOnClickListener(this);
        }

        // Handles the row being being clicked
        @Override
        public void onClick(View view) {
            int position = getAdapterPosition(); // gets item position
            // if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
                User user = users.get(position);
                // We can access the data within the views
                Toast.makeText(context, tvName.getText(), Toast.LENGTH_SHORT).show();
           // }
        }
    }

Use onBindViewHolder to handle any interaction on your list items使用onBindViewHolder处理列表项上的任何交互

@Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        Departement departement = list.get(position);
        viewHolder.bind(departement, activity);
        viewHolder.itemView.setOnClickListener(//your action//);
    }

ItemView is the whole item; ItemView是整个项目; you can access your textviews or imageviews as you use it on your bind method, You can use your bind method to apply listeners.您可以在bind方法上使用textviewsimageviews textviews访问它,您可以使用bind方法来应用侦听器。

Handle on click of the item inside your ViewHolder constructor like ,单击 ViewHolder 构造函数中的项目,例如,

 public ViewHolder(View itemView) {
    super(itemView);
    textViewView = (TextView) itemView.findViewById(R.id.text);
    imageView = (ImageView) itemView.findViewById(R.id.image);
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              int position=getAdapterPosition();
              Toast.makeText(context, list.get(position).getText(), Toast.LENGTH_SHORT).show();
        }
    });
}

Create your onClickListner interface as将您的 onClickListner 界面创建为

interface RecylerViewItemClickListner 
{
    void onItemClick(Department item)
}

set the listner in Adapter class在 Adapter 类中设置监听器

private final RecylerViewItemClickListner mOnClickListener;
public Adapter(List<Departement> list, Activity activity) {
        this.list = list;
        this.activity = activity;
        this.mOnClickListener = activity;
    }

Now in ViewHolder class现在在 ViewHolder 类中

public void bind(final Departement item, final mOnClickListener listener) {

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    mOnClickListener.onItemClick(item);
                }
            });
        }

and change onBindViewHolder as below并更改 onBindViewHolder 如下

 @Override
 public void onBindViewHolder(ViewHolder holder, int position) {
            holder.bind(items.get(position), mOnClickListener);
        }

Override onItemClick(Department item) in activity覆盖活动中的 onItemClick(Department item)

@override
onItemClick(Department item)
{
  //show toast here...
}

implement OnClickListener in your ViewHolder class在您的 ViewHolder 类中实现 OnClickListener

  public class ViewHolder extends RecyclerView.ViewHolder implements
    View.OnClickListener
    {
                @Override
                public void onClick(View v)
                {
                   //do action
                }
    }

Implement below method in your ViewHolder class.在您的 ViewHolder 类中实现以下方法。

itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final User currentItem = users.get(getAdapterPosition());
                    Toast.makeText(mContext,currentItem.getText()+" is selected!",Toast.LENGTH_SHORT).show();

                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM