如何管理刷卡数?

时间:2019-11-22 05:16:38

标签: android

我正在从here.实现刷卡堆栈演示,现在我想跟踪刷卡的进度。我已经制定了自己的逻辑,并且工作正常。但是我还有一个功能,那就是在单击上一个按钮时,用户可以按相反的顺序一张一张地刷卡。

但是在这种情况下,刷卡进度堆栈不起作用。我该怎么办?

说明:不是刷卡总数,而是我现在看到哪一张卡像10张纸中的4张

DisplayCardActivity:

package com.jimmytrivedi.cardswipedemo.Activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.jimmytrivedi.cardswipedemo.Adapter.SwipeStackAdapter;
import com.jimmytrivedi.cardswipedemo.Data.FetchDataFromUriAsyncTask;
import com.jimmytrivedi.cardswipedemo.Listener.JSONDataListener;
import com.jimmytrivedi.cardswipedemo.R;
import com.jimmytrivedi.cardswipedemo.View.SwipeStack;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DisplayCardActivity extends BaseActivity implements SwipeStack.SwipeStackListener, View.OnClickListener, JSONDataListener {
    public static final String TAG = DisplayCardActivity.class.getSimpleName();

    @BindView(R.id.previous)
    ImageButton mPrevious;
    @BindView(R.id.next)
    ImageButton mNext;
    @BindView(R.id.swipe_stack)
    SwipeStack mSwipeStack;
    @BindView(R.id.fab)
    FloatingActionButton mFAB;
    @BindView(R.id.card_progress)
    TextView cardProgress;

    private FetchDataFromUriAsyncTask dataFromUriAsyncTask;
    private ProgressDialog progressDialog;
    private ArrayList<String> mData;
    private ArrayList<String> mTempData;
    private int mCurrentCardPosition = 1, lastSwipedPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_display_card;
    }

    private void init() {
        ButterKnife.bind(this);
        setClickListener();
        mTempData = new ArrayList<>();
        mPrevious.setEnabled(false);
        mPrevious.setColorFilter(getResources().getColor(R.color.colorGray));
        dataFromUriAsyncTask = new FetchDataFromUriAsyncTask(this, this);
        dataFromUriAsyncTask.setJSONDataListener(this);
        progressDialog = new ProgressDialog(this);
        loadDataFromUrl();
    }

    private void setClickListener() {
        mPrevious.setOnClickListener(this);
        mNext.setOnClickListener(this);
        mFAB.setOnClickListener(this);
    }

    private void loadDataFromUrl() {
        //Showing progress dialog
        progressDialog.setMessage("Fetching Data from...");
        progressDialog.setCancelable(false);
        progressDialog.show();
        dataFromUriAsyncTask.execute();
    }

    private void changePreviousButtonStatus(int positionValue) {
        if (lastSwipedPosition == positionValue) {
            mPrevious.setEnabled(false);
            mPrevious.setColorFilter(getResources().getColor(R.color.colorGray));
        } else {
            mPrevious.setEnabled(true);
            mPrevious.setColorFilter(getResources().getColor(R.color.colorBlack));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.previous:
                changePreviousButtonStatus(1);
                lastSwipedPosition = lastSwipedPosition - 1;
                if (lastSwipedPosition >= 0) {
                    mTempData.clear();
                    for (int i = lastSwipedPosition; i < mData.size(); i++) {
                        mTempData.add(mData.get(i));
                    }
                    mSwipeStack.resetStack();
                    setCurrentCardPosition(lastSwipedPosition);
                } else {
                    lastSwipedPosition = 0;
                }

                break;

            case R.id.next:
                changePreviousButtonStatus(1);
                mSwipeStack.swipeTopViewToRight();
                break;

            case R.id.fab:
                mPrevious.setEnabled(false);
                mPrevious.setColorFilter(getResources().getColor(R.color.colorGray));
                lastSwipedPosition = 0;
                mTempData.clear();
                mTempData.addAll(mData);
                mSwipeStack.resetStack();
                if (mData != null)
                    cardProgress.setText(getResources().getString(R.string.default_count) + mData.size());
                break;
        }
    }

    @Override
    public void onViewSwipedToLeft(int position) {
        mCurrentCardPosition = position + 2;
        setCurrentCardPosition(mCurrentCardPosition);
        lastSwipedPosition = position + 1;
        changePreviousButtonStatus(0);
    }

    @Override
    public void onViewSwipedToRight(int position) {
        mCurrentCardPosition = position + 2;
        setCurrentCardPosition(mCurrentCardPosition);
        lastSwipedPosition = position + 1;
        changePreviousButtonStatus(0);
    }

    @Override
    public void onStackEmpty() {
        cardProgress.setText("No card in stack");
    }


    @Override
    public void onClickData(ArrayList<String> getData) {
        // Dismiss the progress dialog
        if (progressDialog.isShowing())
            progressDialog.dismiss();


        mData = new ArrayList<>(getData);
        mTempData.addAll(mData);
        setCurrentCardPosition(mCurrentCardPosition);
        SwipeStackAdapter mAdapter = new SwipeStackAdapter(mTempData);

        mSwipeStack.setAdapter(mAdapter);
        mSwipeStack.setListener(this);
    }

    @Override
    public void onJSONParsingError(String error) {
        if (progressDialog.isShowing())
            progressDialog.dismiss();

        showToast(getString(R.string.user_error), Toast.LENGTH_LONG);
    }

    private void showToast(String message, int length) {
        Toast.makeText(this, message, length).show();
    }

    private void showLog(String msg) {
        Log.d(TAG, msg);
    }

    private void setCurrentCardPosition(int currentPosition) {
        cardProgress.setText(currentPosition + " Out of " + mData.size());
    }


}

