这段代码非常简单。我有一个 ViewPager,里面有几个 Fragment。和一个“添加新寻呼机”按钮将新寻呼机添加到 ViewPager 的第一个位置。
这是我的代码
public class TestPagerActivity extends SherlockFragmentActivity {
private ArrayList<String> mData = new ArrayList<String>();
private ViewPager mPager;
private TestPagerAdapter mAdapter;
@Override
protected void onCreate(Bundle b) {
// TODO Auto-generated method stub
super.onCreate(b);
setContentView(R.layout.testpager);
mData.add("Looking");
mData.add("into");
mData.add("your");
mData.add("dark");
mData.add("blue");
mData.add("eyes");
mPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TestPagerAdapter(getSupportFragmentManager(), mData);
mPager.setAdapter(mAdapter);
}
public void onAddNewPagerClick(View v) {
mData.add(0, "When");
mAdapter.notifyDataSetChanged();
mPager.invalidate();
}
public static class TestPagerAdapter extends FragmentStatePagerAdapter {
private List<String> mData;
public TestPagerAdapter(FragmentManager fm) {
super(fm);
}
public TestPagerAdapter(FragmentManager fm, List<String> data) {
super(fm);
mData = data;
}
public void updateAdapter(List<String> data) {
mData = data;
notifyDataSetChanged();
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(mData.get(position), position + "/" + mData.size());
}
@Override
public int getCount() {
return mData.size();
}
}
public static class TestFragment extends SherlockFragment {
private static final String KEY_CONTENT = "TestFragment:Content";
private static final String KEY_POSITION = "TestFragment:Position";
public static TestFragment newInstance(String content, String position) {
TestFragment fragment = new TestFragment();
StringBuilder builder = new StringBuilder();
builder.append("Page ").append(position).append("\n");
for (int i = 0; i < 20; i++) {
builder.append(content).append(" ");
}
builder.deleteCharAt(builder.length() - 1);
fragment.mContent = builder.toString();
return fragment;
}
private String mContent = "???";
private String mPosition = "x/y";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
mPosition = savedInstanceState.getString(KEY_POSITION);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText(mContent);
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(text);
return layout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
outState.putString(KEY_POSITION, mPosition);
}
}
}
问题是:当我在添加新寻呼机后刷新 ViewPager 时,其内容无法正确显示。一开始我有6个页面,但是添加一个新页面后,这里仍然显示6个页面,而我预计有7个页面。
谁能告诉我这里有什么问题?感谢您的任何建议。