简体   繁体   中英

Passing data on click in RecyclerView.Adapter

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. 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.

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.

Code of my Adapter:

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;
        }
    }

}

Help will be much appreciated, thanks.

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);
        }

}

My custom 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");
        }
    }

and on onBindViewHolder() add this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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