2
我不知道如何解釋這個,所以我上傳了一個視頻Youtube. 當我添加頁面時,一切正常。如果我從列表的末尾開始並逐步移動到第一頁,也刪除頁面作品,但如果我刪除了0-端之間的地方,然後我添加頁面,則會出現空白頁。片段被創建,但觀點不是。Android viewpager:添加/刪除頁面時出現奇怪的錯誤
我的代碼有問題嗎?是的,我已經看到this的問題,返回POSITION_NONE
不是一種選擇,因爲我正在從web上下載內容並在listview中顯示它的應用程序,所以每次用戶刪除頁面時重新創建片段不是很有效。
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;
}
}
}
activity_main.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>
fragment_layout.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" />