Make Recycler View show rows from bottom

后端 未结 6 828
遇见更好的自我
遇见更好的自我 2021-01-03 17:24

I saw somewhere method to make RecyclerView show ViewHolders from bottom to top. Now, i can\'t find it anywhere (after half of hour going through <

相关标签:
6条回答
  • 2021-01-03 18:07

    If you are using LinearLayoutManager make third param (reverseLayout) false

    LinearLayoutManager linearLayoutManager =
                new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    
    0 讨论(0)
  • 2021-01-03 18:12

    Solution on your query:


    LinearLayoutManager layoutManager=
                    new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,true);
                    layoutManager.setStackFromEnd(true);
            recyclerView.setLayoutManager(layoutManager); 
    

    In above code recyclerView is the id of RecyclerView and layoutManager is object of LinearLayoutManager.

    0 讨论(0)
  • 2021-01-03 18:17

    Is it LinearLayoutManager.setStackFromEnd(true) you are looking for?

    Edit

    Turns out LinearLayoutManager.setReverseLayout(true) does the trick. Either way, the reader may want to try each of the methods and the combination of both to get the needed effect.

    0 讨论(0)
  • 2021-01-03 18:17

    Here is the solution in Kotlin

    val llm = LinearLayoutManager(this)
    llm.stackFromEnd = true     // items gravity sticks to bottom
    llm.reverseLayout = false   // item list sorting (new messages start from the bottom)
    
    rv_chat_history.layoutManager = llm
    

    Or if you like the apply method:

    recycler.apply { 
        layoutManager = LinearLayoutManager(this).apply { 
            stackFromEnd = true
            reverseLayout = true
        }
    }
    
    0 讨论(0)
  • 2021-01-03 18:18

    You can achieve this task by adding two lines to the xml code.

    app:stackFromEnd="true"
    app:reverseLayout="true"
    

    This will work as all chat apps.

    0 讨论(0)
  • 2021-01-03 18:27

    This is working for me in kotlin

    recyclerView?.scrollToPosition(mArrayList.size - 1)
    
    0 讨论(0)
提交回复
热议问题