稍做记录
说明:
这是用来给自己回忆的,估计你们也看不太懂
有时间我再整理吧2018.7.15
28.May 明日之星——RecyclerView
学习内容
1、回收与复用View
2、setLayoutManager显示
3、ItemDecoration item如何分隔
4、ItemAnimator Item增加与删除动画效果
SimpleAdapter extends RecyclerView.Adapter<MyViewHolder(泛型)>{ inflater,context,data}
创建构造方法,context ,data,inflater
再写必要的方法
getItemCount(){
return data.getSize();
}
onBindViewHolder(holder , position){
holder.tv.setText(getData(position));
}
onCreateViewHolder(){
View view = mInflater.inflater(传入item布局R.layout.item,);
MyViewHolder viewHolder = new MyViewHolder(传一个View);
}
}
class MyViewHolder(与上面的一致) extends viewHolder
{
当前item所有控件textview
public MyViewHolder(View view){
super(view);
tv;
}
}
MainActivity
mAdapter = new SimpleAdapter(this,mDatas);
mR .setAdapter(mAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mR.setLayoutManager (linearLayoutManager);
变身GridView
menu增加若干项
mR.setLayoutManager(new GridLayoutManager(this,3))
增删动画
SimpleAdapter
addData(int position){
mDatas.add(position,“wenben”);
notifyItemInserted(position);
}
setItemAnimator(new DefaultItemAnimator());
OnClick
SimpleAdapter
接口
public interface OnItemClickListener
{
void onItemClick(View view , int Position);
void onItemLongClick(View view , int Position);
}
private OnItemClickListener mOnItemClickListener;
public setOnItemClickListener(OnItemClickListener listener){
this.mOnItemClickListener = listener;
}//供外部设置回调
onBindViewHolder{
if(mOnItemClickListener != null)
holder.itemView.setOnClickListener..
.
mOnItemClickListener.onItemClick(holder.itemView,position)
}
//监听回调
mAdapter.setOnItemClickListener
瀑布流
copy
动态控制高度
private List<Integer> mH;
Adapter
mH = new ArrayList<Integer>();
for(int i = 0;i<mDatas.size();i++)
{
mH.add((int)(100+Math.random()*300));
}
onBind
LayoutParams lp = holder.itemView.getLayoutParams();
lp.height = mH.get(position);
holder.itemView.setLayoutParams(lp);