我有一张卡片清单,每张卡片都有两面(正面和背面)。我想在ViewPager中将此卡片(正面)显示为CardView,当用户单击卡片时,卡片将翻转并出现在背面,然后我也需要显示一个按钮以在MainAcitvity中转到下一张卡片(正面), 如何检测ViewPager在MainActiviy中单击以在ViewPager中显示下一张卡片?
public Object instantiateItem(ViewGroup container, int position) {
TextView txtFront, txtBack, txtFrontHeader, txtBackHeader;
final CardView fr,bk;
layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.cards_review_layout,container,false);
String s = context.getString(R.string.box);
txtFrontHeader = (TextView) view.findViewById(R.id.front_header_txt);
txtBackHeader = (TextView) view.findViewById(R.id.back_header_txt);
txtFront = (TextView) view.findViewById(R.id.display_card_front_text);
txtBack = (TextView) view.findViewById(R.id.display_card_back_text);
fr = (CardView) view.findViewById(R.id.card_front);
bk = (CardView) view.findViewById(R.id.card_back);
txtFrontHeader.setText(cardList.get(position).getGroupName() + " " + cardList.get(position).getId());
txtBackHeader.setText(s + " " + boxString(cardList.get(position).getBoxNo()));
txtFront.setText(cardList.get(position).getFront());
txtBack.setText(cardList.get(position).getRear());
fr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fr.animate().rotationX(-90).setDuration(300).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
fr.setVisibility(View.GONE);
bk.setRotationX(90);
bk.setVisibility(View.VISIBLE);
bk.animate().rotationX(0).setDuration(300).setListener(null);
}
});
}
});
bk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bk.animate().rotationX(-90).setDuration(300).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
bk.setVisibility(View.GONE);
fr.setRotationX(90);
fr.setVisibility(View.VISIBLE);
fr.animate().rotationX(0).setDuration(300).setListener(null);
}
});
}
});
container.addView(view,0);
return view;
}
我在MainActivity中使用此代码,但是onClickListener不起作用
adapter=new Adapter(cards,this);
viewPager=(ViewPager) findViewById(R.id.pager_review);
viewPager.setAdapter(adapter);
viewPager.setPadding(0,0,0,0);
btn=(Botton) findViewById(R.id.next_card);
viewPager.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(View.VISIBLE);
}
});