我的应用视图A和B中有两个回收站视图,如果您触摸视图A中的卡片它是从视图A克隆到视图B,如果您从视图B触摸卡片,它只是从其视图中删除,我的问题是视图A中的卡片,无论你按哪一张它总是只是从视图A添加第一张卡片到视图B,你可以按下你想要的任何一张,它只是添加第一张然后第二张然后第三张然后第三张等等,
当按下一个小吃栏时,我会显示一个小吃吧,它会显示该卡的一些信息,此信息始终是正确的,
实例;
如果我按的第一张卡是1
snackbar显示卡1按下,实际添加的卡是卡1,
如果我然后选择了卡4
snackbar显示卡4已按下,但添加的卡将为卡2
如果我选择卡6
snackbar显示卡6已按下,但添加的卡将为3
所以onClick方法的位置正确,但我添加卡片的方法一定是错的,我不知道如何纠正它,也许这里有人可以提供帮助,
这是我点击回收者视图B
recyclerView.addOnItemTouchListener(new
RecyclerTouchListener(getActivity().getApplicationContext(), recyclerView,
new ClickListener() {
@Override
public void onClick(View view, int position) {
CardWriter cardWriter = cardMakerList.get(position);
addTo(SpeakGrid.cardMakerList.size());
Snackbar.make(view, "This card is " +
cardWriter.getCardEmotion() + " and it would say " +
cardWriter.getCardEmotion(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
这是我的addTo方法
public void addTo(int position) {
CardWriter cardWriter = cardMakerList.get(position);
SpeakGrid.cardMakerList.add(position, cardWriter);
SpeakGrid.cardAdapter.notifyItemInserted(position);
SpeakGrid.cardAdapter.notifyItemRangeChanged(position,
cardMakerList.size());
SpeakGrid.textToSpeak.append(cardWriter.getSpeechText());
}
任何帮助表示赞赏
答案 0 :(得分:0)
我最终抛弃了方法并在onclick调用中执行了所有操作,这样位置始终是正确的,所以我的代码现在看起来像这样
public void onClick(View view, int position) { //gets correct position
CardWriter cardWriter = cardMakerList.get(position);
SpeakGrid.cardMakerList.add(SpeakGrid.cardMakerList.size(),cardWriter);
SpeakGrid.cardAdapter.notifyItemInserted(SpeakGrid.cardMakerList.size());
//i update two recyclerviews
SpeakRecyclerGrid.cardMakerList.add
(SpeakRecyclerGrid.cardMakerList.size(),cardWriter);
SpeakRecyclerGrid.cardAdapter.notifyItemInserted
(SpeakGrid.cardMakerList.size());