Not sequentially displaying FragmentStatePagerAdapter's elements.



  • There's a list of words that should be consistently displayed on the screen when they're singing. With MainActivity in FragmentActivity - method public Fragment getItem(int position) He's working twice at once, and he's in the logic:

    • Position: 0 (list element with index 0)

    • Position: 1 (list element with index 1)

    After which, when the NEXT button is pressed again, the list element with index 1 (position 2) is re-emerged, and the list element with index 0 is not displayed at the beginning or at the end, all other elements are in order and displayed correctly!

    TOTAL: The element of the list with an index 0 is not displayed at any time, the element of the list with index 1 is displayed twice, the rest of the elements are displayed correctly.

    FragmentActivity.java

    public class LearnActivity extends AppCompatActivity {
    
    private static final int NUM_PAGES = 6;
    private static final String TAG = "LEARN ACTIVITY";
    
    private ViewPager mPager;
    private Button buttonNext;
    
    private PagerAdapter mPagerAdapter;
    
    ArrayList<String> words;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_learn);
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    
    
        mPager.setAdapter(mPagerAdapter);
        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onPageSelected");
                invalidateOptionsMenu();
            }
        });
    
        ((Button) findViewById(R.id.btnNext)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPager.setCurrentItem(mPager.getCurrentItem() + 1);
            }
        });
    
        fillArray();
    
    }
    
    private void fillArray() {
        words = new ArrayList<>();
        words.add(0,"Officia");
        words.add(1,"Lorem ipsum");
        words.add(2,"Minim veniam");
        words.add(3,"Quis nostrud");
        words.add(4,"Exercitation ");
        words.add(5,"Ullamco");
    
    }
    
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            Log.d(TAG,"Position: " + position);
            return FragmentLearnActivity.create(position, words.get(position));
        }
    
        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
    

    }

    Fragment.java

    public class FragmentLearnActivity extends Fragment {

    public static final String ARG_PAGE = "page";
    
    private int mPageNumber;
    
    static String word;
    
    public static FragmentLearnActivity create(int pageNumber,String s) {
        FragmentLearnActivity fragment = new FragmentLearnActivity();
        word = s;
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);
        fragment.setArguments(args);
        return fragment;
    }
    
    public FragmentLearnActivity() {
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPageNumber = getArguments().getInt(ARG_PAGE);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout containing a title and body text.
        ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_learn_activity, container, false);
    
        ((TextView)rootView.findViewById(R.id.mainWord)).setText(word);
    
        // Set the title view to show the page number.
    

    // ((TextView) rootView.findViewById(android.R.id.text1)).setText(
    // getString(R.string.title_template_step, mPageNumber + 1));

        return rootView;
    }
    
    /**
     * Returns the page number represented by this fragment object.
     */
    public int getPageNumber() {
        return mPageNumber;
    }
    

    }



  • Your problem is the variable. word in the fragment class is static. Which means that it's all about copies of the class. So, fragments for ViewPager default shall be created for 0 and 1 position, the variable shall be assigned the value words.get(1)♪ In the future, by creating new fragments, you rewrite it.

    Decision

    Get the retrofit. static variable word Your fragment. And put her weight in. Bundle together with the position. Just like the last one. onCreate




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2