2014-05-09 52 views
0

我有一個viewPager,它必須防止背部滑動。這裏是我的代碼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] 

回答

相關問題