In the side menu with a dozen items, clicking on each of them calls the method: recyclerView.scrollToPosition (position); If we click in the side menu in ascending order (1-10), then when scrolling the list to the desired item, this item is displayed at the bottom of the screen. And if you press (10 - 1), the list item is displayed at the top of the screen.

How to make that when you go to the selected item, it is always displayed at the top of the screen?

enter image description here

enter image description here

public class MainActivity extends AppCompatActivity public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.nav_people) { recyclerView.scrollToPosition(position); 

 public class CommandAdapter extends SectionedRecyclerViewAdapter<CommandAdapter.ViewHolder> { private CharSequence[][] arrayComand; String[] section; TypedArray icons; public CommandAdapter(CharSequence[][] arrayComand, String[] section, TypedArray icons) { this.arrayComand = arrayComand; this.section = section; this.icons = icons; } @Override public int getSectionCount() { return arrayComand.length; } @Override public int getItemCount(int section) { return arrayComand[section].length; } @Override public void onBindHeaderViewHolder(CommandAdapter.ViewHolder holder, int section) { holder.title.setText(this.section[section]); holder.viewIcon.setImageResource(icons.getResourceId(section, -1)); } @Override public void onBindViewHolder(CommandAdapter.ViewHolder holder, int section, int relativePosition, int absolutePosition) { holder.tvDescription.setText(arrayComand[section][relativePosition]); } @Override public CommandAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { int layout; switch (viewType) { case VIEW_TYPE_HEADER: layout = R.layout.list_item_header; break; case VIEW_TYPE_ITEM: layout = R.layout.item; break; default: layout = R.layout.list_item_header; break; } View v = LayoutInflater.from(parent.getContext()) .inflate(layout, parent, false); return new ViewHolder(v); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView tvDescription; public TextView title; private ImageView viewIcon; public ViewHolder(View v) { super(v); title = (TextView) v.findViewById(R.id.title); tvDescription = (TextView) v.findViewById(R.id.tv_description); viewIcon = (ImageView) v.findViewById(R.id.ivIcon); } } } 

    1 answer 1

    Perhaps this will help you. LinearLayoutManager has a method:

     layoutManager.scrollToPositionWithOffset(index, offset); 

    you need to specify offset = 0

    • Thank you for what the doctor prescribed. - Nikolay Later September