Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recycler view does not scroll to last item when keyboard is opened #41

Open
chitrey opened this issue Jan 17, 2017 · 3 comments
Open

Recycler view does not scroll to last item when keyboard is opened #41

chitrey opened this issue Jan 17, 2017 · 3 comments

Comments

@chitrey
Copy link

chitrey commented Jan 17, 2017

I think this is RecyclerView issue. Could you please tell me how to fix it or fix it in the upcoming update. Many thanks

@camclendenin
Copy link

@chitrey I solved this by adding the following in my activity which contains the slyce messaging fragment. It's a bit ugly IMO but it works.

At the end of onCreate() I added...

        final View contentView = getRecyclerView();
        contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect r = new Rect();
                contentView.getWindowVisibleDisplayFrame(r);
                int screenHeight = contentView.getRootView().getHeight();

                // r.bottom is the position above soft keypad or device button.
                // if keypad is shown, the r.bottom is smaller than that before.
                int keypadHeight = screenHeight - r.bottom;

                if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
                    // keyboard is opened
                    if (!scrollingToBottom) {
                        scrollingToBottom = true;
                        scrollRecyclerViewToBottom(getRecyclerView());
                    }
                }
                else {
                    // keyboard is closed
                    scrollingToBottom = false;
                }
            }
        });
    private static void scrollRecyclerViewToBottom(RecyclerView recyclerView) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter != null && adapter.getItemCount() > 0) {
            recyclerView.scrollToPosition(adapter.getItemCount() - 1);
        }
    }

@RodrigoBertotti
Copy link

Thank you @camclendenin

@moraruuvasile
Copy link

    message_recycler.apply {
        val layout = LinearLayoutManager(context)
        layout.stackFromEnd = true
        layoutManager = layout
        adapter = chatsAdapter
    }

in another listener where change my data
chatsAdapter.notifyDataSetChanged()
message_recycler.scrollToPosition(listMessage.size-1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants