0

我会通过实现滑动视图布局来了解片段创建(用于将其放在 ViewPager 中)的工作原理。每个片段覆盖

onCreateView()

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

doInBackGround()

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

onPostExecute()

被执行。如果我在初始化 FragmentAdapter 之前调用 AsyncTask,也会发生相同的行为。因此,如果我启动服务而不是 AsyncTask,则不会调用 onCreateView 并且应用程序崩溃。所有这些行为背后的逻辑是什么?

主要活动

@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();
    }
}
}

蚂蚁这是一个片段,另一个是相似的

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]);
    }
}
}
4

0 回答 0