我正在开发一个应用程序,它使用 ViewPager 向用户显示一大堆闪存卡。我的问题如下 - 我有一个自定义的 PagerAdapter 来处理光标并从我的数据库中获取一大堆数据并将其放入视图中。我想添加一个删除卡的选项,但我不知道该怎么做。这是我的 PagerAdapter 代码。不太确定从这里去哪里。
public class CardCursorPagerAdapter extends PagerAdapter {
private Cursor cursor;
private LayoutInflater inflater;
public CardCursorPagerAdapter(Context context, Cursor cursor){
this.cursor = cursor;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void swapCursor(Cursor cursor){
this.cursor = cursor;
}
@Override
public void destroyItem(View view, int position, Object object) {
((ViewPager) view).removeView((LinearLayout) object);
}
@Override
public int getCount() {
if (cursor == null) {
return 0;
} else {
return 1000;
}
}
@Override
public Object instantiateItem(View view, int position) {
LinearLayout layout;
int position2 = position;
if (position == 0){
layout = (LinearLayout) inflater.inflate(R.layout.activity_slide_info, null);
} else {
position2++;
position2 = position2 % cursor.getCount();
cursor.moveToPosition(position2);
layout = (LinearLayout) inflater.inflate(R.layout.activity_card, null);
TextView cardTitle = (TextView) layout.findViewById(R.id.card_title);
TextView cardExample = (TextView) layout.findViewById(R.id.card_example);
TextView cardDefinition = (TextView) layout.findViewById(R.id.card_definition);
cardTitle.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_TITLE)));
cardExample.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_SENTENCE)));
cardDefinition.setText(cursor.getString(cursor.getColumnIndex(FlashCardTable.COLUMN_DEFINITION)));
}
((ViewPager) view).addView(layout);
return layout;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}