我正在尝试制作一种类似火药的可刷卡片。我有这个库https://github.com/kikoso/Swipeable-Cards。我按照演示,它工作正常。但是,该演示只有一个cardModel是setoncarddismissedlisterner。如何让适配器中的所有cardModel工作?我希望在被解雇之后得到每张卡片模型的信息(例如标题,文字)
private CardContainer mCardContainer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainlayout);
mCardContainer = (CardContainer) findViewById(R.id.layoutview);
Resources r = getResources();
SimpleCardStackAdapter adapter = new SimpleCardStackAdapter(this);
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title4", "Description goes here", r.getDrawable(R.drawable.picture1)));
adapter.add(new CardModel("Title5", "Description goes here", r.getDrawable(R.drawable.picture2)));
adapter.add(new CardModel("Title6", "Description goes here", r.getDrawable(R.drawable.picture3)));
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1)));
CardModel cardModel = new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1));
String title = cardModel.getTitle();
cardModel.setOnClickListener(new CardModel.OnClickListener() {
@Override
public void OnClickListener() {
Log.i("Swipeable Cards","I am pressing the card");
}
});
cardModel.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
@Override
public void onLike() {
Log.i("Swipeable Cards","I like the card");
datasource.createComment(title);
}
@Override
public void onDislike() {
Log.i("Swipeable Cards","I dislike the card");
datasource.deleteComment(title);
}
});
adapter.add(cardModel);
mCardContainer.setAdapter(adapter);
}
/ ****************** 更新 ***************** * / 我尝试在所有的cradModel上重复setOnCardDismissedListener。如果是onLike,我想保存cardModel的标题。但是,我检查了我的数据库,我确实保存了两次,但两个标题都是" title2",我想知道为什么" title1"没有被保存。
CardModel cardModel1 = new CardModel("Title1", "Description goes here",
r.getDrawable(R.drawable.picture1));
final String title1 = cardModel1.getTitle();
CardModel cardModel2 = new CardModel("Title2", "Description goes here",
r.getDrawable(R.drawable.picture1));
final String title2 = cardModel2.getTitle();
adapter.add(cardModel1);
adapter.add(cardModel2);
cardModel1.setOnClickListener(new CardModel.OnClickListener() {
@Override
public void OnClickListener() {
Log.i("Swipeable Cards", "I am pressing the card");
}
});
cardModel1
.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
// onLike swipe to left, but we want it to be removed
@Override
public void onLike() {
Log.i("Swipeable Cards", "I like the card");
datasource.createComment(title1);
}
// disLike swipe to right, but we want it to be saved
@Override
public void onDislike() {
Log.i("Swipeable Cards", "I dislike the card");
}
});
cardModel2.setOnClickListener(new CardModel.OnClickListener() {
@Override
public void OnClickListener() {
Log.i("Swipeable Cards", "I am pressing the card");
}
});
cardModel2
.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
// onLike swipe to left, but we want it to be removed
@Override
public void onLike() {
Log.i("Swipeable Cards", "I like the card");
datasource.createComment(title2);
}
// disLike swipe to right, but we want it to be saved
@Override
public void onDislike() {
Log.i("Swipeable Cards", "I dislike the card");
}
});
答案 0 :(得分:1)
在您的所有卡型号上重复setOnCardDimissedListener
。
如果界面返回某种令牌,您可以为所有卡片型号设置一个OnCardDimissedListener
,但不幸的是,如果您这样做,您将无法知道对哪张卡片采取了行动。
您需要在您创建的每个卡模型上设置不同的OnCardDimissedListener
实例。
编辑 - 你有没看过github页面?
Additionally, you can set up a Delegate to be notified when the image is being liked or disliked:
card.setOnCardDimissedDelegate(new CardView.OnCardDimissedDelegate() {
@Override
public void onLike(CardView cardView) {
Log.d("Swipeable Card", "I liked it");
}
@Override
public void onDislike(CardView cardView) {
Log.d("Swipeable Card", "I did not liked it");
}
});
创建一个OnCardDimissedDelegate
的类实例并在每张卡片上设置它。