Java Code Examples for android.support.v7.widget.RecyclerView#getChildViewHolder()

The following examples show how to use android.support.v7.widget.RecyclerView#getChildViewHolder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StickHeaderDecoration.java    From ItemDecorationDemo with Apache License 2.0 6 votes vote down vote up
/**
 * 下一个StickHeader的ViewHolder
 */
private RecyclerView.ViewHolder nextStickHolder(RecyclerView parent, int currPos) {
    int childCount = parent.getChildCount();
    if (childCount == 1) {
        return null;
    }
    for (int i = 1; i < childCount; i++) {
        //从RecyclerView第二个Child开始找
        View child = parent.getChildAt(i);
        int childPos = parent.getChildAdapterPosition(child);
        if (mProvider.isStick(childPos) && childPos > currPos) {
            return parent.getChildViewHolder(child);
        }
    }
    return null;
}
 
Example 2
Source File: StickyHeadersItemDecoration.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

    ensureHeaderLaidOut();

    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams)view.getLayoutParams();
    RecyclerView.ViewHolder holder = parent.getChildViewHolder(view);

    if (overlay || !isHeader(holder)) {
        outRect.set(0, 0, 0, 0);
    }
    else {
        //TODO: Handle layout direction
        outRect.set(0, headerHeight, 0, 0);
    }

    if (lp.isItemRemoved()) {
        headers.remove(holder.getItemId());
    }
}
 
Example 3
Source File: InsetDividerDecoration.java    From Interessant with Apache License 2.0 6 votes vote down vote up
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    int childCount = parent.getChildCount();
    if (childCount < 2) return;

    RecyclerView.LayoutManager lm = parent.getLayoutManager();
    float[] lines = new float[childCount * 4];
    boolean hasDividers = false;

    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);

        if (viewHolder.getClass() == dividedClass) {
            lines[i * 4] = inset + lm.getDecoratedLeft(child);
            lines[(i * 4) + 2] = lm.getDecoratedRight(child);
            int y = lm.getDecoratedBottom(child) + (int) child.getTranslationY() - height;
            lines[(i * 4) + 1] = y;
            lines[(i * 4) + 3] = y;
            hasDividers = true;
        }
    }
    if (hasDividers) {
        canvas.drawLines(lines, paint);
    }
}
 
Example 4
Source File: FocusResizeScrollListener.java    From FocusResize with Apache License 2.0 6 votes vote down vote up
private void forceScrollItem(RecyclerView recyclerView, View view, int j, int positionScrolled) {
    if (!(recyclerView.getChildViewHolder(view) instanceof FocusResizeAdapter.FooterViewHolder)) {
        if (j == positionScrolled) {
            view.getLayoutParams().height = heightExpandedItem;
            adapter.onItemBigResizeScrolled(recyclerView.getChildViewHolder(view), itemToResize, dyAbs);
        } else {
            if (mLinearLayoutManager.findFirstCompletelyVisibleItemPosition()
                    == mLinearLayoutManager.getItemCount() - 1
                    || mLinearLayoutManager.findFirstCompletelyVisibleItemPosition() == -1) {
                view.getLayoutParams().height = heightExpandedItem;
                adapter.onItemBigResizeScrolled(recyclerView.getChildViewHolder(view), itemToResize, dyAbs);
            } else {
                view.getLayoutParams().height = heightCollapsedItem;
                adapter.onItemSmallResizeScrolled(recyclerView.getChildViewHolder(view), itemToResize, dyAbs);
            }
        }
    }
}
 
Example 5
Source File: StickyHeadersItemDecoration.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

    ensureHeaderLaidOut();

    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams)view.getLayoutParams();
    RecyclerView.ViewHolder holder = parent.getChildViewHolder(view);

    if (overlay || !isHeader(holder)) {
        outRect.set(0, 0, 0, 0);
    }
    else {
        //TODO: Handle layout direction
        outRect.set(0, headerHeight, 0, 0);
    }

    if (lp.isItemRemoved()) {
        headers.remove(holder.getItemId());
    }
}
 
