gpt4 book ai didi

java - 当我在 CardStackView 的最后一项时?

转载 作者:太空狗 更新时间:2023-10-29 14:37:41 28 4
gpt4 key购买 nike

我有 RecyclerView( CardStackView ),其中有 60 个项目。

现在完成所有项目后..我有空白页..如果所有项目都完成,我不想显示空白页...我想进入另一个 Activity 您可以看到空白页Here

当我实现 CardStackListener 时,我也像这样添加了那些 Method 。我想当所有卡片都回答 IntentResultActivity...喜欢This第 60 个问题后我想要 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);
}
}

在 Activity 中设置 CardStackView

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

我想说如果项目完成了做某事(显示对话框等)

谢谢大家

最佳答案

工作代码

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));
}
}
  • 您有 CardStackLayoutManager。刷卡时,回调来自onCardSwiped
  • 在这里您将获得最高位置(位置显示在顶部)和总项目数。
  • 如果 top_position == total_item_count,那么它是最后一个位置,现在你可以做任何事情。

下面是输出还是一样

sc

关于java - 当我在 CardStackView 的最后一项时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53353553/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com