当我进入CardStackView的最后一个项目时?

时间:2018-11-17 17:10:30

标签: java android android-recyclerview

我有RecyclerViewCardStackView)有60个项目。

现在所有项目都完成后..我有空白页..如果所有项目都完成了,我不想显示空白页...我要进行其他活动您可以看到空白页Here

当我实现CardStackListener时,我也像这样添加了Method。我想当所有卡都回答IntentResultActivity ...就像This第60个问题后,我希望InstedOf空白页意图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);
  }
}

在活动中设置CardStackView

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

我想说项目是否完成了某件事(显示对话框等)

谢谢你们

6 个答案:

答案 0 :(得分:0)

使用arg2检查您是否到达最后一个项目。内部 (* I have a section with many variables and definitions. *) Section SectionWithDefs. Context {A B C: Type}. Class Arg1 : Type := arg1 : A -> B. Context `{IArg1 : Arg1}. Class Arg2 : Type := arg2 : B -> C. Context `{IArg2 : Arg2}. (* Functions that uses these variables. *) Definition f a := arg2 (arg1 a). (* ... *) End SectionWithDefs. (* Now I want to use some of these functions. *) Section AnotherSection. Context {A B C: Type}. (* Here are the arguments. *) Context `{MyIArg1 : Arg1 A B}. Context `{MyIArg2 : Arg2 B C}. Variable a: A. Section CallFunctionWithInstances. (* The implicit type class arguments [IArg1] and [IArg2] are resolved using instances in scope...*) Compute (f a). End CallFunctionWithInstances. End AnotherSection. 方法

position

答案 1 :(得分:0)

您可以通过以下方式使用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
    }
}

});

答案 2 :(得分:0)

某些代码更正,最好不要在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);
}

}

答案 3 :(得分:0)

您可以将CardStackLayoutManager.getTopPosition()与之比较,后者应该是当前显示的索引位置。 CardStackView GitHub解释了可用的方法。

或者,只需计算滑动次数:

int currentPosition = 0;

@Override
public void onCardSwiped(Direction direction) {
    currentPosition++;
}

答案 4 :(得分:0)

工作代码

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

答案 5 :(得分:0)

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