滑动删除recyclerView后的getItemCount

时间:2019-05-19 19:44:03

标签: java android

我正在开发用于库存的Android扫描仪QR代码。

当用户成功扫描QR码时, 该应用程序从后端应用程序获取信息, 该应用会将结果添加到recyclerView中。

在我的应用中,我计算所有这些结果

这是代码;

RecyclerView recyclerView;
ItemTouchHelper itemTouchHelper;

@Override
protected void onCreate(Bundle state) {
    super.onCreate(state);

    // The label information
    textViewCountDataResultScan = findViewById(R.id.labelCountDataResult);


    setContentView(R.layout.activity_antam_incoming_scan_qr);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    mScannerView = new ZXingScannerView(this);

    FrameLayout rl = findViewById(R.id.relative_scan_take_single);
    rl.addView(mScannerView);

    int currentApiVersion = Build.VERSION.SDK_INT;
    if (currentApiVersion >= Build.VERSION_CODES.LOLLIPOP) {
        if (!checkPermission()) {
            requestPermission();
        }
    }

    setUpRecyclerView();
}

private void setUpRecyclerView() {
    recyclerView = (RecyclerView) findViewById(R.id.recyclerViewAntamIncomingScanQR);
    bagLotNumberAdapter = new BagLotNumberAdapter(this, bagLotNumbers);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setAdapter(bagLotNumberAdapter);
    recyclerView.setHasFixedSize(true);

    // swipe delete
    itemTouchHelper = new
            ItemTouchHelper(new SwipeToDeleteBagLotNumberCallback(bagLotNumberAdapter));
    itemTouchHelper.attachToRecyclerView(recyclerView);

}

这是处理QR码扫描结果的方法,

@Override
public void handleResult(Result result) {
    final String scanResult = result.getText();
    String[] keysExplode = scanResult.split("\\s*,\\s*");

    // Adding To Adapter
    preparedObjectDataBagLotNumber(
            Integer.parseInt(keysExplode[0]),
            Integer.parseInt(keysExplode[2]),
            keysExplode[3],
            Integer.parseInt(keysExplode[4]),
            Integer.parseInt(keysExplode[5]),
            keysExplode[1]
    );

    //Count Result Scan
    countDataResultScan();
}

public void countDataResultScan() {
    textViewCountDataResultScan = (TextView) findViewById(R.id.labelCountDataResult);

    // count Adapter
    int countArrayListResultScan = recyclerView.getAdapter().getItemCount();
    textViewCountDataResultScan.setText(String.valueOf(countArrayListResultScan));
}

我的问题是,滑动删除后如何计算适配器中的项目?

public class SwipeToDeleteBagLotNumberCallback extends ItemTouchHelper.SimpleCallback {

    private BagLotNumberAdapter mAdapter;
    private Drawable icon;
    private final ColorDrawable background;


    public SwipeToDeleteBagLotNumberCallback(BagLotNumberAdapter bagLotNumberAdapter) {
        super(0,ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
        mAdapter = bagLotNumberAdapter;
        icon = ContextCompat.getDrawable(mAdapter.getContext(),
                R.drawable.ic_delete_white_36);
        background = new ColorDrawable(Color.RED);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int position = viewHolder.getAdapterPosition();
        mAdapter.deleteItem(position);
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX,
                dY, actionState, isCurrentlyActive);
        View itemView = viewHolder.itemView;
        int backgroundCornerOffset = 20;

        int iconMargin = (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
        int iconTop = itemView.getTop() + (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
        int iconBottom = iconTop + icon.getIntrinsicHeight();

        if (dX > 0) { // Swiping to the right
            int iconLeft = itemView.getLeft() + iconMargin;
            int iconRight = itemView.getLeft() + iconMargin + icon.getIntrinsicWidth();
            icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

            background.setBounds(itemView.getLeft(), itemView.getTop(),
                    itemView.getLeft() + ((int) dX) + backgroundCornerOffset,
                    itemView.getBottom());
        } else if (dX < 0) { // Swiping to the left
            int iconLeft = itemView.getRight() - iconMargin - icon.getIntrinsicWidth();
            int iconRight = itemView.getRight() - iconMargin;
            icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

            background.setBounds(itemView.getRight() + ((int) dX) - backgroundCornerOffset,
                    itemView.getTop(), itemView.getRight(), itemView.getBottom());
        } else { // view is unSwiped
            background.setBounds(0, 0, 0, 0);
        }

        background.draw(c);
        icon.draw(c);
    }
}

0 个答案:

没有答案