0

I have to check if the RecyclerView is able to layoutManager.findLastCompletelyVisibleItemPosition() always returns -1 for me .This is my code. I refer it form Check if RecyclerView is scrollable

 private void setAdapterData() {
        mChatAdapter = new ChatAdapter(mMessagesList);
        mChatMessagesRecyclerView.setAdapter(mChatAdapter);
        boolean ss=isRecyclerScrollable();
        Log.e("ss",ss+""+mMessagesList.size()+"ll");
    }

    public boolean isRecyclerScrollable() {
        LinearLayoutManager layoutManager = (LinearLayoutManager) mChatMessagesRecyclerView.getLayoutManager();

        if (layoutManager == null || mChatAdapter == null) return false;
        Log.e("ss",layoutManager.findLastCompletelyVisibleItemPosition() +"ll");
        return layoutManager.findLastCompletelyVisibleItemPosition() < mChatAdapter.getItemCount() - 1;

}

My actual code is

private void setAdapterData() {
        mChatAdapter = new ChatAdapter(mMessagesList);
        mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mChatMessagesRecyclerView.setAdapter(mChatAdapter);
        if(isRecyclerScrollable())
            mLayoutManager.setStackFromEnd(true);
        mChatMessagesRecyclerView.setLayoutManager(mLayoutManager);

    }
    public boolean isRecyclerScrollable() {

            RecyclerView.Adapter adapter = mChatMessagesRecyclerView.getAdapter();
            if (mLayoutManager == null || adapter == null) return false;

            return mLayoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
        }

I am actually looking answer for this question

set setStackFromEnd parameter to recycler view based on some condition check

my current working code is

if(mMessagesList.size()>5)
                    setStackFromEndLayoutManager();
                setAdapterData();

private void setStackFromEndLayoutManager() {
        mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mLayoutManager.setStackFromEnd(true);
        mChatMessagesRecyclerView.setLayoutManager(mLayoutManager);
    }

i hard coded here mMessagesList.size()>5 ... i have to make it as dynamic..so i looking for answer Check if RecyclerView is scrollable??/

Community
  • 1
  • 1

3 Answers3

0

hii please add following code in CalladapterMethod

private void setAdapterData() {
    mChatAdapter = new ChatAdapter(mMessagesList);
    mChatMessagesRecyclerView.setAdapter(mChatAdapter);
    boolean ss=isRecyclerScrollable();
    Log.e("ss",ss+""+mMessagesList.size()+"ll");
mChartAdapter.notifyDataSetChanged();
}
Rahul Karande
  • 259
  • 2
  • 9
0

From your code, You are setting

mChatMessagesRecyclerView.setLayoutManager(mLayoutManager);

after your condition check where you are looking for

mLayoutManager.findLastCompletelyVisibleItemPosition()

if your condition if(mMessagesList.size()>5) is false, the layout manager is not set in the recyclerview and it will not have any visible items.Set it before the condition.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • actually my expectation is to replace if(mMessagesList.size()>5) this condition because it is static instead i have to make the funactional call like this – Dineshkumar Arunachalam Dec 30 '16 at 07:36
  • public boolean isRecyclerScrollable() { LinearLayoutManager layoutManager = (LinearLayoutManager) mChatMessagesRecyclerView.getLayoutManager(); if (layoutManager == null || mChatAdapter == null) return false; Log.e("ss",layoutManager.findLastCompletelyVisibleItemPosition() +"ll"); return layoutManager.findLastCompletelyVisibleItemPosition() < mChatAdapter.getItemCount() - 1; – Dineshkumar Arunachalam Dec 30 '16 at 07:37
  • But it not works well ....layoutManager.findLastCompletelyVisibleItemPosition() always return -1 – Dineshkumar Arunachalam Dec 30 '16 at 07:37
  • because you are setting that layout manager to recyclerview after condition check..so it cant find your items..set the layout manager to your recyclerview before the isScrollable method is called – Suraj Rao Dec 30 '16 at 07:39
0

Try this setAdapterData(). And call this before all other things.

private void setAdapterData() {
    mChatAdapter = new ChatAdapter(mMessagesList);

    mLayoutManager = new LinearLayoutManager(getApplicationContext());
    mChatMessagesRecyclerView.setLayoutManager(mLayoutManager);

    mChatMessagesRecyclerView.setAdapter(mChatAdapter);

    if(isRecyclerScrollable())
        mLayoutManager.setStackFromEnd(true);

}
public boolean isRecyclerScrollable() {
        RecyclerView.Adapter adapter = mChatMessagesRecyclerView.getAdapter();
        if (mLayoutManager == null || adapter == null) return false;
            return mLayoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
}

And you don't need setStackFromEndLayoutManager() method

Charu
  • 1,055
  • 13
  • 18