CardStackListener没有提供任何回调函数。

3
我正在使用yuyakaido的库CardStackView,但似乎无法使CardStackListener正常工作。这是该库的链接:https://github.com/yuyakaido/CardStackView#callbacks。我已将其实现到我的Fragment中。
public class PendingFragment extends Fragment implements CardStackListener{

private CardStackLayoutManager manager;
private CardStackView cardStackView;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View mView = inflater.inflate(R.layout.fragment_pending, container, false);

    manager = new CardStackLayoutManager(getActivity());
    manager.setStackFrom(StackFrom.Top);
    manager.setVisibleCount(3);
    manager.setCanScrollVertical(false);

    cardStackView = mView.findViewById(R.id.pendingList_cardStackView);
    cardStackView.setLayoutManager(manager);

}

@Override
public void onCardDragging(Direction direction, float ratio) {

}

@Override
public void onCardSwiped(Direction direction) {
    Log.e("PendingFragment", "onCardSwiped: " + direction );
    if(direction == Direction.Right){
        Toast.makeText(getActivity(), "Accepted", Toast.LENGTH_SHORT).show();
    }else if(direction == Direction.Left){
        Toast.makeText(getActivity(), "Rejected", Toast.LENGTH_SHORT).show();

    }
}

@Override
public void onCardRewound() {

}

@Override
public void onCardCanceled() {

}

@Override
public void onCardAppeared(View view, int position) {

}

@Override
public void onCardDisappeared(View view, int position) {

}

}

滑动效果正常,但没有回调函数。我是否漏掉了什么?
编辑: 我正在使用gradle: implementation "com.yuyakaido.android:card-stack-view:2.2.0"。 我还在使用FirebaseRecyclerAdapter作为我的适配器。
2个回答

8
您在PendingFragment中实现了CardStackListener,但我没有看到您设置此侦听器,您可能忘记了添加以下内容:
cardStackView.setCardStackListener(this)

更新

查看了该库的源代码后,我发现解决方案,你使用了错误的构造函数,请按照以下代码进行操作。

manager = new CardStackLayoutManager(getActivity(), this);

没有名为.setListener()的函数。 - Gab Ledesma
1
也没有 .setCardStackListener()。 - Gab Ledesma
2
@GabLedesma,你是对的,但在查看源代码后,我找到了解决方案并更新了我的答案。 - GianhTran
@GabLedesma 很高兴能够帮助。 - GianhTran
manager = new CardStackLayoutManager(this, this); // 如果 getActivity() 对你没有用处 - Evans Kakpovi
UPDATE - ekashking

2
您正在使用此构造函数,该构造函数仅要求上下文。
最初的回答:您正在使用只需要上下文的构造函数。
 manager = new CardStackLayoutManager(getActivity());

你应该使用这个。这对你有用。最初的回答:

你应该使用这个。这将对你起作用。

manager = new CardStackLayoutManager(getActivity(), this);

"最初的回答"将会是您完整的代码如下所示。
   > public class UsersCardView extends Fragment implements
    > CardStackListener {
    >     List<Spot> spotList;
    > 
    >     public UsersCardView() {
    >         // Required empty public constructor
    >     }
    > 
    > 
    >     @Override
    >     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    >                              Bundle savedInstanceState) {
    >         // Inflate the layout for this fragment
    >         View view =inflater.inflate(R.layout.fragment_users_card_view, container, false);
    > 
    >         //to set gradient on status bar
    >         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    >             try{
    >             Window window = getActivity().getWindow();
    >                 Drawable background = getResources().getDrawable(R.drawable.bg_gradient);
    >                 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    >                 window.setStatusBarColor(getResources().getColor(android.R.color.transparent));
    >                 //   window.setNavigationBarColor(getResources().getColor(android.R.color.transparent));
    >                 window.setBackgroundDrawable(background);
    > 
    >             }
    >             catch(Exception e){
    >                 Log.d("TAG", "UserCardView "+e);
    >             }
    >         }
    > 
    >         CardStackLayoutManager manager = new CardStackLayoutManager(getContext(),this);
    >         CardStackAdapter adapter = new CardStackAdapter(createSpot(),getContext());
    >         CardStackView cardStackView = view.findViewById(R.id.card_stack_view);
    >         cardStackView.setLayoutManager(manager);
    >         cardStackView.setAdapter(adapter);
    >         manager.setStackFrom(StackFrom.Bottom);
    > 
    >         manager.setVisibleCount(3);
    >         manager.setTranslationInterval(8f);
    >         manager.setDirections(Direction.FREEDOM);
    >         return view;
    >     }
    > 
    >     public List<Spot> createSpot()
    >     {
    >         spotList = new ArrayList<>();
    >         spotList .add(new Spot("Yasaka Shrine", "Kyoto", "https://source.unsplash.com/Xq1ntWruZQI/600x800"));
    >         spotList .add(new Spot("Fushimi Inari Shrine", "Kyoto",  "https://source.unsplash.com/NYyCqdBOKwc/600x800"));
    >         spotList .add(new Spot("Bamboo Forest",  "Kyoto",  "https://source.unsplash.com/buF62ewDLcQ/600x800"));
    >         spotList .add(new Spot("Brooklyn Bridge", "New York", "https://source.unsplash.com/THozNzxEP3g/600x800"));
    >         spotList .add(new Spot("Empire State Building","New York", "https://source.unsplash.com/USrZRcRS2Lw/600x800"));
    >         spotList .add(new Spot("The statue of Liberty","New York", "https://source.unsplash.com/PeFk7fzxTdk/600x800"));
    >         spotList .add(new Spot("Louvre Museum", "Paris","https://source.unsplash.com/LrMWHKqilUw/600x800"));
    >         spotList .add(new Spot("Eiffel Tower", "Paris", "https://source.unsplash.com/HN-5Z6AmxrM/600x800"));
    >         spotList .add(new Spot("Big Ben", "London","https://source.unsplash.com/CdVAUADdqEc/600x800"));
    >         spotList .add(new Spot("Great Wall of China", "China", "https://source.unsplash.com/AWh9C-QjhE4/600x800"));
    >         return spotList;
    >     }
    > 
    >     @Override
    >     public void onCardDragging(Direction direction, float v) {
    >         Toast.makeText(this.getContext()," dragging"+direction,Toast.LENGTH_LONG).show();
    >     }
    > 
    >     @Override
    >     public void onCardSwiped(Direction direction) {
    >         Toast.makeText(this.getContext()," Direction "+direction,Toast.LENGTH_LONG).show();
    >     }
    > 
    >     @Override
    >     public void onCardRewound() {
    > 
    >     }
    > 
    >     @Override
    >     public void onCardCanceled() {
    > 
    >     }
    > 
    >     @Override
    >     public void onCardAppeared(View view, int i) {
    > 
    >     }
    > 
    >     @Override
    >     public void onCardDisappeared(View view, int i) {
    > 
    >     } }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接