Skip to content

Instantly share code, notes, and snippets.

@takaaki7
Last active June 5, 2016 14:02
Show Gist options
  • Select an option

  • Save takaaki7/645d8d07bbe190eab7bdc55b7f82bb13 to your computer and use it in GitHub Desktop.

Select an option

Save takaaki7/645d8d07bbe190eab7bdc55b7f82bb13 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_dark" />
<Button
android:id="@+id/remove_0_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="remove0" />
</LinearLayout>
package com.nakama.checkfragmentstatebug;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
((ViewPager) findViewById(R.id.view_pager)).setAdapter(adapter);
findViewById(R.id.remove_0_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.remove0();
}
});
}
private static class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<String> data = new ArrayList<>(Arrays.asList(new String[]{"0", "1", "2"}));
private List<Fragment> fragments = new ArrayList<>(3);
private boolean removed = false;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getItemPosition(Object object) {
int index = fragments.indexOf(object);
int ret = index != -1 ? index : POSITION_NONE;
Log.d(TAG, "getItemPosition object:" + object + ",ret:" + ret);
return ret;
}
@Override
public Fragment getItem(int position) {
Fragment f = MyFragment.newInstance(data.get(position));
Log.d(TAG, "getItem:" + position + ",f:" + f);
while (fragments.size() <= position) {
fragments.add(null);
}
fragments.set(position, f);
return f;
}
public void remove0() {
if (!removed) {
removed = true;
data.remove(0);
fragments.remove(0);
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return data.size();
}
}
public static class MyFragment extends Fragment {
public static MyFragment newInstance(String data) {
Bundle bundle = new Bundle();
bundle.putString("data", data);
MyFragment f = new MyFragment();
f.setArguments(bundle);
return f;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
textView.setText(getArguments().getString("data"));
textView.setTextSize(24);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
textView.setGravity(Gravity.CENTER);
return textView;
}
}
}
@takaaki7

takaaki7 commented Apr 6, 2016

Copy link
Copy Markdown
Author

Reproducing process:

  1. Open app.
  2. Check a page with text '0' is displayed.
  3. Tap remove0 button to remove a page 0, and check now a page '1' is displayed.
  4. Swipe left, there should be a page with text '2', but practically there is an empty page. Is it bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment