Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我在CardStackView的最后一项中时?_Java_Android_Android Recyclerview - Fatal编程技术网

Java 当我在CardStackView的最后一项中时?

Java 当我在CardStackView的最后一项中时?,java,android,android-recyclerview,Java,Android,Android Recyclerview,我有60件物品的回收视图 现在完成所有项目后。。我有空白页。。我不想显示空白页,如果所有项目完成…我想专注于另一个活动,你可以看到空白页 当我实现CardStackListener时,我也像这样添加了方法。我想当所有卡片都回答Intent到ResultActivity…就像在第60个问题之后,我想取代空白页Intent到ResultActivity Adapter.java public class MbtiQuestAdapter extends RecyclerView.Adapter&

我有60件物品的回收视图

现在完成所有项目后。。我有空白页。。我不想显示空白页,如果所有项目完成…我想专注于另一个活动,你可以看到空白页

当我实现
CardStackListener
时,我也像这样添加了
方法
。我想当所有卡片都回答
Intent
ResultActivity
…就像在第60个问题之后,我想取代空白页Intent到
ResultActivity



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

我想说完成的项目是否有作用(显示对话框等)


感谢各位

使用
位置
检查您是否已到达最后一项。内部
onBindViewHolder
方法

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

您可以使用scrollChangeListener进行尝试,如:

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

}))

一些代码更正,最好不要在
onBindViewHolder()
中创建监听器,因为每次调用此方法时都会设置监听器,最好在ViewHolder中设置监听器

例如:

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);
}
覆盖
公共无效onBindViewHolder(@NonNull final MbtiQuestViewHolder,final int位置){
持有者绑定(问题列表获取(位置))
}
类MbtiQuestViewHolder扩展了RecyclerView.ViewHolder{
私有MbtiItemRowBinding绑定;
MbtiQuestViewHolder(MbtiItemRowBinding绑定){
super(binding.getRoot());
这个。绑定=绑定;
binding.firstQues.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
listener.onItemClick(getAdapterPosition(),questionList.get(getAdapterPosition()).getAnswers().get(0.getValue());
}
});
binding.secondQues.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
netimclick(getAdapterPosition(),questionList.get(getAdapterPosition()).getAnswers().get(1.getValue());
}
});
}
公众假期(问题){
binding.txtQuesNum.setText(String.valueOf(position+1));
binding.txtQuesTitle.setText(question.getquestile());
binding.firstQues.setText(question.getAnswers().get(0.getAnswer());
binding.secondQues.setText(question.getAnswers().get(1.getAnswer());
约束性问题(问题);
}
公共问题(列出问题){
this.questionList.addAll(问题);
}

}

您可以与当前显示的索引位置相比较。GitHub解释了可用的方法

或者,只需计算刷卡次数:

int currentPosition = 0;

@Override
public void onCardSwiped(Direction direction) {
    currentPosition++;
}
工作代码
  • 您有
    CardStackLayoutManager
    。刷卡时,回拨会在刷卡时出现
    oncardswipped
  • 在这里,您将获得顶部位置(顶部显示的位置)和项目总数
  • 如果
    top\u position==total\u item\u count
    ,那么它是最后一个位置,现在您可以做任何事情
下面是输出或相同

谢谢,亲爱的…但这对我不起作用…当我在57项中时,吐司在60后不显示
if(position+1==questionList.size()){toast.makeText(holder.itemView.getContext(),“我在最后”,toast.LENGTH_SHORT.Show();}
我想知道如果所有卡片都完成了,而我在空白页,对话框/祝酒词显示…这意味着你完成了…亲爱的。。2.我还没有看过。。我的应用程序崩溃了。。我有CardStackView
java.lang.ClassCastException:com.yuyakaido.android.CardStackView.CardStackLayoutManager无法强制转换为androidx.recyclerview.widget.LinearLayoutManager
logcat@sana-伊巴迪:我以为前面的答案解决了这个问题,我要看一看。@sanaebadi也添加了比较。只是不确定
.gett()
是否返回零或基于一的位置索引。。。或者索引编号的方向。最后一张卡片也可能是位置
0
1
,而不是物品计数,具体取决于方向。@sanaebadi我怎么知道?
CardStackLayoutManager.gett()返回哪个值,比如第一张和最后一张卡?您可能需要实现
CardStackListener
,以便访问这些事件侦听器。@sanaebadi您需要
onCardSwiped()
CardStackLayoutManager
实例的句柄。。。第一张卡和最后一张卡几乎都有位置
0
,至少不在
onCardSwiped()
的范围内。如果
get反对派()
不起作用…您可以简单地添加一个计数器变量
int position=0
然后用
position++增加它的值
onCardSwiped()
只需
位置++而变量需要在该范围之外定义。我在实现CardStackListener时更新了我的问题,这是plz查看所有这些变量的方法
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);
}
int currentPosition = 0;

@Override
public void onCardSwiped(Direction direction) {
    currentPosition++;
}
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));
    }
}
//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);