3

My task is to develop a list of different type of questions, a survey. It could include types like Integer-Answer-Question, Long-Text-Answer-Question, and so on.

Why is it needed to be a list? Because for the people using the app is way better to scrolldown answering each question rather than swiping to right, or doing another movement.

So I was face to face with the dilemma of using a ListView or a RecyclerView. My research gave the final outcome of using a recyclerView and having a viewHolder for the different types of questions that I have.

The struggle came when I realized that there is a type of question that has dependency related to it; how so? if you select one option then you have to "show" some questions, and if you deselected this option then you have to "hide" it again.

The thing is that I need to know the reference of each question to their viewHolder in order to "show" o "hide" each of them, but if the recycler is recycling viewHolders then it could create a mess on my logic.

My punctual questions are: Am I using the correct component with the RecyclerView?, is there any way to access a viewHolder with a unique reference, like and id or something?.

if you need me to show some code, I'd do it happily.

Valuable information:

if you are interest in how notifyDataSetChanged() works you can access to this link for further and detail information.

Minas mina's approach was the correct one!

cancinos
  • 43
  • 1
  • 8

1 Answers1

0

If I understand correctly, you need to hide some types of questions when the user selects an option.


Your understanding of what the view holders are supposed to do is not quite right. The view holders cache a bunch of views that you later use in onBindViewHolder() to fill-in data from your model objects.

The actual model objects should be in your adapter. In your case, something like

List<Question> questions

In onBindViewHolder(), you fill-in the fields of the View holder with the data from a Question object.

As for your question, what you can do is to set a flag in your recyclerView adapter, e.g. hideQuestionsOfTypeA to true and then call notifyDatasetChanged() on the adapter.

When binding objects, check if that flag is true and if yes, set the visibility to GONE to the views that need to be hidden.

Minas Mina
  • 2,058
  • 3
  • 21
  • 35
  • What I understand about how onBindViewHolder() works comes from here: https://stackoverflow.com/a/37524217/5352887, it says that "Instead of creating new view for each new row, an old view is recycled and reused by binding new data to it." So if I hide a viewHolder (by hiding I mean setting LayoutParams to 0) then if it is associated to another question it would replicate the same behave. Am I wrong with this? – cancinos Mar 01 '18 at 22:57
  • @PabloAndrésCancinos that is a good explanation. I do not see how it would be a problem to hide some questions depending on a flag, like I suggested above :) Let me know if I haven't understood the problem correctly. – Minas Mina Mar 01 '18 at 22:58
  • 1
    @PabloAndrésCancinos Yes, you are (thankfully :) ) wrong! A View Holder can only be "associated" with one question _at a time_. So making changes in a view holder will not affect any other items on the screen. I encourage you to find a recycler view sample project only and play with it to better understand how it works. – Minas Mina Mar 01 '18 at 23:04
  • 1
    It is all clear now! I'm not at the office right now but I'll test it tomorrow morning! Save my day, man! – cancinos Mar 02 '18 at 00:38
  • @cancinos No problem. Can you please accept the answer if it helped you? – Minas Mina Mar 02 '18 at 06:52