0

I am using a RecyclerView with 2 view types. I want to handle click events on individual types seperately. I can do this within my Adapter, in the onBindViewHolder method. But one of my view type has to rerender the activity because it changes the RecyclerView data.

Basically it is a file explorer application, one view type is for Files, the other is for Folders. If user taps on Folders, I want to rerender the RecyclerView with "folder path".

I researched this issue, but I feel like I got lost with different onItemClickListener implementations for RecyclerView and invoking Activity methods from the adapter which I think it creates quite a mess.

ListView seems to be the way to go for my situation with its simple onItemClickListener method but it is not optimized as the RecyclerView.

What might be the cleanest approach for this?

thefrogking
  • 31
  • 1
  • 7
  • please share code sample – Faisal Naseer Jul 18 '18 at 12:03
  • So let me get this straight, you want to click on a folder and the recyclerview for files should show the files from the clicked folder, if that's it what you could do is delegate the click events from the recyclerview to the activity and when u click u send the path to the activity the activity then gets the file list and updates the other recyclerview with them – Alban Gashi Jul 18 '18 at 12:07
  • @AlbanGashi I want to refresh same RecyclerView with clicked folder path. Is there any implementation to handle click events in activity for RecyclerView that you can recommend? – thefrogking Jul 18 '18 at 12:17
  • Follow this link http://www.digitstory.com/recyclerview-multiple-viewholders/ – Pranav Darji Jul 18 '18 at 12:53
  • I used [this](https://stackoverflow.com/a/26196831/7183794) answer. But it overrides click events in onBindViewHolder, so I cannot detect click events for sub views in the items. It kind of solves my problem, but not throughly. – thefrogking Jul 18 '18 at 13:06

1 Answers1

0

I will give you an example of how it could look like:

//The interface the activity implements
    public interface DirectoryClickListener {
        void onClick(Directory directory)
    }

//The Activity
    public class DirectoryActivity extends AppCompatActivity implements DirectoryClickListener {
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            adapter = new DirectoryAdapter(this);
        }

        @Override
        public void onClick(Directory directory) {
        //yourcode
        }
    }

//The adapter constructor
    public DirectoryAdapter(DirectoryClickListener listener) {
        this.listener = listener;
    }

//OnBindViewHolder you call the onClick to send the directory to the activity where from there you get the files list and refresh the adapter data
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.itemView.setOnClickListener(view -> {
            listener.onClick(getItem(position));
        });
    }
Alban Gashi
  • 538
  • 3
  • 13