Reputation: 1509
I have an AsyncTask class in my fragment and it must execute only once on each fragment onActivityCreated
called. But in the onCreate
fragment method I have a log message about number of what page is showing now. But I can't understand why this message is telling me that number of page is 0 and 1 instantly when I open Activity
at first time. Hope somebody could help.
public class ScheduleActivity extends ActionBarActivity {
static final String TAG = "myLogs";
static final int PAGE_COUNT = 9;
ViewPager pager;
PagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule_view);
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.drawable.new_nav_bg));
background.setTileModeX(android.graphics.Shader.TileMode.REPEAT);
actionBar.setBackgroundDrawable(background);
pager = (ViewPager) findViewById(R.id.pagerS);
// pager.setOffscreenPageLimit(2);
pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
Log.w(TAG, "onPageSelected, position = " + position);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ScheduleFragment.newInstance(position);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
}
}
Fragment class
public class ScheduleFragment extends Fragment {
static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";
int pageNumber;
private ArrayList<PlistItem> schedList = null;
private ListView listSched = null;
public CustomSheduleItem adapter;
int date;
static ScheduleFragment newInstance(int page) {
ScheduleFragment ScheduleFragment = new ScheduleFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
ScheduleFragment.setArguments(arguments);
return ScheduleFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);
Log.w("page number", "" + pageNumber);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
view = inflater.inflate(R.layout.schedule_fragment, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView schedDate = (TextView) getView().findViewById(R.id.dateSched);
date = 23 + pageNumber;
String sDate = Integer.toString(date);
schedDate.setText(" " + sDate);
String jsonString = getResources().getString(R.string.scheduleJson);
new DownloadFilesTask().execute(jsonString);
}
public void updateList() {
listSched = (ListView) getView().findViewById(R.id.listSched);
adapter = new CustomSheduleItem(getActivity().getApplicationContext(), schedList);
listSched.setAdapter(adapter);
}
private class DownloadFilesTask extends AsyncTask<String, Integer, Void> {
@Override
protected void onProgressUpdate(Integer... values) {
}
@Override
protected void onPostExecute(Void result) {
if (null != schedList) {
updateList();
}
}
@Override
protected Void doInBackground(String... params) {
String jsonString = params[0];
// JSONObject json = getJSONFromUrl(url);
InputStream is = null;
JSONObject jObj = null;
String json = null;
is = getResources().openRawResource(R.raw.shedule);
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e1) {
e1.printStackTrace();
}
try {
is.close();
} catch (IOException e1) {
e1.printStackTrace();
}
json = sb.toString();
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
parseJson(jObj);
return null;
}
}
public void parseJson(JSONObject jObj) {
try {
// if (json.getString("status").equalsIgnoreCase("ok")) {
JSONArray schedule = jObj.getJSONArray(""+date+"");
Log.w("SCHEDULE", "" + schedule);
schedList = new ArrayList<PlistItem>();
for (int i = 0; i < schedule.length(); i++) {
Log.w("LENGTH", "" + schedule.length());
JSONObject day = (JSONObject) schedule.getJSONObject(i);
Log.w("DAY", "" + day);
for (int j=1; j<day.length()+1;j++) {
JSONObject row = (JSONObject)day.getJSONObject(""+j+"");
Log.w("ROW",""+row);
PlistItem item = new PlistItem();
item.setPlace(row.getString("place"));
item.setTimeOne(row.getString("time1"));
item.setTimeTwo(row.getString("time2"));
item.setText(row.getString("text"));
schedList.add(item);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
LogCat messages:
08-15 18:44:36.716 5697-5697/com.project1.2014 W/page number﹕ 0
08-15 18:44:36.720 5697-5697/com.project1.2014 W/page number﹕ 1
Upvotes: 1
Views: 1830
Reputation: 3771
Viewpagers preload pages to allow for that seamless transition.
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.
You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.
Take note of the highlighted content. This would be the reason you are getting two fragments loaded at once.
Upvotes: 3