初始应用加载时未加载ViewPager数据

时间:2018-10-24 12:42:00

标签: java android android-viewpager

我正在使用我使用android studio Tabbed Activity 创建的应用程序进行工作,因此我选择了此活动,以便在用户滑动时从json url加载一些数据,并且我创建了另一个可获取JSON数据的类在onCreateView(LayoutInflater inflater, ViewGroup container方法上运行,并且一切正常,除了当应用程序启动时以及当我调用mViewPager.setAdapter(mSectionsPagerAdapter)时从create方法上的主要活动进入时,布局中都没有数据填充当我从右向左滑动时才显示

MainActivity

public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    static TextView factTitle;
    static TextView factDesc;
    static ImageView factImg;
    static ProgressBar loader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";
        public PlaceholderFragment() {
        }
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            factTitle = (TextView) rootView.findViewById(R.id.fact_label);
            factDesc = (TextView) rootView.findViewById(R.id.fact_description);
            factImg = (ImageView) rootView.findViewById(R.id.imageView);
            loader = (ProgressBar) rootView.findViewById(R.id.progressBar);
            fetchData process = new fetchData();
            process.execute();
            return rootView;
        }
    }
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
          Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }
        @Override
        public int getCount() {
            return 999;
        }
    }
}

fetchData类

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data ="";
    String factTitle = "";
    String factDesc = "";
    String factImg ="";
    String singleParsed ="";
    String DoubleParsed ="";
    String tripleParsed ="";
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("http://xxxxxx.com/api");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null){
                line = bufferedReader.readLine();
                data = data + line;
            }
            JSONArray JA = new JSONArray(data);
            for(int i =0 ;i <JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);
                singleParsed =  ""+JO.get("fact");
                DoubleParsed =  ""+JO.get("factdesc");
                tripleParsed =  ""+JO.get("img");
                factTitle = factTitle + singleParsed;
                factDesc  = factDesc + DoubleParsed;
                factImg  = factImg + tripleParsed;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        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);
        MainActivity.loader.setVisibility(View.INVISIBLE);
    }
}

2 个答案:

答案 0 :(得分:0)

您必须先拨打网络电话,一旦收到响应,请刷新Viewpager或设置适配器。

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);

}

然后在执行异步任务后,设置适配器或刷新页面

@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

答案 1 :(得分:0)

像这样更新两个类,可能对您有帮助!

fetchDataclass:

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data ="";
    String factTitle = "";
    String factDesc = "";
    String factImg ="";
    String singleParsed ="";
    String DoubleParsed ="";
    String tripleParsed ="";
    Activity mContext;
    TextView mfactTitle,mfactDesc;
    ImageView mfactImg;
    ProgressBar mProgressbar;

   public fetchData(Activity context,TextView mfactTitle,TextView 
   mfactDesc,ImageView mfactImg,ProgressBar mProgressbar){
   this.context=context;
   this.mfactTitle=mfactTitle; 
   this.mfactDesc=mfactDesc;
   this.mfactImg=mfactImg;
   this.mProgressbar=mProgressbar;
   }
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("http://xxxxxx.com/api");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null){
                line = bufferedReader.readLine();
                data = data + line;
            }
            JSONArray JA = new JSONArray(data);
            for(int i =0 ;i <JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);
                singleParsed =  ""+JO.get("fact");
                DoubleParsed =  ""+JO.get("factdesc");
                tripleParsed =  ""+JO.get("img");
                factTitle = factTitle + singleParsed;
                factDesc  = factDesc + DoubleParsed;
                factImg  = factImg + tripleParsed;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        mfactImg.setVisibility(View.VISIBLE);
        mfactTitle.setText(this.factTitle);
        mfactDesc.setText(this.factDesc);
     Picasso.get().load(factImg).placeholder(R.drawable.defaultthumb).into(mfactImg);
       mProgressBar.setVisibility(View.INVISIBLE);
    }
}

PlaceHolder片段:

public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            factTitle = (TextView) rootView.findViewById(R.id.fact_label);
            factDesc = (TextView) rootView.findViewById(R.id.fact_description);
            factImg = (ImageView) rootView.findViewById(R.id.imageView);
            loader = (ProgressBar) rootView.findViewById(R.id.progressBar);
            fetchData process = new fetchData(getActivity(),factTitle,factDesc,factImg,loader);
            process.execute();
            return rootView;
        }
    }