ViewPager 数据未在初始应用程序加载时加载
ViewPager data not loading on initial app load
我正在开发我使用 android studio 创建的应用程序 Tabbed Activity 我选择这个 activity 这样当用户滑动时它从 json url 加载一些数据,我创建了另一个 class ,它在 onCreateView(LayoutInflater inflater, ViewGroup container
方法上获取 JSON 数据,这一切都很好,除了当应用程序启动和从 main activity 调用创建方法时,当我调用 mViewPager.setAdapter(mSectionsPagerAdapter)
时,布局中没有填充任何数据 我想要的是当加载应用程序 MainActivity 时,我想显示仅当我从中滑动时才显示的数据从右到左
主要Activity
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;
}
}
}
获取数据Class
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);
}
}
您必须先进行网络调用,一旦收到响应,请刷新 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);
}
并在 Post 执行异步任务、设置适配器或刷新页面
@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
更新你的两个 类 这样可能对你有帮助!
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);
}
}
占位符片段:
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;
}
}
我正在开发我使用 android studio 创建的应用程序 Tabbed Activity 我选择这个 activity 这样当用户滑动时它从 json url 加载一些数据,我创建了另一个 class ,它在 onCreateView(LayoutInflater inflater, ViewGroup container
方法上获取 JSON 数据,这一切都很好,除了当应用程序启动和从 main activity 调用创建方法时,当我调用 mViewPager.setAdapter(mSectionsPagerAdapter)
时,布局中没有填充任何数据 我想要的是当加载应用程序 MainActivity 时,我想显示仅当我从中滑动时才显示的数据从右到左
主要Activity
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;
}
}
}
获取数据Class
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);
}
}
您必须先进行网络调用,一旦收到响应,请刷新 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);
}
并在 Post 执行异步任务、设置适配器或刷新页面
@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
更新你的两个 类 这样可能对你有帮助!
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);
}
}
占位符片段:
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;
}
}