适配器:

package com.jimmytrivedi.cardswipedemo.Adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jimmytrivedi.cardswipedemo.R;

import java.util.List;

public class SwipeStackAdapter extends BaseAdapter {

    public static final String TAG = SwipeStackAdapter.class.getSimpleName();

    private List<String> mData;

    public SwipeStackAdapter(List<String> data) {
        this.mData = data;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false);
        TextView textViewCard = convertView.findViewById(R.id.textViewCard);
        textViewCard.setText(mData.get(position));

        return convertView;
    }

    private void showLog(String msg) {
        Log.d(TAG, msg);
    }
}

问题:

用例:我有10张卡(001、002..010)。总共有5件事。 1.向左滑动 2.右色 3.上一个按钮 4.下一个按钮 5.重置按钮

如果我开始向右轻扫001,然后再按002,则我按了003和004的下一个按钮。时间顺序没有维持。

2 个答案:

答案 0 :(得分:0)

保留一个全局变量,并在SwipeLeftSwipeRight回调中将其递增

private int totalCardsSwiped = 0;

@Override
public void onViewSwipedToLeft(int position) {
totalCardsSwiped++; }

@Override
public void onViewSwipedToRight(int position) {
totalCardsSwiped++; }

答案 1 :(得分:0)

为什么要在滑动回调方法中执行此操作?

mCurrentCardPosition =位置+ 2;

我认为这是问题所在。

或者您可以使用当前位置调用此 setCurrentCardPosition()方法

@Override
public void onViewSwipedToLeft(int position) {
    mCurrentCardPosition = position + 2;
    setCurrentCardPosition(position);
    lastSwipedPosition = position + 1;
    changePreviousButtonStatus(0);
}

@Override
public void onViewSwipedToRight(int position) {
    mCurrentCardPosition = position + 2;
    setCurrentCardPosition(position);
    lastSwipedPosition = position + 1;
    changePreviousButtonStatus(0);
}
  

已编辑

int currentPosition = 1;

在列表中添加数据后

txtCardProgress.setText("1 out of " + mData.size());

在onClick

@Override
public void onClick(View v) {
    if (v.equals(mButtonLeft)) {
        swipedLastPosition = swipedLastPosition - 1;
        currentPosition =currentPosition -1;
        if (swipedLastPosition >= 0) {
            mTempData.clear();//clear all data
            for (int i = swipedLastPosition; i < mData.size(); i++) {
                mTempData.add(mData.get(i));
            }
            mSwipeStack.resetStack();
            if (swipedLastPosition == 0) {
                Toast.makeText(this, "hide back button", Toast.LENGTH_SHORT).show();
            }
            currentPosition =currentPosition -1;
            setCardProgress(currentPosition);
        } else {
            swipedLastPosition = 0;
            currentPosition = 0;
            setCardProgress(currentPosition);
        }

    } else if (v.equals(mButtonRight)) {
        mSwipeStack.swipeTopViewToRight();
    } else if (v.equals(mFab)) {
        swipedLastPosition = 0;
        mTempData.clear();
        mTempData.addAll(mData);
        mSwipeStack.resetStack();
        setCardProgress(swipedLastPosition);
    }

回调方法

@Override
public void onViewSwipedToRight(int position) {
    swipedLastPosition = position + 1;
    setCardProgress(position);
}

@Override
public void onViewSwipedToLeft(int position) {
    swipedLastPosition = position;     
    setCardProgress(position);
}

更新进度方法

void setCardProgress(int position) {
    if (position == mData.size()-1) {
        currentPosition = mData.size();
        txtCardProgress.setText(currentPosition + " out of " + mData.size());
    } else {
        currentPosition++;
        txtCardProgress.setText(currentPosition + " out of " + mData.size());
    }
}