0

I have one quiz layout in which I am using recyclerview to show questions. I am using LinearLayoutManager for the recyclerview adapter. What I need is that whenever the user selects an option from the given questions, the next question should scroll to one position up (to the top of the layout). I tried to use different ways to get this but the problem is that scrolling occurs but it's not reaching to the top of the layout. It's scrolling in such way that the question and options are visible in the layout screen completely but not at the top it's showing in the down section. Code snippet in short

 recyclerView = view.findViewById(R.id.recyclerView);
     mLayoutManager = new LinearLayoutManager(context);
 recyclerView.scrollToPosition(Qno+1);
//        recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, new RecyclerView.State(), (Qno+1));

I tried both scrolltoposition and smoothscrolltoposition, but i am getting the same result. Attaching the screenshot for the better explanation. Here what I am getting after clicking any option on Q.5 enter image description here

This is what I am expecting, to load Q.6 in the top of the layout. enter image description here

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
Amit Sen
  • 177
  • 2
  • 12

1 Answers1

0

I suppose that you have a list of questions (java or Kotlin objects) and you want to scroll to Item with position = 20 (Qno = 20). Then you need to use code snippet I added below:

recyclerView.scrollToPosition(questions.indexOf(question))

enter image description here

Mohammad Asheri
  • 322
  • 1
  • 7
  • Thanks for your reply, But the issue is something different, i have uploaded image in which u can see that scrolling is working but its like after scrolling the new question is not reaching to the top of the layout. I want it like the second image that i uploaded, (it means no previous question in the layout only the next question and if free space is left after uploading the next question then the next to next should be shown). But with my code and using your code i am getting new question along with previous question in free space of the layout – Amit Sen Mar 04 '19 at 06:44