[英]AsyncTask is not functioning on my second fragment connected to Mysql database on swipe
[英]Fragment Swipe Asynctask Disappearance?
我有一個主要活動,下面有三個片段,可以左右滑動來切換。 沒什么特別的。 第一個和第三個(最后一個)片段都使用Asynctask從網上獲取一些信息。 我要在這里實現的是三個片段,這些片段可以從網上加載信息,以后再加載,我想添加下拉列表以進行刷新,但這應該不是問題。
這是發生了什么。
我相信這與Asynctask有關。 如果有更好的方法來設置我在此處的全部內容,請隨時進行設置。
主要
public class Main extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1, false);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
// Fragment fragment = new DummySectionFragment();
// Bundle args = new Bundle();
// args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
// fragment.setArguments(args);
// return fragment;
switch (position) {
case 0:
// Top Rated fragment activity
return new Post();
case 1:
// Games fragment activity
return new Feed();
case 2:
// Movies fragment activity
return new Explore();
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "Post";
case 1:
return "Feed";
case 2:
return "Explore";
}
return null;
}
}
}
第一片段
public class Post extends Fragment {
private MyAsyncTask mAuthTask = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.f_post, container, false);
Button addGoalButton = (Button) rootView.findViewById(R.id.submit);
addGoalButton.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
// Pass the fragmentView through to the handler
// so that findViewById can be used to get a handle on
// the fragments own views.
attemptLogin();
}
});
return rootView;
}
public void attemptLogin() {
if (mAuthTask != null) {
return;
}
mAuthTask = new MyAsyncTask();
mAuthTask.execute((Void) null);
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPostExecute(Void result) {
//mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
Toast.makeText(getActivity(),
"dont worry, be happy!", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
// my network operation
return null;
}
}
}
第三片段
public class Explore extends Fragment {
private MyAsyncTask mAuthTask = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.f_explore, container, false);
attemptLogin();
return rootView;
}
public void attemptLogin() {
if (mAuthTask != null) {
return;
}
mAuthTask = new MyAsyncTask();
mAuthTask.execute((Void) null);
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPostExecute(Void result) {
//mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
Toast.makeText(getActivity(),
"Loading Explore", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
// my network operation
return null;
}
}
}
我認為可能是幾個問題。
多個Asynctask不能同時運行,並且使用Viewpagers時,默認情況下會加載3個視圖。 我相信您需要致電:(任何您不希望為所有需要加載的視圖運行代碼的地方)
if(getUserVisibleHint()) { //logic in here attemptLogin(); }
我相信這將停止您的onCreateView()方法被調用3次。 之所以這么多,是因為視圖分頁器具有以下方法: mPager.setOffscreenPageLimit(1);
這樣做:
Set the number of pages that should be retained to either side of the current page
in the view hierarchy in an idle state.
默認情況下為1,這表示是否將片段加載到左側和右側以使滾動平滑。 (您不能將其更改為0)。 因此,getUserVisibleHint將檢查當前視圖是否為Suer可以看到的視圖,並僅在可見時調用您放入其中的任何內容。 (因為默認情況下它將調用您的asynctask 3次,並且它們不能同時運行。
我可能是錯的。
編輯#1:通過在onPostExecute中將asynctask設置回null來修復第一個片段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.