0

Question: How can get all previous swiped cards in stack again if a user click on previous button like Google primer app?

Explaination:

Suppose, I have 10 cards. 001, 002...010. Now I started swiping. I swiped 001, 002 and 003, so I'm currently seeing 004. now I clicked on the previous button. So it is showing 003, which is correct.

After that there are two use case can happen:

1: Suppose I clicked previous button, so it should show 002, which is not showing.

2: Suppose I clicked next button, so it should show 004 again, which is showing also and it is correct, but now again I clicked previous button, now this time it is not showing 003!

Google primer app sample Image: enter image description here

I tried this library classes in my project because I don't want to add dependency in my project: https://github.com/flschweiger/SwipeStack

Swipe Stack Adapter:

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);
    }
}

I found this method in SwipeStack.java:

private void addNextView() {
    if (mCurrentViewIndex < mAdapter.getCount()) {
        View bottomView = mAdapter.getView(mCurrentViewIndex, null, this);
        bottomView.setTag(R.id.new_view, true);

        if (!mDisableHwAcceleration) {
            bottomView.setLayerType(LAYER_TYPE_HARDWARE, null);
        }

        if (mViewRotation > 0) {
            bottomView.setRotation(mRandom.nextInt(mViewRotation) - (mViewRotation / 2));
        }

        int width = getWidth() - (getPaddingLeft() + getPaddingRight());
        int height = getHeight() - (getPaddingTop() + getPaddingBottom());

        LayoutParams params = bottomView.getLayoutParams();
        if (params == null) {
            params = new LayoutParams(
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT);
        }

        int measureSpecWidth = MeasureSpec.AT_MOST;
        int measureSpecHeight = MeasureSpec.AT_MOST;

        if (params.width == LayoutParams.MATCH_PARENT) {
            measureSpecWidth = MeasureSpec.EXACTLY;
        }

        if (params.height == LayoutParams.MATCH_PARENT) {
            measureSpecHeight = MeasureSpec.EXACTLY;
        }

        bottomView.measure(measureSpecWidth | width, measureSpecHeight | height);
        addViewInLayout(bottomView, 0, params, true);

        mCurrentViewIndex++;
    }
}

EDIT: Thanks Navneet Shamra for your special help. Thanks a lot. I really appreciate your experience and talent.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

//Add a new variable for saving last swiped position

int lastSwipedPosition=0;

// Main list & temp list

private ArrayList<String> mData =  new Arraylist();
private ArrayList<String> mTempData =  new Arraylist();

//Now fill all the data in temp list and use temp in your adaptor`

`mTempData.addAll(mData);
 mAdapter = new SwipeStackAdapter(mTempData);
 mSwipeStack.setAdapter(mAdapter);

// Callback methods

@Override
public void onViewSwipedToRight(int position) {
  lastSwipedPosition=position+1;
}
@Override
public void onViewSwipedToLeft(int position) {
 lastSwipedPosition=position+1;
}

/Clicking on previous button & for going back to previous we will subtract 1 from lastSwipedPosition to show last swiped card.

lastSwipedPosition = lastSwipedPosition - 1;
        if (lastSwipedPosition >= 0) {
            mTempData.clear();//clear all data
            for (int i = lastSwipedPosition; i < mData.size(); i++) {
                mTempData.add(mData.get(i));
            }
            mSwipeStack.resetStack();
            if (lastSwipedPosition == 0){
                Toast.makeText(this, "hide back button", Toast.LENGTH_SHORT).show();
            }
        } else {
            lastSwipedPosition = 0;
        }