When I am in last Item In CardStackView?

Viewed 1858

I have RecyclerView(CardStackView) With 60 items.

Now After finish All Items .. I have Blank Page .. I do not want to Show The blank Page if all items finished ... I want to Intent to Another Activity You can see The blank Page Here

when I implement CardStackListener I Added Those Method too Like This .I want to When All card Answered Intent To ResultActivity...Like This After 60th question I want InstedOf Blank Page Intent To ResultActivity


 //Card Method
  @Override
  public void onCardDragging(Direction direction, float ratio) {
    Log.d("CardStackView", "onCardDragging: d = " + direction.name() + ", r = " + ratio);
  }

  @Override
  public void onCardSwiped(Direction direction) {
    Log.d("CardStackView", "onCardSwiped: p = " + manager.getTopPosition() + ", d = " + direction);
    if (manager.getTopPosition() == mbtiQuestAdapter.getItemCount() - 5) {
      mbtiQuestAdapter.addQuestion(questions);
      mbtiQuestAdapter.notifyDataSetChanged();
    }
    position++;
    Intent intent = new Intent(MbtiQuestionActivity.this, ResultActivity.class);
    intent.putExtra(RESULT, result);
    Log.i(TAG, "onResultClick: " + result);
    startActivity(intent);


  }

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

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

Adapter.java

public class MbtiQuestAdapter extends RecyclerView.Adapter<MbtiQuestAdapter.MbtiQuestViewHolder> {


  private List<Question> questionList;
  private LayoutInflater layoutInflater;
  private final OnItemClickListener listener;


  private static final String TAG = "MbtiQuestAdapter";


  public interface OnItemClickListener {
    void onItemClick(int position, char value);
  }


  public MbtiQuestAdapter(List<Question> questionList, OnItemClickListener listener) {
    this.questionList = questionList;
    this.listener = listener;
  }


  @NonNull
  @Override
  public MbtiQuestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if (layoutInflater == null) {
      layoutInflater = LayoutInflater.from(parent.getContext());

    }

    MbtiItemRowBinding binding = DataBindingUtil.inflate(layoutInflater,
      R.layout.mbti_item_row, parent, false);


    return new MbtiQuestViewHolder(binding);
  }


  @Override
  public void onBindViewHolder(@NonNull final MbtiQuestViewHolder holder, final int position) {

    final Question question = questionList.get(position);


    holder.binding.txtQuesNum.setText(String.valueOf(position + 1));

    holder.binding.txtQuesTitle.setText(question.getQuestion());
    holder.binding.firstQues.setText(question.getAnswers().get(0).getAnswer());
    holder.binding.secondQues.setText(question.getAnswers().get(1).getAnswer());


    holder.binding.firstQues.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        listener.onItemClick(position, question.getAnswers().get(0).getValue());

      }
    });


    holder.binding.secondQues.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        listener.onItemClick(position, question.getAnswers().get(1).getValue());
      }
    });


    holder.binding.setQuestion(question);
  }


  @Override
  public int getItemCount() {
    Log.i(TAG, " List Size : " + questionList.size());
    return questionList.size();
  }


  class MbtiQuestViewHolder extends RecyclerView.ViewHolder {

    private MbtiItemRowBinding binding;

    MbtiQuestViewHolder(MbtiItemRowBinding binding) {
      super(binding.getRoot());
      this.binding = binding;

    }
  }

  public void addQuestion(List<Question> questions) {
    this.questionList.addAll(questions);
  }
}

SetUp CardStackView In Activity

 //Swipe RecyclerView with CardStackView
    cardStackView = binding.cardStackView;
    manager = new CardStackLayoutManager(this);
    mbtiQuestAdapter = new MbtiQuestAdapter(questions, this);
    cardStackView.setLayoutManager(manager);
    cardStackView.setAdapter(mbtiQuestAdapter);

I want to Say If The items Finished Do SomeThing (Show Dialog or etc.)