Example 6
Source File: ExpandableAdapter.java    From ExpandableRecyclerView with Apache License 2.0 6 votes vote down vote up
private RecyclerView findParent(BaseViewHolder holder) {
    RecyclerView parent = null;
    if (holder.associateRv != null) {
        parent = holder.associateRv.get();
    }
    if (parent != null) {
        return parent;
    } else {
        for (RecyclerView rv : mAttachedRecyclerViews) {
            RecyclerView.ViewHolder vh = rv.getChildViewHolder(holder.itemView);
            if (holder == vh) {
                return rv;
            }
        }
    }
    throw new RuntimeException("can not find parent for ViewHolder:" + holder);
}
 
Example 7
Source File: MaterialViewPagerHeaderDecorator.java    From MaterialViewPager with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) {
    final RecyclerView.ViewHolder holder = recyclerView.getChildViewHolder(view);
    final Context context = recyclerView.getContext();

    if(!registered) {
        MaterialViewPagerHelper.registerRecyclerView(context, recyclerView);
        registered = true;
    }

    int headerCells = 1;

    //don't work with stagged
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if(layoutManager instanceof GridLayoutManager){
        GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
        headerCells = gridLayoutManager.getSpanCount();
    }

    MaterialViewPagerAnimator animator = MaterialViewPagerHelper.getAnimator(context);
    if (animator != null) {
        if (holder.getAdapterPosition() < headerCells) {
            outRect.top = Math.round(Utils.dpToPx(animator.getHeaderHeight() + 10, context));
        }
    }
}
 
Example 8
Source File: DefaultAdapter.java    From Aurora with Apache License 2.0 5 votes vote down vote up
/**
 * 遍历所有{@link BaseHolder},释放他们需要释放的资源
 *
 * @param recyclerView
 */
public static void releaseAllHolder(RecyclerView recyclerView) {
    if (recyclerView == null) return;
    for (int i = recyclerView.getChildCount() - 1; i >= 0; i--) {
        final View view = recyclerView.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view);
        if (viewHolder != null && viewHolder instanceof BaseHolder) {
            ((BaseHolder) viewHolder).onRelease();
        }
    }
}
 
Example 9
Source File: StickyHeadersItemDecoration.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {


    final int childCount = parent.getChildCount();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();
    View header = headerViewHolder.itemView;
    Float lastY = null;

    for (int i = childCount - 1; i >= 0; i--) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams)child.getLayoutParams();
        final int position = parent.getChildPosition(child);
        RecyclerView.ViewHolder holder = parent.getChildViewHolder(child);

        if (!lp.isItemRemoved()) {

            float translationY = ViewCompat.getTranslationY(child);

            if (i == 0 || isHeader(holder)) {

                float y = getHeaderY(child, lm) + translationY;

                if (lastY != null && lastY < y + headerHeight) {
                    y = lastY - headerHeight;
                }

                adapter.onBindViewHolder(headerViewHolder, position);

                c.save();
                c.translate(0, y);
                header.draw(c);
                c.restore();

                lastY = y;
            }
        }
    }
}
 
Example 10
Source File: StickyHeadersItemDecoration.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {


    final int childCount = parent.getChildCount();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();
    View header = headerViewHolder.itemView;
    Float lastY = null;

    for (int i = childCount - 1; i >= 0; i--) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams)child.getLayoutParams();
        final int position = parent.getChildPosition(child);
        RecyclerView.ViewHolder holder = parent.getChildViewHolder(child);

        if (!lp.isItemRemoved()) {

            float translationY = ViewCompat.getTranslationY(child);

            if (i == 0 || isHeader(holder)) {

                float y = getHeaderY(child, lm) + translationY;

                if (lastY != null && lastY < y + headerHeight) {
                    y = lastY - headerHeight;
                }

                adapter.onBindViewHolder(headerViewHolder, position);

                c.save();
                c.translate(0, y);
                header.draw(c);
                c.restore();

                lastY = y;
            }
        }
    }
}
 
Example 11
Source File: GridItemDividerDecoration.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.isAnimating()) return;

    final int childCount = parent.getChildCount();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);

        if (requiresDivider(viewHolder)) {
            final int right = lm.getDecoratedRight(child);
            final int bottom = lm.getDecoratedBottom(child);
            // draw the bottom divider
            canvas.drawRect(lm.getDecoratedLeft(child),
                    bottom - dividerSize,
                    right,
                    bottom,
                    paint);
            // draw the right edge divider
            canvas.drawRect(right - dividerSize,
                    lm.getDecoratedTop(child),
                    right,
                    bottom - dividerSize,
                    paint);
        }
    }
}
 
