当recyclerview的itemview进入屏幕区域时,如何更新Firebase中的子值?

时间:2019-06-07 10:36:27

标签: java android firebase

我想在用户到达recyclerview的每个项目视图时更新子值

假设我在firebase数据库中有“用户”节点。并有三个孩子,分别是“ Title”,“ Description”和“ count”,

{
Users:
 "UID":
    "Title":"..."
    "Description":"..."
    "count":"..."
{

为了获得这个孩子的价值,我实现了代码

@Override
            protected void onBindViewHolder(ViewHolder holder, final int position, Model model) {
                holder.setTxtTitle(model.getmTitle());
                holder.setTxtDesc(model.getmDesc());

                holder.root.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
                    }
                });
            }

现在,我只想在屏幕内出现itemview时更新“计数”值。 假设我在“用户”节点中有5个UID节点,因此我将获得5个itemview。 当用户加载所有这5个uid时

如果用户当前看到1个项目视图,而不是仅更新其子“计数”值。 如果用户当前看到2个项目视图,而不是仅更新其子“计数”值。

简而言之,“计数”值仅在滚动时显示项目视图时更改

1 个答案:

答案 0 :(得分:0)

我试图代表逻辑,但是我没有机会运行它。我试图在notepad ++编辑器上写代码,所以不确定在AndroidStudio上是否会显示语法错误,但是您的逻辑与此类似。

private RecyclerView list;

private adapter recyclerAdapter;

private LinearLayoutManager layoutManager;

private int visibleItemCount, totalItemCount, firstVisibleItemPosition, lastVisibleItem;
private int oldFirstVisibleItemPos = -1, oldLastVisibleItemPos = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler_view5);
    recView = (RecyclerView) findViewById(R.id.list);

    layoutManager = new LinearLayoutManager(this);
    recView.setLayoutManager(layoutManager);
    recyclerAdapter = new adapter(RecyclerViewActivity5.this, country);
    recView.setAdapter(recyclerAdapter);
    recView.addOnScrollListener(onScrollListener);

    firstVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
    lastVisibleItem = layoutManager.findLastCompletelyVisibleItemPosition();
    updateVisibleItem();
}

private RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }
    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        visibleItemCount = layoutManager.getChildCount();
        totalItemCount = layoutManager.getItemCount();
        firstVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
        lastVisibleItem = firstVisibleItemPosition +visibleItemCount;
        Toast.makeText(RecyclerViewActivity5.this, "Visible Item Total:"+String.valueOf(visibleItemCount), Toast.LENGTH_SHORT).show();

        updateVisibleItem();
    }
};

private void updateVisibleItem() {
    if(oldFirstVisibleItemPos == -1 && lastVisibleItem == -1) {
        oldFirstVisibleItemPos = firstVisibleItemPosition;
        oldLastVisibleItemPos = lastVisibleItem;
    } else if(firstVisibleItemPosition < oldFirstVisibleItemPos){

        if(lastVisibleItem < oldFirstVisibleItemPos)
            oldLastVisibleItemPos = lastVisibleItem;
        else
            oldLastVisibleItemPos = oldFirstVisibleItemPos -1;

        oldFirstVisibleItemPos = firstVisibleItemPosition;
    } else {
        if(firstVisibleItemPosition > oldLastVisibleItemPos)
            oldFirstVisibleItemPos = firstVisibleItemPosition;
        else
            oldFirstVisibleItemPos = oldLastVisibleItemPos + 1;
        oldLastVisibleItemPos = lastVisibleItem;
    }

    for(int i = oldFirstVisibleItemPos; i <= oldLastVisibleItemPos; i++) {
        Model model = list.get(i);
        model.setCount(model.getCount() + 1);
    }
}

在这里,列表是您的“模型”列表视图,每个模型都将被视为一个变量。方法updateVisibleItem()用于通过消除重叠的屏幕可见项目来更新项目明智的计数。

我希望这将帮助您完成要求。