在RecyclerView.Adapter中单击传递数据 [英] Passing data on click in RecyclerView.Adapter

查看:221
本文介绍了在RecyclerView.Adapter中单击传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在片段上有一个RecyclerView,它显示了我从FTP服务器加载的某些文件. 我正在尝试为每个项目设置一个onClickListener,因此当单击它时,将显示一个DialogFragment,用户可以在其中选择是下载还是打开文件.
我的问题是:
我需要向我的对话框发送2件事:

I have a RecyclerView on my fragment that shows certain files i've loaded from an FTP Server. I'm trying to set an onClickListener for each item, so when it will be clicked, a DialogFragment will be shown, in which the user will be able to choose whether to download or open the file.
My problem is:
I need to send to my dialog 2 things:

  1. 目标片段,在用户选择要执行的操作后将下载/打开文件.使用setTargetFragment()可以轻松做到这一点,唯一的问题是我无法访问RecyclerView.ViewHolder中的片段.
  2. 要下载/打开的文件的名称.这样对话框将把它发送到目标片段.这里的问题是我无法访问RecyclerView.ViewHolder中的数据集,因为它是静态的,而我的数据集不是.
  1. The target fragment, which will download/open the file after the user chooses what to do. This can be easily dont with setTargetFragment(), the only problem is I dont have access to the fragment within my RecyclerView.ViewHolder.
  2. The name of the file to be downloaded/opened. So that the dialog will send that to the target fragment. The problem here is that I dont have access to the dataset within the RecyclerView.ViewHolder since its static and my dataset is not.

为什么要使用ViewHolder
如果有更好的选择,我很高兴听到.我尝试在RecyclerView.ViewHolder中执行此操作的原因是它可以访问该位置(被单击的项目).我已经考虑过在onBindViewHolder()上执行此操作,但是我可以访问数据集,但不能访问该位置.

Why on ViewHolder
If there's a better place to do that, I'd be happy to hear. The reason I'm trying to that within my RecyclerView.ViewHolder is that it has access to the position (the item that was clicked). I've thought about doing that on onBindViewHolder(), but there I have access to the dataset BUT NOT to the position.

我的适配器的代码:

public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.ViewHolder> {
    public final String TAG = "FILES_ADAPTER";
    private FTPFile[] dataset;

    //some methods and stuff...

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final String TAG = "FILES_VH";
        private final TextView nameTextView;
        private final TextView infoTextView;
        private final ImageView imageView;

        public ViewHolder(View v) {
            super(v);
            // Define click listener for the ViewHolder's View.
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "Element " + getPosition() + " clicked.");
                    //here i want to open the dialog fragment and start downloading/openning

                }
            });
            nameTextView = (TextView) v.findViewById(R.id.fileNameTextView);
            infoTextView = (TextView) v.findViewById(R.id.fileInfoTextView);
            imageView = (ImageView) v.findViewById(R.id.fileImageView);
        }

        public TextView getNameTextView() {
            return nameTextView;
        }

        public ImageView getImageView() {
            return imageView;
        }

        public TextView getInfoTextView() {
            return infoTextView;
        }
    }

}

非常感谢您的帮助.

推荐答案

所以我找到了解决方案,我在onBindViewHolder()上用匹配的参数创建了一个自定义的OnClickListener,并将其设置为:
ViewHolder:

So I've found a solution for this, I created a custom OnClickListener on onBindViewHolder() with the matching parameters, and set it to the ViewHolder:
ViewHolder:

 public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView nameTextView;
        private final TextView infoTextView;
        private final ImageView imageView;
        public final String TAG = "FILES_VH";
        private View v;

        public ViewHolder(View v) {
            super(v);
            this.v = v;
            nameTextView = (TextView)v.findViewById(R.id.fileNameTextView);
            infoTextView = (TextView)v.findViewById(R.id.fileInfoTextView);
            imageView = (ImageView)v.findViewById(R.id.fileImageView);
        }

        public void setOnClickListener(View.OnClickListener listener){
            v.setOnClickListener(listener);
        }

}

我的自定义侦听器:

private class downloadOnClickListener implements View.OnClickListener{
        RemoteFilesFragment fragment;
        String file;

        public downloadOnClickListener(RemoteFilesFragment fragment, String file){
            this.file = file;
            this.fragment = fragment;
        }

        @Override
        public void onClick(View v) {
            Log.d(TAG, "File " + file + " clicked.");
            DownloadDialog dialog = DownloadDialog.newInstance(file);
            dialog.setTargetFragment(fragment, 1);
            Log.e(TAG, fragment == null ? "fragment null" : "fragment not null");
            FragmentManager fm = fragment.getActivity().getSupportFragmentManager();
            dialog.show(fm, "Download");
        }
    }

,然后在onBindViewHolder()上添加以下代码:

holder.setOnClickListener(new downloadOnClickListener(fragment, dataset[position].getName()));

这篇关于在RecyclerView.Adapter中单击传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