这段代码非常简单。我有一个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页。
谁能告诉我这里有什么问题?谢谢你的任何建议。
答案 0 :(得分:2)
首先要注意的是,您在页面列表的前面插入了新项目,但是您还没有实现PagerAdapter方法getItemPosition(Object)
来告诉ViewPager页面移动后的位置数据集的变化。正如该方法的文档所述,默认实现报告页面永远不会改变位置。如果适配器未正确实现此方法,则具有更改位置的页面的ViewPager将无法正常运行。
答案 1 :(得分:0)
避免使用FragmentPagerAdapter并使用FragmentStatePagerAdapter。
我希望它也适合你。