How to implement firebase recycler using TextWatcher?

喜欢而已 提交于 2021-01-27 21:37:47

问题


I have implemented Firebase recycler in my app. Everything works fine while using text watcher, but recycler view is not updating in onTextChanged with the keyboard up, and gets updated as soon as i press the keyboard to go down.

This is my code:

private TextWatcher textWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if(s.toString().trim().length()<1)
            {
                mRecyclerView.setVisibility(View.GONE);
            }
            else
            {
                mRecyclerView.setVisibility(View.VISIBLE);
                firebaseSearch(s.toString());
            }
            Toast.makeText(ChooseUser.this,s.toString(),Toast.LENGTH_SHORT).show();
        }
    };

This is firebase recycler:

private void firebaseSearch(String searchText)
    {
        Query firebaseSearchQuery = databaseReference
                .orderByChild("Name")
                .startAt(searchText.toLowerCase())
                .endAt(searchText.toLowerCase() + "\uf8ff");

        FirebaseRecyclerOptions<Users> options =
            new FirebaseRecyclerOptions.Builder<Users>()
                    .setQuery(firebaseSearchQuery, snapshot ->
                            new Users(snapshot.child("Name").getValue().toString(),
                            snapshot.child("Image").getValue().toString(),
                                    snapshot.getKey(),
                                    snapshot.child("Profile").getValue().toString()
                            ))
                    .build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
                options
        ) {
            @Override
            protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {

                    holder.setDetails(getApplicationContext(), model.getName(), model.getImage());

            }

            @NonNull
            @Override
            public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.search_list_items, parent, false);

                return new UsersViewHolder(view);
            }
        };
        firebaseRecyclerAdapter.startListening();
        mRecyclerView.setAdapter(firebaseRecyclerAdapter);
    }

Is there anything to change in my code to update the view dynamically as I enter the words and to show recycler when the keyboard is up?


回答1:


It is just that, I was having mRecyclerView.setHasFixedSize(true); in my code. Removing this made it work.



来源:https://stackoverflow.com/questions/58352582/how-to-implement-firebase-recycler-using-textwatcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!