我的片段中有一个AsyncTask类,它必须在每个被调用的片段onActivityCreated
上执行一次。但是在onCreate
片段方法中,我有一条关于现在显示的页面数量的日志消息。但我无法理解为什么当我第一次打开Activity
时,此消息告诉我页面的数量是0和1。希望有人可以提供帮助。
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;
}
}
}
片段类
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消息:
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
答案 0 :(得分:3)
Viewpage预加载页面以实现无缝过渡。
设置应保留在页面两侧的页数 处于空闲状态的视图层次结构中的当前页面。页面超出此范围 在需要时,将从适配器重新创建限制。
这是作为优化提供的。如果您事先知道这个号码 您将需要支持或具有延迟加载机制的页面 放在你的页面上,调整这个设置可以带来好处 感知到的分页动画和交互的平滑性。如果你有 少量页面(3-4),您可以一次保持活动状态, 在新创建的视图子树的布局中花费的时间会减少 用户来回翻页。
您应该保持此限制,特别是如果您的网页很复杂 布局。 此设置默认为1 。
记下突出显示的内容。这就是你一次加载两个片段的原因。