CardStackListener 不提供任何回调
CardStackListener does not provide any callbacks
我正在使用 yuyakaido 的库 CardStackView
,我似乎无法使 CardStackListener
工作。这是图书馆的 link:
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
作为我的适配器
您在 PendingFragment
中实现了 CardStackListener
,但我没有看到您在哪里设置此侦听器,您忘记了如下内容:
cardStackView.setCardStackListener(this)
更新
看了这个库的源代码后,我找到了解决办法,你使用了错误的构造函数,请按照下面的代码
manager = new CardStackLayoutManager(getActivity(), this);
您正在使用此构造函数。这只是询问上下文。
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) {
>
> } }
我正在使用 yuyakaido 的库 CardStackView
,我似乎无法使 CardStackListener
工作。这是图书馆的 link:
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
作为我的适配器
您在 PendingFragment
中实现了 CardStackListener
,但我没有看到您在哪里设置此侦听器,您忘记了如下内容:
cardStackView.setCardStackListener(this)
更新
看了这个库的源代码后,我找到了解决办法,你使用了错误的构造函数,请按照下面的代码
manager = new CardStackLayoutManager(getActivity(), this);
您正在使用此构造函数。这只是询问上下文。
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) {
>
> } }