ViewPager data not loading on initial app load(ViewPager 数据未在初始应用加载时加载)
问题描述
我正在开发一个使用 android studio 创建的应用程序 Tabbed Activity 我选择了这个活动,以便当用户滑动它时,它会从 json url 加载一些数据,然后我创建了另一个获取 JSON 数据的类在 onCreateView(LayoutInflater inflater, ViewGroup container
方法上,这一切正常,除了当应用程序启动时和从创建方法上的主要活动时调用 mViewPager.setAdapter(mSectionsPagerAdapter)
没有数据填充布局我想要的是当应用程序 MainActivity 加载时我想显示只有当我从右向左滑动时才显示的数据
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 and i created another class which fetch JSON data on onCreateView(LayoutInflater inflater, ViewGroup container
method and this all works fine except when app starts and from main activity on create method when i call mViewPager.setAdapter(mSectionsPagerAdapter)
no data is filled in layout what i want is when app MainActivity loaded i want to show data which only now show when i swipe from right to left
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);
}
}
推荐答案
像这样更新你的两个类可能对你有帮助!
Update your both classes like that may be this help you!
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;
}
}
这篇关于ViewPager 数据未在初始应用加载时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ViewPager 数据未在初始应用加载时加载


基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01