简体   繁体   English

每次将项目添加到列表时如何使RecyclerView不会滚动到底部

[英]How to make RecyclerView not scroll to the bottom each time an item is added to the list

I want my RecyclerView to be scrolled to the bottom when my chat loads up but when the user manually scrolls up the list to read previous messages it should not scroll back down again upon the addition of new item in the list unless the message(new item) is sent/added by the user himself. 我希望在我的聊天加载时将RecyclerView滚动到底部,但是当用户手动向上滚动列表以阅读以前的消息时,除非在列表中添加新项,否则不应再次向下滚动,除非出现message(new item )由用户自己发送/添加。

I have tried using both layoutManager.setStackFromEnd(true) and recyclerView.scrollToPosition(list.size() - 1). 我已经尝试同时使用layoutManager.setStackFromEnd(true)和recyclerView.scrollToPosition(list.size()-1)。 Both of these methods automatically scroll down the list upon receiving a new message if I have scrolled up manually it should not scroll down to the bottom again unless im the one sending the message. 如果我手动向上滚动,这两种方法都会在接收到新消息时自动向下滚动列表,除非我发送消息,否则不应再次向下滚动到底部。

mref.child(RoomName).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()) {
            ChatMessage value = dataSnapshot1.getValue(ChatMessage.class);
            ChatMessage fire = new ChatMessage();
            String msgtxt = value.getMessageText();
            String user=value.getMessageUser();
            long msgtime=value.getMessageTime();
            String  prothumb=value.getProfuri();
            String sentimguri=value.getSentimguri();
            String type=value.getType();

            fire.setMessageUser(user);
            fire.setMessageText(msgtxt);
            fire.setMessageTime(msgtime);
            fire.setProfuri(prothumb);
            fire.setSentimguri(sentimguri);
            fire.setType(type);
            list.add(fire);
        }
        adapter = new RecyclerViewAdapter(ChatRoom.this, list);
        recyclerView.setAdapter(adapter);

        //layoutManager.setStackFromEnd(true);
    }

    //Initializing RecyclerView
    private void initRecyclerView() {
        //Log.d(TAG, "initRecyclerView: init recyclerview.");
        recyclerView =(RecyclerView) findViewById(R.id.list_of_messages);
        recyclerView.setHasFixedSize(true);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //recyclerView.scrollToPosition(list.size() - 1);
    }
}

The issue I can see is that you are creating a new instance of adapter everytime when data changes. 我看到的问题是,每次数据更改时,您都在创建适配器的新实例。 Which explains why it always scroll down to bottom. 这就解释了为什么它总是向下滚动到底部。 And it's not a proper way of using a recyclerView. 这不是使用recyclerView的正确方法。

What you need to do is to create just one instance of adapter during initRecyclerView using an empty list. 您需要做的是使用空列表在initRecyclerView期间仅创建一个适配器实例。 Then whenever data changes, update the existing list and then call adapter.notifyDataSetChanged(). 然后,每当数据更改时,更新现有列表,然后调用adapter.notifyDataSetChanged()。 This will give you the behaviour that you wanted. 这将为您提供所需的行为。

Upon getting the chat list , you need not have to create the new instance of adapter everytime .Instead just notify the adapter about the change . 获取聊天列表后,您不必每次都创建适配器的新实例。相反,只需将更改通知给适配器即可。

mref.child(RoomName).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
       list.clear();
    for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()) {
        ChatMessage value = dataSnapshot1.getValue(ChatMessage.class);
        ChatMessage fire = new ChatMessage();
        String msgtxt = value.getMessageText();
        String user=value.getMessageUser();
        long msgtime=value.getMessageTime();
        String  prothumb=value.getProfuri();
        String sentimguri=value.getSentimguri();
        String type=value.getType();

        fire.setMessageUser(user);
        fire.setMessageText(msgtxt);
        fire.setMessageTime(msgtime);
        fire.setProfuri(prothumb);
        fire.setSentimguri(sentimguri);
        fire.setType(type);
        list.add(fire);
    }

  if(!(recyclerView.canScrollVertically(1)))
            {
            recyclerView.smoothScrollToPosition(adapter.getItemCount());
            }

    adapter.notifyDataSetChanged();
    //layoutManager.setStackFromEnd(true);
}

//Initializing RecyclerView
private void initRecyclerView() {
    //Log.d(TAG, "initRecyclerView: init recyclerview.");
    recyclerView =(RecyclerView) findViewById(R.id.list_of_messages);
    recyclerView.setHasFixedSize(true);
    layoutManager=new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    //initialse your adapter only once 
    adapter = new RecyclerViewAdapter(ChatRoom.this, list);        
    recyclerView.setAdapter(adapter);
    layoutManager.setStackFromEnd(true);

    //recyclerView.scrollToPosition(list.size() - 1);
}

} }

This might will solve your issue . 这可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM