Android中从ViewPager中删除片段页
MainActivity.java
public class MainActivity extends FragmentActivity implements TextProvider { private Button mAdd; private Button mRemove; private ViewPager mPager; private MyPagerAdapter mAdapter; private ArrayList<String> mEntries = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mEntries.add("pos 1"); mEntries.add("pos 2"); mEntries.add("pos 3"); mEntries.add("pos 4"); mEntries.add("pos 5"); mAdd = (Button) findViewById(R.id.add); mRemove = (Button) findViewById(R.id.remove); mPager = (ViewPager) findViewById(R.id.pager); mAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { addNewItem(); } }); mRemove.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { removeCurrentItem(); } }); mAdapter = new MyPagerAdapter(this.getSupportFragmentManager(), this); mPager.setAdapter(mAdapter); } private void addNewItem() { mEntries.add("new item"); mAdapter.notifyDataSetChanged(); } private void removeCurrentItem() { int position = mPager.getCurrentItem(); mEntries.remove(position); mAdapter.notifyDataSetChanged(); } @Override public String getTextForPosition(int position) { return mEntries.get(position); } @Override public int getCount() { return mEntries.size(); }
炎炎设计
慕尼黑的夜晚无繁华
相关分类