学习笔记,整理中;
面:
思路:
复用:从集合中去取
复用:从集合中去获取
入口:滑动 Move 事件 --> scrollByInternal --> scrollStep --> mLayout.scrollVerticallyBy
--> scrollBy --> fill --> layoutChunk 布局块--> layoutState.next 布局状态--> addView(view);
layoutState.next --> getViewForPosition --> tryGetViewHolderForPositionByDeadline 截止日期 -->
怎么从集合中去获取:tryGetViewHolderForPositionByDeadline,分几种情况去获取ViewHolder
1. getChangedScrapViewForPosition -- mChangeScrap 与动画相关
2. getScrapOrHiddenOrCachedHolderForPosition -- mAttachedScrap附属废料 、mCachedViews 缓存
3. getScrapOrCachedViewForId -- mAttachedScrap 、mCachedViews (ViewType,itemid)
4. mViewCacheExtension.getViewForPositionAndType -- 自定义缓存 -- (使用情况:局部刷新??)
5. getRecycledViewPool().getRecycledView -- 从缓冲池里面获取
RecycledViewPool -- 缓存池
ViewHolder -- 包装View的 --- ItemView
当没有缓存的时候??--- mAdapter.createViewHolder --》 onCreateViewHolder
多级缓存的目的 -- 为了性能
创建ViewHolder 后 绑定: tryBindViewHolderByDeadline--》 mAdapter.bindViewHolder--》onBindViewHolder
回收(缓存)机制:看这一个情况--- ViewHolder
LinearLayoutManager.onLayoutChildren --> detachAndScrapAttachedViews 分离--> scrapOrRecycleView
--> 1.recycler.recycleViewHolderInternal(viewHolder); -- 处理 CacheView 、RecyclerViewPool 的缓存
--> 1.ViewHodler改变 不会进来 -- 先判断mCachedViews的大小
--> mCachedViews.size 大于默认大小 --- recycleCachedViewAt
--- >addViewHolderToRecycledViewPool --- 缓存池里面的数据都是从mCachedViews里面出来的
--> 2.addViewHolderToRecycledViewPool --> getRecycledViewPool().putRecycledView(holder);
--> scrap.resetInternal(); ViewHolder 清空
--> 2.recycler.scrapView(view);
mCachedViews当前的大小 如果 大于等于mViewCacheMax(默认的CachedViews的大小)
ViewType --
缓存池 里面保存 只是 ViewHolder 类型 没有数据
去查找缓存和复用的一种情况
入口:复用:RecyclerView.onLayout --> dispatchLayout 派遣--》 dispatchLayoutStep2 --》 onLayoutChildren --》 fill
缓存:fill -->recycleByLayoutState-->recycleViewsFromStart --> recycleChildren
--> removeAndRecycleViewAt --> recycler.recycleView
--> recycler.recycleViewHolderInternal(viewHolder); -- 处理 CacheView 、RecyclerViewPool 的缓存
要学会copy系统的代码 --- 怎么优化
layoutChunk --> layoutDecoratedWithMargins
notifyDataSetChanged-->mObservable.notifyChanged
--> (RecyclerViewDataObserver)mObservers.get(i).onChanged --> requestLayout
mChangeScrap与 mAttachedScrap
用来缓存还在屏幕内的 ViewHolder
2.mCachedViews
用来缓存移除屏幕之外的 ViewHolder
3.mViewCacheExtension
开发给用户的自定义扩展缓存,需要用户自己管理 View 的创建和缓存
4.RecycledViewPool
ViewHolder 缓存池
—————————-
自定义layoutmanage 继承recycleview.layout 可以实现布局改变 在界面上拖动或者拖拽,settranslationy拖动 setscrolx缩放
SlideCallback extends ItemTouchHelper.SimpleCallback
SlideCardLayoutManagerextends RecyclerView.LayoutManager
------------------------------------------------------------------------
public class MainActivityextends AppCompatActivity {
private RecyclerViewrv;
private UniversalAdapteradapter;
private ListmDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = findViewById(R.id.rv);
rv.setLayoutManager(new SlideCardLayoutManager());
mDatas = SlideCardBean.initDatas();
adapter =new UniversalAdapter(this, mDatas, R.layout.item_swipe_card) {
@Override
public void convert(ViewHolder viewHolder, SlideCardBean slideCardBean) {
viewHolder.setText(R.id.tvName, slideCardBean.getName());
viewHolder.setText(R.id.tvPrecent, slideCardBean.getPostition() +"/" +mDatas.size());
Glide.with(MainActivity.this)
.load(slideCardBean.getUrl())
.into((ImageView) viewHolder.getView(R.id.iv));
}
};
rv.setAdapter(adapter);
// 初始化数据
CardConfig.initConfig(this);
SlideCallback slideCallback =new SlideCallback(rv, adapter, mDatas);
ItemTouchHelper itemTouchHelper =new ItemTouchHelper(slideCallback);
itemTouchHelper.attachToRecyclerView(rv);
}
}
------------------------------------------------------------------------
public class SlideCardLayoutManagerextends RecyclerView.LayoutManager {
@Override
public RecyclerView.LayoutParamsgenerateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
// 布局
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
// ViewHodler回收复用
detachAndScrapAttachedViews(recycler);
int bottomPosition;
int itemCount = getItemCount();
if (itemCount < CardConfig.MAX_SHOW_COUNT) {
bottomPosition =0;
}else {
// 布局了四张卡片 --- 4,5,6,7
bottomPosition = itemCount - CardConfig.MAX_SHOW_COUNT;
}
for (int i = bottomPosition; i < itemCount; i++) {
// 复用
View view = recycler.getViewForPosition(i);
addView(view);
measureChildWithMargins(view, 0, 0);
int widthSpace = getWidth() - getDecoratedMeasuredWidth(view);
int heightSpace = getHeight() - getDecoratedMeasuredHeight(view);
// 布局 ---draw -- onDraw ,onDrawOver, onLayout
layoutDecoratedWithMargins(view, widthSpace /2, heightSpace /2,
widthSpace /2 + getDecoratedMeasuredWidth(view),
heightSpace /2 + getDecoratedMeasuredHeight(view));
int level = itemCount - i -1;
if (level >0) {
if (level < CardConfig.MAX_SHOW_COUNT -1) {
view.setTranslationY(CardConfig.TRANS_Y_GAP * level);
view.setScaleX(1 - CardConfig.SCALE_GAP * level);
view.setScaleY(1 - CardConfig.SCALE_GAP * level);
}else {
// 最下面的那个view 与前一个view 布局一样
view.setTranslationY(CardConfig.TRANS_Y_GAP * (level -1));
view.setScaleX(1 - CardConfig.SCALE_GAP * (level -1));
view.setScaleY(1 - CardConfig.SCALE_GAP * (level -1));
}
}
}
}
}
------------------------------------------------------------------------
public class SlideCallbackextends ItemTouchHelper.SimpleCallback {
private RecyclerViewmRv;
private UniversalAdapteradapter;
private ListmDatas;
public SlideCallback(RecyclerView mRv,
UniversalAdapter adapter, List mDatas) {
super(0, 15);
this.mRv = mRv;
this.adapter = adapter;
this.mDatas = mDatas;
}
// drag 拖拽
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
// 滑动
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
SlideCardBean remove =mDatas.remove(viewHolder.getLayoutPosition());
mDatas.add(0, remove);
adapter.notifyDataSetChanged();// onMeasure, onlayout
}
// onDra
@Override
public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
double maxDistance = recyclerView.getWidth() *0.5f;
double distance = Math.sqrt(dX * dX + dY * dY);
double fraction = distance / maxDistance;
if (fraction >1) {
fraction =1;
}
// 显示的个数 4个
int itemCount = recyclerView.getChildCount();
for (int i =0; i < itemCount; i++) {
View view = recyclerView.getChildAt(i);
int level = itemCount - i -1;
if (level >0) {
if (level < CardConfig.MAX_SHOW_COUNT -1) {
view.setTranslationY((float) (CardConfig.TRANS_Y_GAP * level - fraction * CardConfig.TRANS_Y_GAP));
view.setScaleX((float) (1 - CardConfig.SCALE_GAP * level + fraction * CardConfig.SCALE_GAP));
view.setScaleY((float) (1 - CardConfig.SCALE_GAP * level + fraction * CardConfig.SCALE_GAP));
}
}
}
}
@Override
public long getAnimationDuration(@NonNull RecyclerView recyclerView, int animationType, float animateDx, float animateDy) {
return 1500;
}
}
------------------------------------------------------------------------
------------------------------------------------------------------------