how smooth scroll recyclerview when additem?

I want when all item recyclerview added using Observable.interval and additem(scrollToPosition and notifyDataSetChanged) ; recyclerview smoothly scroll animation like this video :

https://file.io/bBu43O1A7g8g

I use smoothscrolltoposituion but done scroll with animation like that video...

please help me for this problem this code addeed item to adapter and scroll smooth to position

    public static void rxcode_load_chat(Activity activity, ArrayList<ChatModule_Model> chatList, ChatList_Adapter chatList_adapter, RecyclerView recyclerView, String state_first_call, String state_nxt_lsn, ChatList_Fragment chatlistFragment, Market_ViewModel market_viewModel, LinearLayoutManager layoutManager) {


        try {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    try {
                        newChat = new ArrayList<>();
                        newChat.addAll(chatList);
                        BaseActivity.state_listlsn = "";


                        _chatList_adapter = chatList_adapter;
                        _chatlistFragment = chatlistFragment;
                        _layoutManager = layoutManager;
                        _activity = activity;
                        _recyclerView = recyclerView;
                        observable = Observable.zip(Observable.create(new ObservableOnSubscribe<ChatModule_Model>() {
                                    @Override
                                    public void subscribe(ObservableEmitter<ChatModule_Model> emitter) throws Exception {

                                        if (newChat != null) {

                                            for (ChatModule_Model chat : newChat) {

                                                emitter.onNext(chat);
                                                recentChat.add(chat);
                                            }
                                        }
                                        emitter.onComplete();
                                    }
                                }),
                                Observable.interval(890, TimeUnit.MILLISECONDS),
                                (obs, timer) -> obs)
                                .doOnNext(item -> {
                                });
                        observer = new DisposableObserver<ChatModule_Model>() {
                            @Override
                            public void onNext(ChatModule_Model chat) {

                                try {
                                    part_name = "";
                                    part_id = "";
                                    state_nxt_lsn_ = state_nxt_lsn;
                                    lsn_id_ = "";
                                    lsn_name_ = "";
//                            Log.e("TAG", "onNext: last items: "+chat.getPart_id() );
                                    last_partid_learn.add(chat.getPart_id());


                                    if (last_partid_learn.size() != 0) {

                                        if (last_partid_learn.size() - 2 < 0) {

                                        } else {

                                            if (chat.getPart_id().equals(last_partid_learn.get(last_partid_learn.size() - 2))) {

                                            } else {

                                                parts_list_learned_user.add(chat.getPart_id());
                                                if (parts_list_learned_user.size() == BaseActivity.parts_list.size()) {

                                                    if (BaseActivity.lsns_list.size() == 0) {
                                                        Intent in = new Intent("active_comming");
                                                        LocalBroadcastManager.getInstance(_activity).sendBroadcast(in);

                                                        observer.dispose();
                                                        Toast.makeText(activity, "completed list", Toast.LENGTH_SHORT).show();
                                                        ((AppCompatActivity) activity).getSupportFragmentManager().popBackStack();
                                                    } else {

                                                        Gson gson = new Gson();
                                                        String json = gson.toJson(chatList_adapter.getlist());


                                                        int index_partid = BaseActivity.parts_list.size() - 1;

                                                        String cur_part_id = BaseActivity.parts_list.get(BaseActivity.parts_list.size() - 1).getPart_id();


                                                        BaseActivity.part_id = cur_part_id;
                                                        Intent in = new Intent("SaveLastPart_");
                                                        in.putExtra("part_id_", cur_part_id);
                                                        in.putExtra("tbl_chats", json);
                                                        LocalBroadcastManager.getInstance(_activity).sendBroadcast(in);


                                                        BaseActivity.state_listlsn = "lsn";

                                                        lsn_id_ = BaseActivity.lsns_list.get(BaseActivity.Lsn_position_live).getLsn_id();
                                                        lsn_name_ = BaseActivity.lsns_list.get(BaseActivity.Lsn_position_live).getLsn_name();

                                                        Log.e("TAG", "onNext:aaaa Lsn_position_live  " + BaseActivity.Lsn_position_live + "   " + lsn_name_);


                                                        if (BaseActivity.lsns_list.size() != 0) {
                                                            Log.e("TAG", "onNext: lsnnammeeee  " + lsn_id_);
                                                            Log.e("TAG", "onNext: lsnnammeeee  " + lsn_name_);
                                                            Intent intent = new Intent("request_newlsn");
                                                            intent.putExtra("lsn_id", lsn_id_);
                                                            intent.putExtra("lsn_name", lsn_name_);


                                                            LocalBroadcastManager.getInstance(_activity).sendBroadcast(intent);
                                                        }

                                                        observer.dispose();
                                                        Dialog dialog = new Dialog(_activity, android.R.style.Theme_Light);
                                                        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                                                        dialog.setContentView(R.layout.dialog_nxt_lsn);
                                                        ImageView img_clction_passed = dialog.findViewById(R.id.img_clction_passed);
                                                        TextView name_lsn_passed = dialog.findViewById(R.id.name_lsn_passed);
                                                        TextView btn_continue = dialog.findViewById(R.id.btn_continue);
                                                        dialog.setCancelable(false);

                                                        btn_continue.setOnClickListener(new View.OnClickListener() {
                                                            @Override
                                                            public void onClick(View view) {

                                                                int size_lsn_list = BaseActivity.lsns_list.size() - 1;

                                                                cur_lsn_name = BaseActivity.lsns_list.get(BaseActivity.Lsn_position_live).getLsn_name();

                                                                Log.e("TAG", "onClick:ffffffffffff " + cur_lsn_name);
                                                                Log.e("TAG", "onClick:ffffffffffff " + BaseActivity.lsns_list.get(size_lsn_list).getLsn_name());
                                                                if (cur_lsn_name.equals(BaseActivity.lsns_list.get(size_lsn_list).getLsn_name())) {


                                                                    Intent in = new Intent("active_comming");
                                                                    LocalBroadcastManager.getInstance(_activity).sendBroadcast(in);


                                                                    ((AppCompatActivity) activity).getSupportFragmentManager().popBackStack();

                                                                    dialog.dismiss();
                                                                } else {

                                                                    chatList_adapter.clearList();
                                                                    BaseActivity.Lsn_position_live++;
                                                                    chatlistFragment.setNewChat(chatlistFragment, BaseActivity.lsns_list.get(BaseActivity.Lsn_position_live).getLsn_id(), BaseActivity.lsns_list.get(BaseActivity.Lsn_position_live).getLsn_name(), chatList_adapter, _activity, market_viewModel);

                                                                    dialog.dismiss();
                                                                }


//                                                    }


                                                            }
                                                        });


                                                        name_lsn_passed.setText("You passed " + lsn_name_ + " successfully");


                                                        if (!BaseActivity.cln_logo.equals("")) {
                                                            Glide
                                                                    .with(_activity)
                                                                    .load(BaseActivity.cln_logo)

                                                                    .into(img_clction_passed);


                                                        }
                                                        dialog.show();
                                                    }

                                                } else {
                                                    for (int r = 0; r < BaseActivity.parts_list.size() - 1; r++) {


                                                        if (!last_partid_learn.get(last_partid_learn.size() - 1).equals(BaseActivity.parts_list.get(r).getPart_id())) {


                                                            Gson gson = new Gson();
                                                            String json = gson.toJson(chatList_adapter.getlist());

                                                            String cur_part_id = BaseActivity.parts_list.get(r).getPart_id();
                                                            Log.e("TAG", "SaveLastPart_Request cur_part_id: bbb " + BaseActivity.parts_list.get(r).getPart_id());
                                                            BaseActivity.part_id = cur_part_id;
                                                            Intent in = new Intent("SaveLastPart_");
                                                            in.putExtra("part_id_", "currr");
                                                            in.putExtra("tbl_chats", json);
                                                            LocalBroadcastManager.getInstance(_activity).sendBroadcast(in);
                                                            try {
                                                                BaseActivity.setPreference(
                                                                        _activity,
                                                                        "part_id",
                                                                        BaseActivity.parts_list.get(r).getPart_id()
                                                                );

                                                                if (runningTask != null)
                                                                    runningTask.cancel(true);
                                                                runningTask = new MyTask();
                                                                runningTask.execute();


                                                            } catch (Exception e) {
                                                                Log.e("TAG", "onNext:vvvv " + e.getMessage());
                                                                e.printStackTrace();
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (parts_list_learned_user.size() == BaseActivity.parts_list.size()) {


                                        _recyclerView.smoothScrollToPosition(_chatList_adapter.getItemCount() - 1);
                                    } else {
                                        if (chat.getModule_type().equals("continue") || (!chat.getModule_type().contains("text") && !chat.getModule_type().contains("audio_txt") && !chat.getModule_type().contains("list_sound_txt") && !chat.getModule_type().contains("audio_txt_img") && !chat.getModule_type().contains("audio") && !chat.getModule_type().contains("picture") && !chat.getModule_type().contains("picture") && !chat.getModule_type().contains("conversation"))) {
                                            SharedPreferences prefs = _activity.getSharedPreferences("action_continue", MODE_PRIVATE);
                                            String pos_clicked = prefs.getString("position", "-1");


                                            // then you use
                                            if (chatList_adapter.getItemCount() == Integer.valueOf(pos_clicked)) {

                                                chatList_adapter.addItem(chat, -1);

                                                _recyclerView.smoothScrollToPosition(_chatList_adapter.getItemCount() - 1);

                                                if (!newChat.get(chatList_adapter.getItemCount() - 1).getPart_id().toString()
                                                        .equals(chat.getPart_id().toString())
                                                ) {

                                                    if (chatList_adapter.getItemCount() - 2 < 0) {
                                                    } else {
                                                    }
                                                }

                                            } else {
                                                if (!check_status_continue) {

                                                    chatList_adapter.addItem(chat, -1);

                                                    _recyclerView.smoothScrollToPosition(_chatList_adapter.getItemCount() - 1);

                                                    check_status_continue = true;

                                                    if (chatList_adapter.getlist().size() - 2 < 0) {

                                                    } else {

                                                    }

                                                }
                                            }

                                        } else {
                                            if (!check_status_continue) {

                                                new add_item().execute(chat); // assuming pass is a string
                                            }
                                        }
                                    }


                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                                check_status_continue = false;

                            }
                        };

                        disposable = observable
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribeOn(Schedulers.io())
                                .subscribeWith(observer);
                    } catch (Exception e) {
                        Log.e("TAG", "run: rxcode_load_chat  catch  " + e.getMessage());
                    }

                }
            });

        } catch (Exception e) {
            Log.e("TAG", "rxcode_load_chat Exception: " + e.getMessage());
        }

    }



Read more here: https://stackoverflow.com/questions/68466240/how-smooth-scroll-recyclerview-when-additem

Content Attribution

This content was originally published by Parisa at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: