我正在尝试制作类似火种的可刷卡。我有这个图书馆https://github.com/kikoso/Swipeable-Cards。我按照演示进行了,效果很好。但是,该演示只有一个cardModel可以设置为carddismissedlisterner。如何使适配器中的所有cardModels工作?我想在关闭后获取每个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);
}
/ ******************更新****************** /
我尝试在所有cradModels上重复setOnCardDismissedListener。我想保存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");
}
});
最佳答案
只需在所有卡型号上重复该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
的类实例,并将其设置在每个卡上。关于android - Android Tinder,例如可滑动刷卡setOnCardDismissedListener,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25295927/