如何避免FirebaseRecyclerAdapter的刷新效果

时间:2018-01-27 06:28:15

标签: android firebase firebase-realtime-database recycler-adapter firebaseui

在输入以自动填充方式加载数据的文本字段时,刷新对FirebaseRecyclerAdapter的效果。我曾经在onTextChanged方法的帮助下调用以下函数firebaseUserSearch来填充适配器。每次调用private void firebaseUserSearch(String searchText) { Query firebaseSearchQuery = mUserDatabase.orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff"); FirebaseRecyclerAdapter<Users, SearchActivity.UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, SearchActivity.UsersViewHolder>( Users.class, R.layout.list_layout, SearchActivity.UsersViewHolder.class, firebaseSearchQuery ) { @Override protected void populateViewHolder(SearchActivity.UsersViewHolder viewHolder, final Users model, final int position) { viewHolder.setDetails(getActivity().getApplicationContext(), model.getName(), model.getPlace(), model.getImage()); viewHolder.relLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), ResultActivity.class); intent.putExtra("name", model.getName()); intent.putExtra("place", model.getPlace()); intent.putExtra("image", model.getImage()); startActivity(intent); } }); } }; mResultList.setAdapter(firebaseRecyclerAdapter); } 时,如果结果已经存在,则刷新我不想要的recylerview。

os.getcwd()

2 个答案:

答案 0 :(得分:1)

每次更改搜索词时,都会创建一个新查询和一个新适配器,从而导致数据的完全重新加载(和重新绘制)。标准length无法阻止这种情况发生。

如果您需要更智能的适配器,则必须自己创建。您必须将从查询中获得的每个项目与您已有的项目进行比较,并仅将实际更改传递给您的适配器。

虽然这绝对是可能的,但这是相当有用的。如果您遇到困难,请随意发布reproduces where you are stuck

的问题

答案 1 :(得分:0)

onTextChanged会在文本字段中输入内容。

您可能想要使用afterTextChanged

这是一个例子 -

editText.addTextChangedListener(
    new TextWatcher() {
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        private Timer timer=new Timer();
        private final long DELAY = 500; // milliseconds

        @Override
        public void afterTextChanged(final Editable s) {
            timer.cancel();
            timer = new Timer();
            timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        // TODO: do what you need here (refresh list)
                        // you will probably need to use runOnUiThread(Runnable action) for some specific actions
                    }
                }, 
                DELAY
            );
        }
    }
);