ViewPager data not loading on initial app load

后端 未结 2 1416
梦毁少年i
梦毁少年i 2020-12-22 12:54

i am working on a app which i created using android studio Tabbed Activity i choose this activity so that when user swipe it load some data from a json url

2条回答
  •  Happy的楠姐
    2020-12-22 13:38

    You have to make network call first and once you receive response refresh the Viewpager or set Adapter.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Add below two lines in on create
            fetchData process = new fetchData();
            process.execute();
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    
    }
    

    And on Post execute of async task, set adapter or refresh page

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    
        MainActivity.mViewPager.setAdapter(mSectionsPagerAdapter);
    
        MainActivity.factImg.setVisibility(View.VISIBLE);
        MainActivity.factTitle.setText(this.factTitle);
        MainActivity.factDesc.setText(this.factDesc);
        Picasso.get().load(factImg).placeholder(R.drawable.defaultthumb).into(MainActivity.factImg);
        MainA
    

提交回复
热议问题