private List<ViewHolder> findSwapTargets(ViewHolder viewHolder) { if (this.mSwapTargets == null) { this.mSwapTargets = new ArrayList(); this.mDistances = new ArrayList(); } else { this.mSwapTargets.clear(); this.mDistances.clear(); } int margin = this.mCallback.getBoundingBoxMargin(); int left = Math.round(this.mSelectedStartX + this.mDx) - margin; int top = Math.round(this.mSelectedStartY + this.mDy) - margin; int right = (viewHolder.itemView.getWidth() + left) + (margin * 2); int bottom = (viewHolder.itemView.getHeight() + top) + (margin * 2); int centerX = (left + right) / 2; int centerY = (top + bottom) / 2; LayoutManager lm = this.mRecyclerView.getLayoutManager(); int childCount = lm.getChildCount(); for (int i = 0; i < childCount; i++) { View other = lm.getChildAt(i); if (other != viewHolder.itemView && other.getBottom() >= top && other.getTop() <= bottom && other.getRight() >= left && other.getLeft() <= right) { ViewHolder otherVh = this.mRecyclerView.getChildViewHolder(other); if (this.mCallback.canDropOver(this.mRecyclerView, this.mSelected, otherVh)) { int dx = Math.abs(centerX - ((other.getLeft() + other.getRight()) / 2)); int dy = Math.abs(centerY - ((other.getTop() + other.getBottom()) / 2)); int dist = (dx * dx) + (dy * dy); int pos = 0; int cnt = this.mSwapTargets.size(); int j = 0; while (j < cnt && dist > ((Integer) this.mDistances.get(j)).intValue()) { pos++; j++; } this.mSwapTargets.add(pos, otherVh); this.mDistances.add(pos, Integer.valueOf(dist)); } } } return this.mSwapTargets; }
private boolean endChangeAnimationIfNecessary(ChangeInfo changeInfo, ViewHolder item) { boolean oldItem = false; if (changeInfo.newHolder == item) { changeInfo.newHolder = null; } else if (changeInfo.oldHolder != item) { return false; } else { changeInfo.oldHolder = null; oldItem = true; } ViewCompat.setAlpha(item.itemView, 1.0f); ViewCompat.setTranslationX(item.itemView, 0.0f); ViewCompat.setTranslationY(item.itemView, 0.0f); dispatchChangeFinished(item, oldItem); return true; }
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view; if (viewType == TYPE_ITEM) { if (mContext == null) { mContext = parent.getContext(); } view = LayoutInflater.from(mContext).inflate(R.layout.content_search_needs, parent, false); if (orderDao == null) orderDao = new OrderDao(mContext); final ItemViewHolder holder = new ItemViewHolder(view); return holder; } else if (viewType == TYPE_FOOTER) { view = LayoutInflater.from(mContext).inflate(R.layout.item_foot, parent, false); Log.e("!!!!!","111111111111"); return new FootViewHolder(view); } return null; }
@Override public boolean animateChange(ViewHolder oldHolder, ViewHolder newHolder, int fromX, int fromY, int toX, int toY) { final float prevTranslationX = ViewCompat.getTranslationX(oldHolder.itemView); final float prevTranslationY = ViewCompat.getTranslationY(oldHolder.itemView); final float prevAlpha = ViewCompat.getAlpha(oldHolder.itemView); endAnimation(oldHolder); int deltaX = (int) (toX - fromX - prevTranslationX); int deltaY = (int) (toY - fromY - prevTranslationY); // recover prev translation state after ending animation ViewCompat.setTranslationX(oldHolder.itemView, prevTranslationX); ViewCompat.setTranslationY(oldHolder.itemView, prevTranslationY); ViewCompat.setAlpha(oldHolder.itemView, prevAlpha); if (newHolder != null && newHolder.itemView != null) { // carry over translation values endAnimation(newHolder); ViewCompat.setTranslationX(newHolder.itemView, -deltaX); ViewCompat.setTranslationY(newHolder.itemView, -deltaY); ViewCompat.setAlpha(newHolder.itemView, 0); } mPendingChanges.add(new ChangeInfo(oldHolder, newHolder, fromX, fromY, toX, toY)); return true; }
private ItemHolderInfo popFromLayoutStep(ViewHolder vh, int flag) { ItemHolderInfo itemHolderInfo = null; int index = this.mLayoutHolderMap.indexOfKey(vh); if (index >= 0) { InfoRecord record = (InfoRecord) this.mLayoutHolderMap.valueAt(index); if (!(record == null || (record.flags & flag) == 0)) { record.flags &= flag ^ -1; if (flag == 4) { itemHolderInfo = record.preInfo; } else if (flag == 8) { itemHolderInfo = record.postInfo; } else { throw new IllegalArgumentException("Must provide flag PRE or POST"); } if ((record.flags & 12) == 0) { this.mLayoutHolderMap.removeAt(index); InfoRecord.recycle(record); } } } return itemHolderInfo; }
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = null; ViewHolder viewHolder = null; if (viewType == TYPE_COLOR) { v = LayoutInflater.from(parent.getContext()).inflate( R.layout.view_color_panel, parent,false); viewHolder = new ColorViewHolder(v); } else if (viewType == TYPE_MORE) { v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.view_color_more_panel,parent,false); viewHolder = new MoreViewHolder(v); } return viewHolder; }
@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == ITEM_TYPE_HEADER) { return new HeaderBottomHolder(recyclerViewHeader); } else if (viewType == ITEM_TYPE_CONTENT) { return adapter.onCreateViewHolder(parent, viewType); } else if (viewType == ITEM_TYPE_BOTTOM) { return new HeaderBottomHolder(recyclerViewFooter); } else if (viewType == ITEM_TYPE_HEADERVIEW) { return new HeaderBottomHolder(headerView); } else if (viewType == ITEM_TYPE_FOOTVIEW) { return new HeaderBottomHolder(footView); } return adapter.onCreateViewHolder(parent, viewType); }
void process(ProcessCallback callback) { for (int index = this.mLayoutHolderMap.size() - 1; index >= 0; index--) { ViewHolder viewHolder = (ViewHolder) this.mLayoutHolderMap.keyAt(index); InfoRecord record = (InfoRecord) this.mLayoutHolderMap.removeAt(index); if ((record.flags & 3) == 3) { callback.unused(viewHolder); } else if ((record.flags & 1) != 0) { if (record.preInfo == null) { callback.unused(viewHolder); } else { callback.processDisappeared(viewHolder, record.preInfo, record.postInfo); } } else if ((record.flags & 14) == 14) { callback.processAppeared(viewHolder, record.preInfo, record.postInfo); } else if ((record.flags & 12) == 12) { callback.processPersistent(viewHolder, record.preInfo, record.postInfo); } else if ((record.flags & 4) != 0) { callback.processDisappeared(viewHolder, record.preInfo, null); } else if ((record.flags & 8) != 0) { callback.processAppeared(viewHolder, record.preInfo, record.postInfo); } else if ((record.flags & 2) != 0) { } InfoRecord.recycle(record); } }
private boolean endChangeAnimationIfNecessary(ChangeInfo changeInfo, ViewHolder item) { boolean oldItem = false; if (changeInfo.newHolder == item) { changeInfo.newHolder = null; } else if (changeInfo.oldHolder == item) { changeInfo.oldHolder = null; oldItem = true; } else { return false; } ViewCompat.setAlpha(item.itemView, 1); ViewCompat.setTranslationX(item.itemView, 0); ViewCompat.setTranslationY(item.itemView, 0); dispatchChangeFinished(item, oldItem); return true; }
public void onLongPress(MotionEvent e) { View child = ItemTouchHelper.this.findChildView(e); if (child != null) { ViewHolder vh = ItemTouchHelper.this.mRecyclerView.getChildViewHolder(child); if (vh != null && ItemTouchHelper.this.mCallback.hasDragFlag(ItemTouchHelper.this.mRecyclerView, vh) && MotionEventCompat.getPointerId(e, 0) == ItemTouchHelper.this.mActivePointerId) { int index = MotionEventCompat.findPointerIndex(e, ItemTouchHelper.this.mActivePointerId); float x = MotionEventCompat.getX(e, index); float y = MotionEventCompat.getY(e, index); ItemTouchHelper.this.mInitialTouchX = x; ItemTouchHelper.this.mInitialTouchY = y; ItemTouchHelper itemTouchHelper = ItemTouchHelper.this; ItemTouchHelper.this.mDy = 0.0f; itemTouchHelper.mDx = 0.0f; if (ItemTouchHelper.this.mCallback.isLongPressDragEnabled()) { ItemTouchHelper.this.select(vh, 2); } } } }
@SuppressWarnings("unchecked") @Override public final void onViewRecycled(ViewHolder holder) { if (!(holder instanceof HeaderFooterViewHolder)) { onItemViewRecycled((VH)holder); } }
private ChangeInfo(ViewHolder oldHolder, ViewHolder newHolder, int fromX, int fromY, int toX, int toY) { this(oldHolder, newHolder); this.fromX = fromX; this.fromY = fromY; this.toX = toX; this.toY = toY; }
private void callBack(OnGridItemMoveListener listener, ViewHolder viewHolder, int fromPos, ViewHolder target, int toPos, boolean addFirst, boolean isLast) { if (isLast || addFirst) { listener.onItemMove(fromPos, toPos, addFirst, isLast); } else if (viewHolder.getItemViewType() == target.getItemViewType()) { listener.onItemMove(fromPos, toPos, false, false); } }
/** * If this method returns true, ItemTouchHelper assumes viewHolder has been * moved to the adapter position of target ViewHolder * @param recyclerView * @param viewHolder * @param target * @return */ @Override public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target) { int toPosition = target.getAdapterPosition(); mListener.onItemMove(viewHolder.getAdapterPosition(), toPosition); draggedTo = toPosition; return true; }
@Override /** Override prepHolderForAnimateRemove * * Called when an item is removed from the RecyclerView. Implementors can choose * whether and how to animate that change, but must always call * {@link #dispatchRemoveFinished(ViewHolder)} when done, either * immediately (if no animation will occur) or after the animation actually finishes. * The return value indicates whether an animation has been set up and whether the * ItemAnimators {@link #runPendingAnimations()} method should be called at the * next opportunity. This mechanism allows ItemAnimator to set up individual animations * as separate calls to {@link #animateAdd(H) animateAdd()}, * {@link #animateMove(ViewHolder, int, int, int, int) animateMove()}, and * {@link #animateRemove(ViewHolder) animateRemove()} come in one by one, then * start the animations together in the later call to {@link #runPendingAnimations()}. * * <p>This method may also be called for disappearing items which continue to exist in the * RecyclerView, but for which the system does not have enough information to animate * them out of view. In that case, the default animation for removing items is run * on those items as well.</p> * * @param holder The item that is being removed. * @return true if a later call to {@link #runPendingAnimations()} is requested, * false otherwise. */ public boolean animateRemove(final ViewHolder holder) { mPendingRemovals.add((H) holder); return prepHolderForAnimateRemove((H) holder); }
private void preAnimateAdd(final RecyclerView.ViewHolder holder) { clear(holder.itemView); if (holder instanceof AnimateViewHolder) { ((AnimateViewHolder) holder).preAnimateAddImpl(); } else { preAnimateAddImpl(holder); } }
@Override public final ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case HEADER_TYPE: return new HeaderFooterViewHolder(header); case FOOTER_TYPE: return new HeaderFooterViewHolder(footer); default: return onCreateItemViewHolder(parent, viewType); } }
@Override public boolean animateAdd(final ViewHolder holder) { resetAnimation(holder); ViewCompat.setAlpha(holder.itemView, 0); ViewCompat.setTranslationY(holder.itemView, holder.itemView.getHeight()); mPendingAdditions.add(holder); return true; }
public void startDrag(ViewHolder viewHolder) { if (!this.mCallback.hasDragFlag(this.mRecyclerView, viewHolder)) { Log.e(TAG, "Start drag has been called but swiping is not enabled"); } else if (viewHolder.itemView.getParent() != this.mRecyclerView) { Log.e(TAG, "Start drag has been called with a view holder which is not a child of the RecyclerView which is controlled by this ItemTouchHelper."); } else { obtainVelocityTracker(); this.mDy = 0.0f; this.mDx = 0.0f; select(viewHolder, 2); } }
private MoveInfo(ViewHolder holder, int fromX, int fromY, int toX, int toY) { this.holder = holder; this.fromX = fromX; this.fromY = fromY; this.toX = toX; this.toY = toY; }
private void preAnimateRemove(final ViewHolder holder) { ViewHelper.clear(holder.itemView); if (holder instanceof AnimateViewHolder) { ((AnimateViewHolder) holder).preAnimateRemoveImpl(); } else { preAnimateRemoveImpl(holder); } }
private void endChangeAnimation(List<ChangeInfo> infoList, ViewHolder item) { for (int i = infoList.size() - 1; i >= 0; i--) { ChangeInfo changeInfo = infoList.get(i); if (endChangeAnimationIfNecessary(changeInfo, item)) { if (changeInfo.oldHolder == null && changeInfo.newHolder == null) { infoList.remove(changeInfo); } } } }
private int endRecoverAnimation(ViewHolder viewHolder, boolean override) { for (int i = this.mRecoverAnimations.size() - 1; i >= 0; i--) { RecoverAnimation anim = (RecoverAnimation) this.mRecoverAnimations.get(i); if (anim.mViewHolder == viewHolder) { anim.mOverridden |= override; if (!anim.mEnded) { anim.cancel(); } this.mRecoverAnimations.remove(i); return anim.mAnimationType; } } return 0; }
private void runAddAnimation(boolean removalsPending, boolean changesPending, boolean movesPending, boolean additionsPending) { if (additionsPending) { final ArrayList<ViewHolder> additions = new ArrayList<>(); // Sorting addition animations based on it's original layout position Collections.sort(mPendingAdditions, new Comparator<ViewHolder>() { @Override public int compare(ViewHolder vh1, ViewHolder vh2) { return vh1.getLayoutPosition() - vh2.getLayoutPosition(); } }); additions.addAll(mPendingAdditions); mAdditionsList.add(additions); mPendingAdditions.clear(); Runnable adder = new Runnable() { public void run() { int index = 0; for (ViewHolder holder : additions) { doAnimateAdd(holder, index++); } additions.clear(); mAdditionsList.remove(additions); } }; if (removalsPending || movesPending || changesPending) { long removeDuration = removalsPending ? getRemoveDuration() : 0; long moveDuration = movesPending ? getMoveDuration() : 0; long changeDuration = changesPending ? getChangeDuration() : 0; long totalDelay = removeDuration + Math.max(moveDuration, changeDuration); View view = additions.get(0).itemView; ViewCompat.postOnAnimationDelayed(view, adder, totalDelay); } else { adder.run(); } } }
@Override public void onChildDrawOver(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); // L.e("call: onChildDrawOver([c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive])-> " + actionState + " " + isCurrentlyActive + " " + viewHolder, 300); if (drawOverInFullscreen && viewHolder.getAdapterPosition() >= 0) { //L.e("call: onChildDrawOver([c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive])-> dx:" + dX + " dy:" + dY + " " + actionState + " " + isCurrentlyActive); c.save(); c.getClipBounds(clipBounds); clipBounds.inset(-ScreenUtil.screenWidth, -ScreenUtil.screenHeight); c.clipRect(clipBounds, Region.Op.REPLACE); c.translate(viewHolder.itemView.getLeft() + dX, viewHolder.itemView.getTop() + dY); viewHolder.itemView.draw(c); c.restore(); } }
public void startSwipe(ViewHolder viewHolder) { if (!this.mCallback.hasSwipeFlag(this.mRecyclerView, viewHolder)) { Log.e(TAG, "Start swipe has been called but dragging is not enabled"); } else if (viewHolder.itemView.getParent() != this.mRecyclerView) { Log.e(TAG, "Start swipe has been called with a view holder which is not a child of the RecyclerView controlled by this ItemTouchHelper."); } else { obtainVelocityTracker(); this.mDy = 0.0f; this.mDx = 0.0f; select(viewHolder, 1); } }
private View nextViewFromScrapList() { int size = this.mScrapList.size(); for (int i = 0; i < size; i++) { View view = ((ViewHolder) this.mScrapList.get(i)).itemView; LayoutParams lp = (LayoutParams) view.getLayoutParams(); if (!lp.isItemRemoved() && this.mCurrentPosition == lp.getViewLayoutPosition()) { assignPositionFromScrapList(view); return view; } } return null; }
@Override public void onBindViewHolder(ViewHolder holder, int position) { if (holder != null) { baseHolder = (SuperItemHolder) holder; baseHolder.setPosition(position); baseHolder.setData(mDatas.get(position), countPosition(position)); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView giphyLogoView = (ImageView) findViewById(R.id.giphy_logo_view); GlideApp.with(this) .load(R.raw.large_giphy_logo) .into(giphyLogoView); RecyclerView gifList = (RecyclerView) findViewById(R.id.gif_list); LinearLayoutManager layoutManager = new LinearLayoutManager(this); gifList.setLayoutManager(layoutManager); RequestBuilder<Drawable> gifItemRequest = GlideApp.with(this) .asDrawable(); ViewPreloadSizeProvider<Api.GifResult> preloadSizeProvider = new ViewPreloadSizeProvider<>(); adapter = new GifAdapter(this, gifItemRequest, preloadSizeProvider); gifList.setAdapter(adapter); RecyclerViewPreloader<Api.GifResult> preloader = new RecyclerViewPreloader<>(GlideApp.with(this), adapter, preloadSizeProvider, 4); gifList.addOnScrollListener(preloader); gifList.setRecyclerListener(new RecyclerListener() { @Override public void onViewRecycled(ViewHolder holder) { // This is an optimization to reduce the memory usage of RecyclerView's recycled view pool // and good practice when using Glide with RecyclerView. GifViewHolder gifViewHolder = (GifViewHolder) holder; GlideApp.with(MainActivity.this).clear(gifViewHolder.gifView); } }); }
public void onChildViewDetachedFromWindow(View view) { removeChildDrawingOrderCallbackIfNecessary(view); ViewHolder holder = this.mRecyclerView.getChildViewHolder(view); if (holder != null) { if (this.mSelected == null || holder != this.mSelected) { endRecoverAnimation(holder, false); if (this.mPendingCleanup.remove(holder.itemView)) { this.mCallback.clearView(this.mRecyclerView, holder); return; } return; } select(null, 0); } }
/** * Returns another view holder that should be dismissed at the same time as the provided one. */ public ViewHolder getDismissSibling(ViewHolder viewHolder) { int swipePos = viewHolder.getAdapterPosition(); int siblingPosDelta = mRoot.getDismissSiblingPosDelta(swipePos); if (siblingPosDelta == 0) return null; return mRecyclerView.findViewHolderForAdapterPosition(siblingPosDelta + swipePos); }
@Override public void onBindViewHolder(ViewHolder holder, int position) { int type = getItemViewType(position); if(type == TYPE_COLOR){ onBindColorViewHolder((ColorViewHolder)holder,position); }else if(type == TYPE_MORE){ onBindColorMoreViewHolder((MoreViewHolder)holder,position); } }
protected ViewHolder getHeader(RecyclerView parent, StickyHeaderAdapter adapter, int position) { final long key = adapter.getHeaderId(position); if (headerCache.containsKey(key)) { return headerCache.get(key); } else { final ViewHolder holder = adapter.onCreateHeaderViewHolder(parent); final View header = holder.itemView; //noinspection unchecked adapter.onBindHeaderViewHolder(holder, position); int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY); int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED); int childWidth = ViewGroup.getChildMeasureSpec(widthSpec, parent.getPaddingLeft() + parent.getPaddingRight(), header.getLayoutParams().width); int childHeight = ViewGroup.getChildMeasureSpec(heightSpec, parent.getPaddingTop() + parent.getPaddingBottom(), header.getLayoutParams().height); header.measure(childWidth, childHeight); header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight()); headerCache.put(key, holder); return holder; } }
public boolean animateChange(@NonNull ViewHolder oldHolder, @NonNull ViewHolder newHolder, @NonNull ItemHolderInfo preInfo, @NonNull ItemHolderInfo postInfo) { int toLeft; int toTop; int fromLeft = preInfo.left; int fromTop = preInfo.top; if (newHolder.shouldIgnore()) { toLeft = preInfo.left; toTop = preInfo.top; } else { toLeft = postInfo.left; toTop = postInfo.top; } return animateChange(oldHolder, newHolder, fromLeft, fromTop, toLeft, toTop); }
@Override public void onBindViewHolder(ViewHolder holder, int position) { if (holder != null) { baseHolder = (SuperItemHolder) holder; baseHolder.setPosition(position); baseHolder.setData(mDatas.get(position)); } }
@Override public @NonNull ItemHolderInfo recordPreLayoutInformation(@NonNull State state, @NonNull ViewHolder viewHolder, @AdapterChanges int changeFlags, @NonNull List<Object> payloads) { final ItemHolderInfo itemHolderInfo = super.recordPreLayoutInformation(state, viewHolder, changeFlags, payloads); if (itemHolderInfo instanceof PayloadItemHolderInfo) { ((PayloadItemHolderInfo) itemHolderInfo).setPayloads(payloads); } return itemHolderInfo; }
public void onBindViewHolder(ViewHolder holder, int position) { PPModel model = (PPModel) this.mDatas.get(position); if (holder != null && model != null) { if (model.type() == ItemType.TYPE_HEADER) { onBindHeaderView(holder, position); } else if (model.type() == ItemType.TYPE_FOOTER) { onBindFooterView(holder, position); } else { onBindItemView(holder, position); } } }