简体   繁体   English

Java:如何为每个RecyclerView项目创建自己的总数计数器?

[英]Java: How to make own total number counter for every RecyclerView item?

I need to make a total number counter for every RecyclerView item individually. 我需要为每个RecyclerView项目单独制作一个总数计数器。 Here are a picture and code to clear this situation out: 这是一张图片和代码,以清除这种情况:

在此输入图像描述

TTL is that total counter that I need. TTL是我需要的总计数器。 When I click player 1 plus symbol, total counter in that particular item should be increased by 1 and minus symbol decrease by 1. 当我点击播放器1加上符号时,该特定项目中的总计数器应增加1,减去符号减少1。

What I have at the moment: 我现在所拥有的:

ViewHolder (handle minus and plus symbols): ViewHolder(句柄减号和加号):

public static class GameViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextPlayer, mTextPar, mTotalTxt, mNumberTotal;
    public ImageView mImageMinus, mImagePlus;

    public GameViewHolder(@NonNull View itemView, final GameAdapter.OnItemClickListener listener) {
        super(itemView);
        mTextPlayer = itemView.findViewById(R.id.gameNameRecycler);
        mTextPar = itemView.findViewById(R.id.gameParNumberRecycler);
        mImageMinus = itemView.findViewById(R.id.game_minus_btn);
        mImagePlus = itemView.findViewById(R.id.game_plus_btn);
        mTotalTxt = itemView.findViewById(R.id.game_total_txt);
        mNumberTotal = itemView.findViewById(R.id.game_total_number);

        mImageMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    int position = getAdapterPosition();

                    if (position != RecyclerView.NO_POSITION) {
                        listener.onMinusClick(position);
                    }
                }
            }
        });

        mImagePlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    int position = getAdapterPosition();

                    if (position != RecyclerView.NO_POSITION) {
                        listener.onPlusClick(position);
                    }
                }
            }
        });
    }
}

In Activity: 在活动中:

mAdapter.setOnItemClickListener(new GameAdapter.OnItemClickListener() {
            int totalCounter = 0;

            @Override
            public void onMinusClick(int position) {
                String parNum = mGameItemList.get(position).getText2();
                int intParNm = Integer.valueOf(parNum);

                if (intParNm != 1) {
                    intParNm -= 1;
                    totalCounter -= 1;

                    mGameItemList.get(position).changeText2(Integer.toString(intParNm));
                    mGameItemList.get(position).changeText4(Integer.toString(totalCounter));
                    mAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onPlusClick(int position) {
                String parNum = mGameItemList.get(position).getText2();
                int intParNm = Integer.valueOf(parNum);

                if (intParNm != 99) {
                    intParNm += 1;
                    totalCounter += 1;

                    mGameItemList.get(position).changeText2(Integer.toString(intParNm));
                    mGameItemList.get(position).changeText4(Integer.toString(totalCounter));
                    mAdapter.notifyDataSetChanged();
                }
            }
        });

So this is what I have now, but atm every item uses that same itemCounter, so if I click player 1 plus button, its TTL is 1 (as it should) but after that when I click player 2 plus button, player 2 TTL should be also 1 but is in fact 2, cause that 1 extra came from that player 1 plus click. 所以这就是我现在所拥有的,但是atm每个项目都使用相同的itemCounter,所以如果我点击播放器1加按钮,它的TTL是1(应该如此)但是之后当我点击播放器2加按钮时,播放器2 TTL应该也是1但实际上是2,因为1个额外来自该玩家1加点击。

How I can change this to work for both individually? 我怎样才能改变它以便单独工作?

On thing is you can call notifyItemChanged(position) , on the position of the row that has changed, it will only update that item in the recycler view. 事情是你可以调用notifyItemChanged(position) ,在已经改变的行的位置上,它只会更新recycleler视图中的那个项目。

Instead of holding your count in totalCounter, you could get the value from the viewholder that is already assigned, so something like String totalCounter = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.view)).getText().toString(); 您可以从已经分配的视图中获取值,而不是将您的计数保存在totalCounter中,因此类似于String totalCounter = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.view)).getText().toString();

@Override
     public void onMinusClick(int position) {
            String parNum = mGameItemList.get(position).getText2();
            int intParNm = Integer.valueOf(parNum);

            if (intParNm != 1) {
                intParNm -= 1;
                totalCounter -= 1;
               mGameItemList.get(position).changeText2(Integer.toString(intParNm));
               mGameItemList.get(position).changeText4(Integer.toString(totalCounter));
               adapter.notifyItemChanged(position);
                }

            }

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

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