Thank Guys

6 Answers

Working Code

private CardStackView cardStackView;
private CardStackLayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 
    layoutManager = new CardStackLayoutManager(getApplicationContext(), this);
    cardStackView.setLayoutManager(layoutManager);
    //
}

@Override
public void onCardSwiped(Direction direction) {
    if (layoutManager.getTopPosition() == adapter.getItemCount()) {
        // -------------------- last position reached, do something ---------------------
        startActivity(new Intent(this, MainActivity.class));
    }
}
  • You have CardStackLayoutManager. When card is swiped, callback comes in onCardSwiped.
  • Here you will get top position (position displayed on top), and total item count.
  • If top_position == total_item_count, then it is last position, now you can do anything.

Below is Output or same

sc

Use position to check if you have arrived to last item. Inside onBindViewHolder method

if( position+ 1 == questionList.size()){
  // you are at the last item
}

You ca try this by using scrollChangeListener like :

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    LinearLayoutManager layoutManager=LinearLayoutManager.class.cast(recyclerView.getLayoutManager());
    int totalItemCount = layoutManager.getItemCount();
    int lastVisible = layoutManager.findLastVisibleItemPosition();

    boolean endHasBeenReached = lastVisible + 5 >= totalItemCount;
    if (totalItemCount > 0 && endHasBeenReached) {
        //you have reached to the bottom of your recycler view
    }
}

});

Some code correction, it is better not to create listeners in onBindViewHolder() because it will be set each time this method called, better to set ones in ViewHolder

For example :

Override
public void onBindViewHolder(@NonNull final MbtiQuestViewHolder holder, final int position) {
        holder.bind(questionList.get(position))
    }

class MbtiQuestViewHolder extends RecyclerView.ViewHolder {
private MbtiItemRowBinding binding;

MbtiQuestViewHolder(MbtiItemRowBinding binding) {
    super(binding.getRoot());
    this.binding = binding;

    binding.firstQues.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onItemClick(getAdapterPosition(), questionList.get(getAdapterPosition()).getAnswers().get(0).getValue());

        }
    });


    binding.secondQues.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.onItemClick(getAdapterPosition(), questionList.get(getAdapterPosition()).getAnswers().get(1).getValue());
        }
    });

}

public void bind(Question question){
    binding.txtQuesNum.setText(String.valueOf(position + 1));
    binding.txtQuesTitle.setText(question.getQuestion());
    binding.firstQues.setText(question.getAnswers().get(0).getAnswer());
    binding.secondQues.setText(question.getAnswers().get(1).getAnswer());

    binding.setQuestion(question);
}

public void addQuestion(List<Question> questions) {
    this.questionList.addAll(questions);
}

}

you could compare with CardStackLayoutManager.getTopPosition(), which should be the index position currently displayed. the CardStackView GitHub explains the methods which are available.

alternatively, just count the number of swipes:

int currentPosition = 0;

@Override
public void onCardSwiped(Direction direction) {
    currentPosition++;
}
//define the layoutmanager
private CardStackLayoutManager manager;

//make CardStackListener in layout manger and u need to implement methods 
manager = new CardStackLayoutManager(this, new CardStackListener() {
  @Override
  public void onCardDragging(Direction direction, float ratio) {

  }

  @Override
  public void onCardSwiped(Direction direction) {

  }

  @Override
  public void onCardRewound() {

  }

  @Override
  public void onCardCanceled() {

  }

  @Override
  public void onCardAppeared(View view, int position) {
    //here if u print position you will find the position of your viewed item and this function will call when card viewed 
  }

  @Override
  public void onCardDisappeared(View view, int position) {
    Log.d("onCardDisappeared: ", "" + position);
    //here if u print position you will find the position of your Disappeared item and this function will call when card disappear 
  }
}
});

cardStackView.setLayoutManager(manager);
cardStackView.setAdapter(cardStackAdapter);
Related