ViewPager碎片创建的工作原理

时间:2013-09-22 10:21:50

标签: android android-fragments android-asynctask android-viewpager

我知道如何通过实现滑动视图布局来创建碎片(将其放入ViewPager中)。 每个片段覆盖

onCreateView()

从视图中检索窗口小部件的方法。但onCreateView()的执行事件是什么? 具体来说,我有一个应用程序,在我创建FragmentAdapter对象后调用AsyncTask。 AsyncTask用

开始执行
doInBackGround()

然后将调用片段的onCreateView(),并在

之后调用
onPostExecute()

被执行。 如果我在初始化FragmentAdapter之前调用AsyncTask,则会出现相同的行为。 所以,如果我启动一个服务而不是AsyncTask,onCreateView它没有被调用,应用程序崩溃。 所有这些行为背后的逻辑是什么?

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = new DatabaseHelper(this);
Intent intent = new Intent(this, DownloadIntent.class);
startService(intent);

// build blocks
pager = (ViewPager) this.findViewById(R.id.pager);
FragmentManager fm = this.getSupportFragmentManager();
adapter = new FragmentAdapter(fm);
// set properties
pager.setAdapter(adapter);
pager.setPageMargin(0);
pager.setOffscreenPageLimit(3);
// retrieve Fragment object
tableFragment = (TableFragment) adapter.getTableFragment();
tempFragment = (TempFragment) adapter.getTempFragment();
humFragment = (HumFragment) adapter.getHumFragment();
mixFragment = (MixFragment) adapter.getMixFragment();

// listener for show action bar
pager.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        int event = arg1.getAction();
        if (event == MotionEvent.ACTION_DOWN) {
            showActionBar();
            return true;
        }
        return false;
    }
});
stored_data = new ArrayList<String[]>();
}

public void saveDataToList(ArrayList<String[]> list) {
    stored_data = list;
// refresh fragments
tableFragment.refresh(list);
tempFragment.refresh(list);
humFragment.refresh(list);
mixFragment.refresh(list);
}

@Override
protected void onResume() {
while (true) {

    Cursor data = database.getData();
    if (data.getCount() > 0) {

        ArrayList<String[]> list = new ArrayList<String[]>();
        String[] to_add;

        if (data.moveToFirst()) {
            while (!data.isAfterLast()) {
                to_add = new String[3];
                for (int i = 0; i < 3; i++) {
                    to_add[i] = data.getString(i);
                }

                data.moveToNext();
                list.add(to_add);
            }
        }
        data.close();
        saveDataToList(list);
    }
    try {
        Log.i("AAAAAAA!", "main sleep");
        Thread.sleep(6000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

ant这是片段之一,另一个是类似的

public class TableFragment extends Fragment {
    MainActivity main;
    ListView list_view;
    ArrayAdapter<String> adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_layout, container, false);
        list_view = (ListView) v.findViewById(R.id.list_view);
        adapter = new ArrayAdapter<String>(v.getContext(),
             android.R.layout.simple_list_item_1);
        list_view.setAdapter(adapter);
    return v;
}

public void refresh(ArrayList<String[]> lista) {
    adapter.clear();
    for (int i = 0; i < lista.size(); i++) {
    adapter.add("T: " + lista.get(i)[0] + " H: " + lista.get(i)[1]
    + " D: " + lista.get(i)[2]);
    }
}
}

0 个答案:

没有答案