CardStackListener不提供任何回调

时间:2018-12-21 03:09:34

标签: android firebase swipe

我正在使用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 个答案:

答案 0 :(得分:1)

您在CardStackListener中实现了PendingFragment,但是我看不到在何处设置此侦听器,您忘记了以下内容:

cardStackView.setCardStackListener(this)

更新

在查看了这个lib的源代码之后,我找到了解决方案,您使用了错误的构造函数,请遵循以下代码

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

答案 1 :(得分:0)

您正在使用此构造函数。只是要求上下文。

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