1

This is my Dashboard_adapt Adapter Class.

public class Dashboard_adapt extends FragmentStatePagerAdapter {
    Context context;
    ArrayList<Fragment> fragments = new ArrayList<>();


    public Dashboard_adapt(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
        setFragments();
    }
    private void setFragments(){
        fragments.add(new Home_dashboard());
        fragments.add(new Search_dashboard());
        fragments.add(new Add_dashboard());
        fragments.add(new Favourite_dashboard());
        fragments.add(new User_dashboard());
    }


    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        if (object != null) {
            return ((Fragment) object).getView() == view;
        } else {
            return false;
        }
    }

    @Override
    public Fragment getItem(int position) {
        Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

THis is the MainActivity of ViewPager and TabLayout.

    public class Dashboard extends AppCompatActivity {
private FragmentManager fm = null;

 private int[] ImageViar = {
            R.drawable.home_dash,
            R.drawable.ic_search_black_24dp,
            R.drawable.plus,
            R.drawable.heart,
            R.drawable.user_dash
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        ((AppCompatActivity) this).getSupportActionBar().hide();
        fm = getSupportFragmentManager();
        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_dash);
        final ViewPager viewPager = (ViewPager) findViewById(R.id.fragment_changer_dashboard);
        Dashboard_adapt adapter = new Dashboard_adapt(fm, this);

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons(tabLayout);
 }

the Adapter class return two Toast on First time.. and the every next time the next Fragment class will open in ViewPager.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ahsan Saeed
  • 41
  • 1
  • 2
  • 9
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jun 19 '17 at 07:19
  • Please do not insist on begging messages in your posts, especially after it has been indicated it is not welcome. It really doesn't help the speed of obtaining answers. I have downvoted. Please don't make it necessary to involve a moderator too! – halfer Jun 19 '17 at 15:47

1 Answers1

0

This is the default behaviour of ViewPager.. The ViewPager loads also "offscreen" pages to optimize swipe gestures... You probably see the toasts for positions =0 and 1.. Check out the setOffscreenPageLimit method... to control the number of "pages"

convexHull
  • 1,761
  • 2
  • 15
  • 18
  • Dear @convexHull can you Suggest any Tutorial for this problem.. or link? – Ahsan Saeed Jun 20 '17 at 17:12
  • Sorry, I don't have any tutorial for that.. Search the internet. Your question has been answered many times... (e.g: https://www.google.cz/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/30342569/getitem-called-twice-and-this-causes-tab1-and-tab2-both-executed-in-fragmentpage&ved=0ahUKEwjFr-P3kc3UAhWQF8AKHYtCBW8QFgghMAI&usg=AFQjCNF4aYh9qOdbRPzVUkp4cJylyoQpbA&sig2=hQ5XA3Gvc8WNE1Za2amuJA) Experiment with offscreen page limit value in your ViewPager – convexHull Jun 20 '17 at 19:28
  • If you want to display the Toast only when pages are changed, you should use https://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html – convexHull Jun 20 '17 at 19:30
  • convexHull it is possible to apply Tag, and then fix this issue?? – Ahsan Saeed Jun 20 '17 at 19:45