yuyakaido / CardStackView

📱Tinder like swipeable card view for Android
Apache License 2.0
2.36k stars 448 forks source link

cardStackView.rewind() not working #326

Closed LassassinX closed 3 years ago

LassassinX commented 4 years ago

Library version: 2.3.4.

I try calling the cardStackView.rewind(), but it doesnt work. I call this method in rewindbtnPressed(). Please help

my code:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private CardStackLayoutManager manager;
private CardStackAdapter adapter;
CardStackView cardStackView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     cardStackView = findViewById(R.id.card_stack_view);
    manager = new CardStackLayoutManager(this, new CardStackListener() {
        @Override
        public void onCardDragging(Direction direction, float ratio) {
            Log.d(TAG, "onCardDragging: d=" + direction.name() + " ratio=" + ratio);
        }

        @Override
        public void onCardSwiped(Direction direction) {
            Log.d(TAG, "onCardSwiped: p=" + manager.getTopPosition() + " d=" + direction);
            if (direction == Direction.Right){
                Toast.makeText(MainActivity.this, "Direction Right", Toast.LENGTH_SHORT).show();
            }
            if (direction == Direction.Top){
                Toast.makeText(MainActivity.this, "Direction Top", Toast.LENGTH_SHORT).show();
            }
            if (direction == Direction.Left){
                Toast.makeText(MainActivity.this, "Direction Left", Toast.LENGTH_SHORT).show();
            }
            if (direction == Direction.Bottom){
                Toast.makeText(MainActivity.this, "Direction Bottom", Toast.LENGTH_SHORT).show();
            }

            // Paginating
            if (manager.getTopPosition() == adapter.getItemCount() - 5){
                paginate();
            }

        }

        @Override
        public void onCardRewound() {
            Log.d(TAG, "onCardRewound: " + manager.getTopPosition());
        }

        @Override
        public void onCardCanceled() {
            Log.d(TAG, "onCardRewound: " + manager.getTopPosition());
        }

        @Override
        public void onCardAppeared(View view, int position) {
            TextView tv = view.findViewById(R.id.item_name);
            Log.d(TAG, "onCardAppeared: " + position + ", nama: " + tv.getText());
        }

        @Override
        public void onCardDisappeared(View view, int position) {
            TextView tv = view.findViewById(R.id.item_name);
            Log.d(TAG, "onCardAppeared: " + position + ", nama: " + tv.getText());
        }
    });
    manager.setStackFrom(StackFrom.None);
    manager.setVisibleCount(3);
    manager.setTranslationInterval(8.0f);
    manager.setScaleInterval(0.95f);
    manager.setSwipeThreshold(0.3f);
    manager.setMaxDegree(20.0f);
    manager.setDirections(Direction.FREEDOM);
    manager.setCanScrollHorizontal(true);
    manager.setSwipeableMethod(SwipeableMethod.Manual);
    manager.setOverlayInterpolator(new LinearInterpolator());
    adapter = new CardStackAdapter(addList());
    cardStackView.setLayoutManager(manager);
    cardStackView.setAdapter(adapter);
    cardStackView.setItemAnimator(new DefaultItemAnimator());

}

private void paginate() {
    List<ItemModel> old = adapter.getItems();
    List<ItemModel> baru = new ArrayList<>(addList());
    CardStackCallback callback = new CardStackCallback(old, baru);
    DiffUtil.DiffResult hasil = DiffUtil.calculateDiff(callback);
    adapter.setItems(baru);
    hasil.dispatchUpdatesTo(adapter);
}

private List<ItemModel> addList() {
    List<ItemModel> items = new ArrayList<>();
    items.add(new ItemModel(R.drawable.sample1, "Markonah", "24", "Jember"));
    items.add(new ItemModel(R.drawable.sample2, "Marpuah", "20", "Malang"));
    items.add(new ItemModel(R.drawable.sample3, "Sukijah", "27", "Jonggol"));
    items.add(new ItemModel(R.drawable.sample4, "Markobar", "19", "Bandung"));
    items.add(new ItemModel(R.drawable.sample5, "Marmut", "25", "Hutan"));

    items.add(new ItemModel(R.drawable.sample1, "Markonah", "24", "Jember"));
    items.add(new ItemModel(R.drawable.sample2, "Marpuah", "20", "Malang"));
    items.add(new ItemModel(R.drawable.sample3, "Sukijah", "27", "Jonggol"));
    items.add(new ItemModel(R.drawable.sample4, "Markobar", "19", "Bandung"));
    items.add(new ItemModel(R.drawable.sample5, "Marmut", "25", "Hutan"));
    return items;
}

public void rewindBtnPressed(View view) {
    RewindAnimationSetting setting = new RewindAnimationSetting.Builder().setDirection(Direction.Right)
            .setDuration(3).setInterpolator(new AccelerateInterpolator()).build();
    manager.setRewindAnimationSetting(setting);
    cardStackView.rewind();
}

}

ivantsaguilar commented 3 years ago

I found the solution!

You need to implement another CardStackLayoutManager!!

`FloatingActionButton rewindButton = (FloatingActionButton)rootView.findViewById(R.id.rewind_button);

rewindButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "click", Toast.LENGTH_SHORT).show(); RewindAnimationSetting settings = new RewindAnimationSetting.Builder() .setDirection(Direction.Bottom) .setDuration(Duration.Normal.duration) .setInterpolator( new DecelerateInterpolator()) .build();

            CardStackLayoutManager cardStackLayoutManager2 = new CardStackLayoutManager(mContext);
            cardStackLayoutManager2.setRewindAnimationSetting(settings);
            cardStackView.setLayoutManager(cardStackLayoutManager2);
            cardStackView.smoothScrollToPosition(0);
        }
    });`
LassassinX commented 3 years ago

Thanks

saberstop commented 3 years ago

What does mContext get replaced with? I'm new to android studio and I'm having the same issue with rewind not working.

ivantsaguilar commented 3 years ago

With getApplicationContext() or this or getBaseContext()

saberstop commented 3 years ago

Thank you for helping me out. When I add in the code you put above and click on the rewind button, it rewinds totally to the first item instead of the previous item. Do you know why this is happening? I've also tried replacing the smoothScrollToPosition(0) with rewind() but it does the same thing.

andrea-liu87 commented 2 years ago

because it is smoothScrollToPosition(0), Card at position 0 is the first card The code should be smoothScrollToPosition (getTopPosition() - 1)

TonyLead commented 1 month ago

it is not working