Example 12
Source File: InsetDividerDecoration.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    int childCount = parent.getChildCount();
    if (childCount < 2) return;

    RecyclerView.LayoutManager lm = parent.getLayoutManager();
    float[] lines = new float[childCount * 4];
    boolean hasDividers = false;

    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);

        if (viewHolder.getClass() == dividedClass) {
            // skip if this *or next* view is activated
            if (child.isActivated()
                 || (i + 1 < childCount && parent.getChildAt(i + 1).isActivated())) {
                continue;
            }
            lines[i * 4] = inset + lm.getDecoratedLeft(child);
            lines[(i * 4) + 2] = lm.getDecoratedRight(child);
            int y = lm.getDecoratedBottom(child) + (int) child.getTranslationY() - height;
            lines[(i * 4) + 1] = y;
            lines[(i * 4) + 3] = y;
            hasDividers = true;
        }
    }
    if (hasDividers) {
        canvas.drawLines(lines, paint);
    }
}
 
Example 13
Source File: TeamTagTest.java    From talk-android with MIT License 5 votes vote down vote up
@UiThreadTest
public void testUpdateTag() {
    TeamTagActivity activity = getActivity();
    mPresenter = new TeamTagPresenter(activity);
    mPresenter.attachModule(new TestTagModel());
    mPresenter.updateTag("", "");
    RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.team_tag_recycler);
    assertNotNull(recyclerView);
    LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
    TextView textView = (TextView) recyclerView.getChildViewHolder(manager.getChildAt(0)).itemView;
    assertEquals(textView.getText().toString(), "teambition");
}
 
Example 14
Source File: HTItemDecoration.java    From ht-refreshrecyclerview with MIT License 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    final RecyclerView.ViewHolder childViewHolder = parent.getChildViewHolder(view);
    if (childViewHolder instanceof HTWrapperAdapter.InnerViewHolder) {
        outRect.set(0, 0, 0, 0);//如果是加载更多的ViewHolder,不进行装饰
        return;
    }
    if (mInnerItemDecoration != null) {
        mInnerItemDecoration.getItemOffsets(outRect, view, parent, state);
    }
}
 
Example 15
Source File: FocusResizeScrollListener.java    From FocusResize with Apache License 2.0 5 votes vote down vote up
private void calculateScrolledPosition(int totalItemCount, RecyclerView recyclerView) {
    for (int j = 0; j < totalItemCount - 1; j++) {
        View view = recyclerView.getChildAt(j);
        if (view != null) {
            if (!(recyclerView.getChildViewHolder(view) instanceof FocusResizeAdapter.FooterViewHolder)) {
                if (j == itemToResize) {
                    onItemBigResize(view, recyclerView);
                } else {
                    onItemSmallResize(view, recyclerView);
                }
                view.requestLayout();
            }
        }
    }
}
 
Example 16
Source File: InsetDividerDecoration.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
@Override public void onDrawOver(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
    int childCount = parent.getChildCount();
    if (childCount < 2) return;
    RecyclerView.LayoutManager lm = parent.getLayoutManager();
    float[] lines = new float[childCount * 4];
    boolean hasDividers = false;
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);
        RecyclerView.ViewHolder viewHolder = parent.getChildViewHolder(child);
        if (!(viewHolder instanceof ProgressBarViewHolder)) {
            boolean canDivide = toDivide == null || viewHolder.getClass() == toDivide;
            if (canDivide) {
                int position = parent.getChildAdapterPosition(child);
                if (child.isActivated() || (i + 1 < childCount && parent.getChildAt(i + 1).isActivated())) {
                    continue;
                }
                if (position != (state.getItemCount() - 1)) {
                    lines[i * 4] = inset == 0 ? inset : inset + lm.getDecoratedLeft(child);
                    lines[(i * 4) + 2] = lm.getDecoratedRight(child);
                    int y = lm.getDecoratedBottom(child) + (int) child.getTranslationY() - height;
                    lines[(i * 4) + 1] = y;
                    lines[(i * 4) + 3] = y;
                    hasDividers = true;
                }
            }
        }
    }
    if (hasDividers) {
        canvas.drawLines(lines, paint);
    }
}
 
