- Activity/Fragment生命周期图
转自
https://www.cnblogs.com/purediy/p/3276545.html
-
Activity
oncreate()方法,来初始化子项(控件,数据)
onpause()方法,释放内存
ondestory() 方法 ,销毁前调用 -
Fragment
oncreateview()获取子项findbyid
onActivitycreate()初始化子项数据
ondestory()销毁frament调用 -
fragment 生命周期的利用
生命周期有 onAttach(),onCreate(),onCreateView(),onActivityCreated();此为创建时会执行的方法
onstart(),onResume(),是变得可见时会执行的方法
onPause(),onStop(),是进入后台会执行的方法。
销毁时会执行 onpause(),onStop(),onDestroyView(),Ondestroy();
我们经常将fragment放在activity中使用,数据是activity获得,而在fragment中展示,那么如何比较简单的实现,在fragment中展示activity的数据呢,
我们可以在fragment中添加list的set方法,在activity中new 了fragment后,调用set方法给list赋值,最后,重写fragment的onStart()方法,在此方法中展示list的数据
转自:https://blog.csdn.net/lxl403853563/article/details/49800231
- Activity生命周期详解
https://blog.csdn.net/xiajun2356033/article/details/78741121
RecyclerAdapter
介绍自学成果
目前是重写这几个方法
/**
* 可选重写
* 得到类型
* @param position
* @return
*/
@Override
public int getItemViewType(int position) {
currentType = position + 1;
if (mlist == null) {
currentType = 0;
}
Log.i(TAG, "position=" + position + " currentType=" + currentType);
return super.getItemViewType(position);
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Log.i(TAG,"position = "+position);
if (currentType == 0) {
NullHolder nullHolder = (NullHolder) holder;
//设置数据 Banner 的数据
nullHolder.setData();
} else {
ViewHolder viewHolder = (ViewHolder) holder;
//设置数据
viewHolder.setData(position);
}
}
public int getItemCount() {
if (mlist != null) {
return mlist.size();
} else {
return 1;
}
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i(TAG,"viewType = "+viewType);
if (currentType != 0) {
if (currentType == 1) {
parent.removeAllViews();
}
return new ViewHolder(mLayoutInflater.inflate(R.layout.recycler_orderdetails, null), mContext, mlist);
} else {
parent.removeAllViews();
return new NullHolder(mLayoutInflater.inflate(R.layout.recycler_orderdetails, null), mContext);
}
}
日志如下
11-06 08:08:27.051 6331-6426/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: 构造布局Had
11-06 08:08:27.051 6331-6426/com.yangs.industrialinternetofthings I/OrderFragment: 设置适配器!
11-06 08:08:27.098 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:27.098 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:27.101 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:27.101 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=0 currentType=1
11-06 08:08:27.101 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: viewType = 0
11-06 08:08:27.194 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position = 0
11-06 08:08:29.968 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:29.968 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=1 currentType=2
11-06 08:08:29.968 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: viewType = 0
11-06 08:08:29.980 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position = 1
11-06 08:08:31.236 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.237 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.237 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=0 currentType=1
11-06 08:08:31.238 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.238 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=1 currentType=2
11-06 08:08:31.238 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.239 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.239 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=0 currentType=1
11-06 08:08:31.239 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.239 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=1 currentType=2
11-06 08:08:31.240 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.240 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.241 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=0 currentType=1
11-06 08:08:31.241 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.241 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=1 currentType=2
11-06 08:08:31.244 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.244 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.244 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=0 currentType=1
11-06 08:08:31.245 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: getItemCount()
11-06 08:08:31.245 6331-6331/com.yangs.industrialinternetofthings I/OrderRecyclerAdapter: position=1 currentType=2
执行顺序
1.创建所有子项视图时顺序
getItemCount=>getItemViewType=>onCreateViewHolder=>onBindViewHolder
getItemCount=>getItemViewType=>onCreateViewHolder=>onBindViewHolder
2.所有子项视图执行完毕后顺序
getItemCount=>getItemViewType
getItemCount=>getItemViewType
对控件进行操作主要是依靠
onBindViewHolder(RecyclerView.ViewHolder holder, int position)
一个position对应一个holder(内部类视图)
根据position找到对应的视图,进行设置。
但是弄不清为什么onCreateViewHolder(ViewGroup parent, int viewType)的viewType一直是0是什么意思