android Fragment 返回自定义返回结果为空

1111115454 发布于 2015/12/30 23:38
阅读 444
收藏 0



import com.redchildrenmall.base.LoadingPager.LoadedResult;
import com.redchildrenmall.utils.UIUtils;


import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;


import android.view.ViewGroup;


/**
 * BaseFragment
 * 
 * 
 * 
 */
public abstract class BaseFragment extends Fragment {
private LoadingPager mLoadingPager;


@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Log.e("time", "onAttach执行");
}


public LoadingPager getLoadingPager() {
return mLoadingPager;
}


@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}


/**
* 创建该Fragment的视图
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mLoadingPager == null) {
Log.e("onCreateView", "onCreateView执行");
mLoadingPager = new LoadingPager(UIUtils.getContext()) {


@Override
public View initSuccessViews() {
Log.e("error", "initSuccessViews");
View initSuccessViews = BaseFragment.this
.initSuccessViews();
BaseFragment.this.initData();
BaseFragment.this.initListener();
return initSuccessViews;
}


@Override
public LoadedResult initData() {
Log.e("error", "loadData");
return BaseFragment.this.loadData();
}
};


} else {
((ViewGroup) mLoadingPager.getParent()).removeView(mLoadingPager);
Log.e("error", "removeViewmLoadingPager");
}
return mLoadingPager;
}


/**
* 当Activity的onCreate方法返回时调用
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}


/**
* 加载布局

* @return
*/
public abstract View initSuccessViews();


/**
* 加载数据

* @return
*/
public void initData() {


}


/**
* 网络加载数据

* @return
*/
public abstract LoadedResult loadData();


/**
* 加载事件
*/
public void initListener() {
// TODO Auto-generated method stub


}


}



调用者

OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {


@Override
public void onPageSelected(int position) {
BaseFragment fragment = FragmentFactory.getFragment(position);
if (fragment != null) {

Log.e("position",“position =”+ position + "");

LoadingPager loadingPager = fragment.getLoadingPager(); ----- 此时为空

Log.e("loadingPager", "loadingPager"loadingPager );
if (loadingPager != null) {
loadingPager.loadData();
}
}


}


@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub


}


@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub


}
};
if (onPageChangeListener != null) {
onPageChangeListener.onPageSelected(0);
}
this.rvpMain.setOnPageChangeListener(onPageChangeListener);

我打印日志看了执行顺序的

结果为

position=0

loadingPager

onAttach执行

onCreateView执行

请问各位大神,我怎么在出事loadingPager在onCreateView方法执行后.




加载中
0
pointerException
pointerException
从Fragment的生命周期来看的话,你要在onCreateView方法之后去初始化的话,你可以写在onActivityCreated方法中
返回顶部
顶部