Example 17
Source File: ItemOffsetDecoration.java    From RecyclerViewTools with Apache License 2.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

   WrapAdapter wrapAdapter = null;
   RecyclerView.Adapter adapter = parent.getAdapter();

   int adapterCount = adapter.getItemCount();

   if (adapter instanceof WrapAdapter) {
      wrapAdapter = (WrapAdapter) adapter;
      adapter = wrapAdapter.getWrapped();
   }

   // make exception for wrapAdapter special view types
   if (wrapAdapter != null) {
      RecyclerView.ViewHolder vh = parent.getChildViewHolder(view);

      // if it's a special view
      if (vh.getItemViewType() > MAIN_VIEW_TYPE_MASK) {
         if (disableSpecialViewsSpacing) {
            outRect.set(0, 0, 0, 0);
         } else {
            outRect.set(externalOffset);
         }
         return;
      }
   }

   int numHeaders = (wrapAdapter == null) ? 0 : wrapAdapter.getHeaderCount();
   int adapterPosition = parent.getChildAdapterPosition(view);
   int offsetAdapterPosition = adapterPosition - numHeaders;

   int spanCount = 1;
   RecyclerView.LayoutManager lm = parent.getLayoutManager();
   if (lm instanceof GridLayoutManager) {
      spanCount = ((GridLayoutManager) lm).getSpanCount();
   } else if (lm instanceof StaggeredGridLayoutManager) {
      spanCount = ((StaggeredGridLayoutManager) lm).getSpanCount();
   }

   final boolean isTop = offsetAdapterPosition < spanCount;
   final boolean isBottom = adapterCount - adapterPosition < spanCount;

   boolean isLeft;
   boolean isRight;

   ViewGroup.LayoutParams lp = view.getLayoutParams();

   // Grid
   if (lp instanceof GridLayoutManager.LayoutParams) {
      GridLayoutManager.LayoutParams glp = (GridLayoutManager.LayoutParams) lp;

      isLeft = glp.getSpanIndex() == 0;
      isRight = glp.getSpanIndex() + glp.getSpanSize() == spanCount;

   }

   // StaggeredGrid
   else if (lp instanceof StaggeredGridLayoutManager.LayoutParams) {
      StaggeredGridLayoutManager.LayoutParams sglp = (StaggeredGridLayoutManager.LayoutParams) lp;

      isLeft = sglp.getSpanIndex() == 0;
      isRight = sglp.getSpanIndex() == spanCount - 1;

   }

   // Assume Linear
   else {

      int itemColumn = offsetAdapterPosition % spanCount;
      isLeft = itemColumn == 0;
      isRight = itemColumn == spanCount - 1;

   }

   outRect.set(internalOffset);

   if (isTop) {
      outRect.top = externalOffset.top;
   }
   if (isBottom) {
      outRect.bottom = externalOffset.bottom;
   }
   if (isLeft) {
      outRect.left = externalOffset.left;
   }
   if (isRight) {
      outRect.right = externalOffset.right;
   }
}
 
Example 18
Source File: DividerDecoration.java    From android-periodic-table with GNU General Public License v3.0 4 votes vote down vote up
private boolean isDecorated(View view, RecyclerView parent) {
    RecyclerView.ViewHolder holder = parent.getChildViewHolder(view);

    return !(holder instanceof PropertiesAdapter.ViewHolder &&
            holder.getItemViewType() == PropertiesAdapter.VIEW_TYPE_HEADER);
}
 
Example 19
Source File: DividerDecoration.java    From android-cache-cleaner with MIT License 4 votes vote down vote up
private boolean isDecorated(View view, RecyclerView parent) {
    RecyclerView.ViewHolder holder = parent.getChildViewHolder(view);

    return !(holder instanceof AppsListAdapter.HeaderViewHolder &&
            holder.getItemViewType() == AppsListAdapter.VIEW_TYPE_HEADER);
}