Last active
June 5, 2016 14:02
-
-
Save takaaki7/645d8d07bbe190eab7bdc55b7f82bb13 to your computer and use it in GitHub Desktop.
Reproducing FragmentStatePager bug: https://code.google.com/p/android/issues/detail?id=19001&can=1&q=FragmentStatePagerAdapter&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reproducing process: