yuyakaido / CardStackView

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

Rewinding Not Working #342

Closed saberstop closed 3 years ago

saberstop commented 3 years ago

I am on version 2.3.4 and here is my code. I'm trying to add in the rewind at the bottom. I know that the line of code is executed, but nothing happens and no rewind occurs.

package com.example.swiping;

import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.DefaultItemAnimator; import androidx.recyclerview.widget.DiffUtil;

import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; import android.view.animation.LinearInterpolator; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;

import com.yuyakaido.android.cardstackview.CardStackLayoutManager; import com.yuyakaido.android.cardstackview.CardStackListener; import com.yuyakaido.android.cardstackview.CardStackView; import com.yuyakaido.android.cardstackview.Direction; import com.yuyakaido.android.cardstackview.Duration; import com.yuyakaido.android.cardstackview.RewindAnimationSetting; import com.yuyakaido.android.cardstackview.StackFrom; import com.yuyakaido.android.cardstackview.SwipeableMethod;

import java.util.ArrayList; import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = "MainActivity";
private CardStackLayoutManager manager;
private CardStackAdapter adapter;
private CardStackView cardStackView;
private int superlikes = 1;
private int bans = 1;
private static ImageView imageView;

@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();
                superlikes--;
                TextView superlikesText = (TextView) findViewById(R.id.textView2);
                superlikesText.setText(superlikes + " left");
            }
            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();
                bans--;
                TextView bansText = (TextView) findViewById(R.id.textView3);
                bansText.setText(bans + " left");
            }

            // 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());

    TextView superlikesText = (TextView) findViewById(R.id.textView2);
    superlikesText.setText(superlikes + " left");
    TextView bansText = (TextView) findViewById(R.id.textView3);
    bansText.setText(bans + " left");

    ImageView imageView5 = (ImageView)findViewById(R.id.imageView5);
    imageView5.setOnClickListener(this);
}

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.fiveguys, "Five Guys", "1 mi, $"));
    items.add(new ItemModel(R.drawable.mcdonalds, "McDonalds", "3.7 mi, $"));
    items.add(new ItemModel(R.drawable.pizzahut, "Pizza Hut", "2 mi, $"));
    items.add(new ItemModel(R.drawable.subway, "Subway", "3.9 mi, $"));
    items.add(new ItemModel(R.drawable.tacobell, "Taco Bell", "6.7 mi, $"));

    items.add(new ItemModel(R.drawable.fiveguys, "Five Guys", "1 mi, $"));
    items.add(new ItemModel(R.drawable.mcdonalds, "McDonalds", "3.7 mi, $"));
    items.add(new ItemModel(R.drawable.pizzahut, "Pizza Hut", "2 mi, $"));
    items.add(new ItemModel(R.drawable.subway, "Subway", "3.9 mi, $"));
    items.add(new ItemModel(R.drawable.tacobell, "Taco Bell", "6.7 mi, $"));
    return items;
}

public void onClick(View v) {
    if (v.getId() == R.id.imageView5) {
        cardStackView.rewind();
    }
}

}

saberstop commented 3 years ago

I figured out my problem, the manager has to be both manual and automatic. I had it only as manual.

manager.setSwipeableMethod(SwipeableMethod.AutomaticAndManual);