@Override public void onItemClicked(ItemAdapter itemAdapter, RssItem rssItem) { int positionToExpand = -1; int positionToContract = -1; if (itemAdapter.getExpandedItem() != null) { positionToContract = currentItems.indexOf(itemAdapter.getExpandedItem()); View viewToContract = recyclerView.getLayoutManager().findViewByPosition(positionToContract); if (viewToContract == null) { positionToContract = -1; } } if (itemAdapter.getExpandedItem() != rssItem) { positionToExpand = currentItems.indexOf(rssItem); itemAdapter.setExpandedItem(rssItem); } else { itemAdapter.setExpandedItem(null); } if (positionToContract > -1) { itemAdapter.notifyItemChanged(positionToContract); } if (positionToExpand > -1) { itemAdapter.notifyItemChanged(positionToExpand); delegate.get().onItemExpanded(this, itemAdapter.getExpandedItem()); } else { delegate.get().onItemContracted(this, rssItem); return; } View viewToExpand = recyclerView.getLayoutManager().findViewByPosition(positionToExpand); int lessToScroll = 0; if (positionToContract > -1 && positionToContract < positionToExpand) { lessToScroll = itemAdapter.getExpandedItemHeight() - itemAdapter.getCollapsedItemHeight(); } recyclerView.smoothScrollBy(0, viewToExpand.getTop() - lessToScroll); }
@Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager(); if (mCenterPivot == 0) { mCenterPivot = lm.getOrientation() == LinearLayoutManager.HORIZONTAL ? (recyclerView.getLeft() + recyclerView.getRight()) : (recyclerView.getTop() + recyclerView.getBottom()); } if (!mAutoSet) { if (newState == RecyclerView.SCROLL_STATE_IDLE) { View view = findCenterView(lm); int position = recyclerView.getChildPosition(view); Log.i("CenterLockListener", "list-position" + position); Toast.makeText(context, "list-position" + position, Toast.LENGTH_SHORT).show(); int viewCenter = lm.getOrientation() == LinearLayoutManager.HORIZONTAL ? (view.getLeft() + view.getRight()) / 2 : (view.getTop() + view.getBottom()) / 2; int scrollNeeded = viewCenter - mCenterPivot; if (lm.getOrientation() == LinearLayoutManager.HORIZONTAL) { recyclerView.smoothScrollBy(scrollNeeded, 0); } else { recyclerView.smoothScrollBy(0, (int) (scrollNeeded)); } mAutoSet = true; } } if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) { mAutoSet = false; } }
private void resetOverScroll() { int scrollBy = overScrollDistance; if (scrollBy > 0) { scrollBy = Math.max(scrollBy - overScrollTopDistanceLock, 0); } recyclerView.smoothScrollBy(0, scrollBy); startOverScrollTime = 0; }
private void appendComment(BotAdapter.Type type, String comment) { botAdapter.addItem(type, comment); botAdapter.setAnimationsLocked(false); botAdapter.setDelayEnterAnimation(false); if (rvComments.getChildCount() > 0) { rvComments.smoothScrollBy( 0, rvComments.getChildAt(0).getHeight() * botAdapter.getItemCount()); } }
// 添加MsgBean列表 public void addMgList(List<MsgBean> newMsgList) { if (newMsgList == null) { Timber.e("没有更多的聊天记录了"); return; } for (int i = 0; i < newMsgList.size(); i++) { msgList.add(0, newMsgList.get(i)); } Timber.e("增加了: \t" + newMsgList.size()); notifyDataSetChanged(); mRecyclerView.smoothScrollBy(0, msgList.size() * 200); }
@Override public void smoothScrollBy(int dx, int dy) { super.smoothScrollBy(dx, dy); getViewTreeObserver() .addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ViewHolder rightHolder = findViewHolderForAdapterPosition(mCenterHolderPosition - 1); if (rightHolder != null) { if (Build.VERSION.SDK_INT < 16) { getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { getViewTreeObserver().removeOnGlobalLayoutListener(this); } onRightItemSnap(mCenterHolderPosition - 1, rightHolder); } } }); getViewTreeObserver() .addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ViewHolder leftHolder = findViewHolderForAdapterPosition(mCenterHolderPosition + 1); if (leftHolder != null) { if (Build.VERSION.SDK_INT < 16) { getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { getViewTreeObserver().removeOnGlobalLayoutListener(this); } onLeftItemSnap(mCenterHolderPosition + 1, leftHolder); } } }); }
@Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { if (v.isInTouchMode()) { v.performClick(); } int[] location = new int[2]; v.getLocationOnScreen(location); /* * if (location[0] - v.getWidth() < mRecyclerView.getLeft() ) { * mRecyclerView.smoothScrollBy(- 400, 0); } */ if (location[0] + (v.getWidth() * 1.3) > mRecyclerView.getRight()) { mRecyclerView.smoothScrollBy(300, 0); } if (v.getId() == R.id.more_game) { panel.getMore_game_shadow().setImageResource(R.drawable.white_focus); panel.getMore_game_content().setScaleX(1.1f); panel.getMore_game_content().setScaleY(1.1f); panel.getMore_game().setNextFocusUpId(R.id.tab_gamecenter); panel.getMore_game().setNextFocusDownId(panel.getMore_game().getId()); if (adInfo != null) { if (adInfo.getVideoUrl() != null && !adInfo.getVideoUrl().isEmpty()) { panel.getMore_game_video().setVisibility(View.VISIBLE); } } } } else { if (v.getId() == R.id.more_game) { panel.getMore_game_shadow().setImageResource(android.R.color.transparent); panel.getMore_game_content().setScaleX(1.0f); panel.getMore_game_content().setScaleY(1.0f); panel.getMore_game_video().setVisibility(View.INVISIBLE); } } }
@Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); if (mCenterPivot == 0) { mCenterPivot = recyclerView.getLeft() + recyclerView.getRight(); } if (!mAutoSet) { synchronized (this) { if (newState == RecyclerView.SCROLL_STATE_IDLE) { View view = linearLayoutManager.findViewByPosition(mPosition); mPosition = INITIAL_POSITION; if (view == null) { view = findCenterView(linearLayoutManager); } int viewCenter = (view.getLeft() + view.getRight()) / 2; int viewWidth = view.getRight() - view.getLeft(); int scrollNeeded = viewCenter - mCenterPivot; if (BuildConfig.DEBUG) Log.d( TAG, "viewCentre = " + viewCenter + " viewWidth = " + viewWidth + " scrollNeeded = " + scrollNeeded + " recyclerViewWidth = " + recyclerView.getMeasuredWidth()); recyclerView.smoothScrollBy(scrollNeeded, 0); mCenterHolderPosition = recyclerView.getChildLayoutPosition(view); if (BuildConfig.DEBUG) Log.d( TAG, "centerHolderPosition = " + mCenterHolderPosition + " mPosition = " + mPosition); if (mScrollType == null) { mScrollType = SwipeType.SWIPE; } onCenterItemSnap( mCenterHolderPosition, recyclerView.getChildViewHolder(view), mScrollType); mScrollType = null; mAutoSet = true; } } } if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) { mAutoSet = false; } }
// 增加消息 public void addMsg(MsgBean msgBean) { msgList.add(msgBean); notifyDataSetChanged(); mRecyclerView.smoothScrollBy(0, msgList.size() * 200); }