我不知道如何解释,所以我在Youtube 上上传了一个视频。 当我添加页面时一切正常。如果我从列表末尾开始并逐步移动到第一页,则删除页面也有效,但是如果我删除 0-end 之间的某个位置,然后添加页面,我会得到一个空白页面。片段已创建,但视图未创建。
我的代码有问题吗?是的,我已经看到了这个问题,返回POSITION_NONE
不是一个选项,因为我正在处理的应用程序从网络下载内容并将其显示在列表视图中,因此每次用户删除页面时重新创建片段并不是很有效。
Button click: Result:
Add page [Fragment 1]
Add page [Fragment 1][Fragment 2]
Add page [Fragment 1][Fragment 2][Fragment 3]
remove page(position2) [Fragment 1][Fragment 2]
remove page(position1) [Fragment 1]
Add page [Fragment 1][Fragment 2]
remove page(position1) [Fragment 1]
remove page(position0) ---No pages---
Add page [Fragment 0]
Add page [Fragment 0][Fragment 1]
Add page [Fragment 0][Fragment 1][Fragment 2]
add page [Fragment 0][Fragment 1][Fragment 2][fragment 3]
remove page(position2) [Fragment 0][Fragment 1][Fragment 3]
add page [Fragment 0][Fragment 1][Fragment 3][Blank page]
remove page(position0) [Fragment 1][Fragment 3][Fragment 3][Blank page]
MainActivity.class
public class MainActivity extends FragmentActivity {
private static final String TAG = "MainActivity";
private ViewPager mViewpager;
private ArrayList<Fragment> fragmentslist;
private SectionsPagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentslist = new ArrayList<Fragment>();
mPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewpager = (ViewPager)findViewById(R.id.pager);
mViewpager.setAdapter(mPagerAdapter);
mViewpager.setOffscreenPageLimit(4);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item){
switch(item.getItemId()){
case R.id.menu_add_page:
Bundle bundle = new Bundle();
bundle.putInt("Position", fragmentslist.size());
fragment frag = new fragment();
frag.setArguments(bundle);
fragmentslist.add(frag);
mPagerAdapter.notifyDataSetChanged();
mViewpager.setCurrentItem(fragmentslist.indexOf(frag));
Log.i(TAG, "Add fragment");
break;
case R.id.menu_remove_page:
if(fragmentslist.size() > 0){
mPagerAdapter.removeItem(mViewpager.getCurrentItem());
}
Log.i(TAG, "Removing page " + mViewpager.getCurrentItem());
break;
}
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentslist.get(position);
}
@Override
public int getCount() {
return fragmentslist.size();
}
@Override
public int getItemPosition(Object object){
int index = fragmentslist.indexOf(object);
if(index != -1){
Log.i(TAG, "Position: " + index);
return index;
}
else{
Log.i(TAG, "POSITION_NONE");
return POSITION_NONE;
}
}
public void removeItem(int position){
Fragment fragment = (Fragment)fragmentslist.get(position);
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragmentslist.remove(position);
notifyDataSetChanged();
}
}
public static class fragment extends Fragment{
private String colors[] = {"#FF0000", "#00FF00", "#FFFF00", "#FF6600"};
public View onCreateView(LayoutInflater inflater, ViewGroup root, Bundle savedInstanceState){
View mView = inflater.inflate(R.layout.fragment_layout, null);
RelativeLayout layout = (RelativeLayout)mView.findViewById(R.id.relativelayout);
TextView text = (TextView)mView.findViewById(R.id.textView1);
int position = this.getArguments().getInt("Position");
text.setText("Page: " + position);
layout.setBackgroundColor(Color.parseColor(colors[position]));
Log.i("fragment", "Page: " + position + " view created");
return mView;
}
}
}
活动主.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
片段布局.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/relativelayout">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Large Text"
android:textSize="40sp" />