我有一个viewPager,它必须防止反击。这是我的代码
public class testActivity extends FragmentActivity
{
ViewPager pager;
LayoutInflater inflater;
List<String> qNum;
int id=1;
MyPagesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_page);
qNum=new ArrayList<String>();
qNum.add("1");
qNum.add("2");
qNum.add("3");
qNum.add("4");
qNum.add("5");
System.out.println(qNum);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
adapter=new MyPagesAdapter();
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
id = pager.getCurrentItem();
System.out.println("id returns "+id);
}
class MyPagesAdapter extends PagerAdapter {
@Override
public int getCount() {
return qNum.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = inflater.inflate(R.layout.question_model, container,false);
System.out.println("current pos: "+position);
((TextView)view.findViewById(R.id.numberView)).setText(String.valueOf(qNum.get(0)));
((ViewPager) container).addView(view, 0);
if(position>id)
{
System.out.println("removes id "+qNum.get(0));
qNum.remove(0);
System.out.println(qNum);
adapter.notifyDataSetChanged();
id=position;
((ViewPager) container).removeView((view));
((TextView)view.findViewById(R.id.numberView)).setText(String.valueOf(qNum.get(0)));
((ViewPager) container).addView(view, 0);
return view;
}
else if(position==id)
{
System.out.println("position and id equals");
((TextView)view.findViewById(R.id.numberView)).setText(String.valueOf(qNum.get(0)));
((ViewPager) container).addView(view, 0);
return view;
}
else if(position<id)
return R.layout.finish;
return R.layout.finish_test;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==(View)arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
object=null;
}
}
}
我的问题是LogCat在从列表中删除元素后显示正确的列表,但在模拟器中,它在2次刷卡后没有变化(我只能看到问题4,但是当我离开手指时它返回到2 )。当我向右滑动时它也显示已移除的项目
我的LogCat是
05-09 02:02:58.561: I/System.out(2252): [1, 2, 3, 4, 5]
05-09 02:02:58.621: I/System.out(2252): id returns 0
05-09 02:03:01.161: I/System.out(2252): current pos: 0
05-09 02:03:01.171: I/System.out(2252): position and id equals
05-09 02:03:01.361: I/System.out(2252): current pos: 1
05-09 02:03:01.391: I/System.out(2252): removes id 1
05-09 02:03:01.391: I/System.out(2252): [2, 3, 4, 5]
05-09 02:03:28.101: I/System.out(2252): current pos: 2
05-09 02:03:28.141: I/System.out(2252): removes id 2
05-09 02:03:28.151: I/System.out(2252): [3, 4, 5]
05-09 02:03:28.351: I/System.out(2252): current pos: 2
05-09 02:03:28.381: I/System.out(2252): removes id 3
05-09 02:03:28.381: I/System.out(2252): [4, 5]
答案 0 :(得分:0)
您可以从下面的文档中获得一个很好的参考,即使它们是一个可以帮助您的示例项目。