Java Code Examples for android.support.v7.widget.RecyclerView#findViewHolderForLayoutPosition()
The following examples show how to use
android.support.v7.widget.RecyclerView#findViewHolderForLayoutPosition() .
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: CommonUtils.java From WanAndroid with MIT License | 5 votes |
/** * 设置Navigation menu item 高度 */ private void resetItemLayout(NavigationView navigationView) { //通过反射拿到menu的item布局。修改布局参数 try { Field mPresenter = NavigationView.class.getDeclaredField("mPresenter"); mPresenter.setAccessible(true); //此处mPresenter.get(navigationView)会得到一个NavigationMenuPresenter对象,但是该类是@hide的。所以此处直接再拿其内部的NavigationMenuView。该类也是@hide的。需要注意的是,该类继承自RecyclerView。菜单的布局也就是由其完成的。 Field mMenuView = mPresenter.get(navigationView).getClass().getDeclaredField("mMenuView"); mMenuView.setAccessible(true); //由于NavigationMenuView是隐藏类。此处用其父类。 RecyclerView recycler = (RecyclerView) mMenuView.get(mPresenter.get(navigationView)); for (int i = 0; i < recycler.getAdapter().getItemCount(); i++) { RecyclerView.ViewHolder holder = recycler.findViewHolderForLayoutPosition(i); //这里看实际项目了。我的项目中添加了一个head。 // if (i == 0 || holder == null)//因为这里有一个header。所以要先排除第一个 // continue; if (holder == null) return; //剩下的就是修改整体布局参数了。 ViewGroup.LayoutParams params = holder.itemView.getLayoutParams(); params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = 10; holder.itemView.setLayoutParams(params); } } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } }
Example 2
Source File: BaseQuickAdapter.java From imsdk-android with MIT License | 5 votes |
@Nullable public View getViewByPosition(RecyclerView recyclerView, int position, @IdRes int viewId) { if (recyclerView == null) { return null; } BaseViewHolder viewHolder = (BaseViewHolder) recyclerView.findViewHolderForLayoutPosition(position); if (viewHolder == null) { return null; } return viewHolder.getView(viewId); }
Example 3
Source File: BaseQuickAdapter.java From demo4Fish with MIT License | 5 votes |
public View getViewByPosition(RecyclerView recyclerView, int position, int viewId) { if (recyclerView == null) { return null; } BaseViewHolder viewHolder = (BaseViewHolder) recyclerView.findViewHolderForLayoutPosition(position); return viewHolder.getView(viewId); }
Example 4
Source File: BaseQuickAdapter.java From JD-Test with Apache License 2.0 | 5 votes |
public View getViewByPosition(RecyclerView recyclerView, int position, int viewId) { if (recyclerView == null) { return null; } BaseViewHolder viewHolder = (BaseViewHolder) recyclerView.findViewHolderForLayoutPosition(position); return viewHolder.getView(viewId); }
Example 5
Source File: RecyclerItemViewAssertion.java From mangosta-android with Apache License 2.0 | 5 votes |
@Override public final void check(View view, NoMatchingViewException e) { RecyclerView recyclerView = (RecyclerView) view; RecyclerView.ViewHolder viewHolderForPosition = recyclerView.findViewHolderForLayoutPosition(position); if (viewHolderForPosition == null) { throw (new PerformException.Builder()) .withActionDescription(toString()) .withViewDescription(HumanReadables.describe(view)) .withCause(new IllegalStateException("No view holder at position: " + position)) .build(); } else { View viewAtPosition = viewHolderForPosition.itemView; itemViewAssertion.check(item, viewAtPosition, e); } }
Example 6
Source File: RecyclerItemViewAssertion.java From KataSuperHeroesAndroid with Apache License 2.0 | 5 votes |
@Override public final void check(View view, NoMatchingViewException e) { RecyclerView recyclerView = (RecyclerView) view; RecyclerView.ViewHolder viewHolderForPosition = recyclerView.findViewHolderForLayoutPosition(position); if (viewHolderForPosition == null) { throw (new PerformException.Builder()).withActionDescription(toString()) .withViewDescription(HumanReadables.describe(view)) .withCause(new IllegalStateException("No view holder at position: " + position)) .build(); } else { View viewAtPosition = viewHolderForPosition.itemView; itemViewAssertion.check(item, viewAtPosition, e); } }
Example 7
Source File: RecyclerItemViewAssertion.java From Game-of-Thrones with Apache License 2.0 | 5 votes |
@Override public final void check(View view, NoMatchingViewException e) { RecyclerView recyclerView = (RecyclerView) view; RecyclerView.ViewHolder viewHolderForPosition = recyclerView.findViewHolderForLayoutPosition(position); if (viewHolderForPosition == null) { throw (new PerformException.Builder()).withActionDescription(toString()) .withViewDescription(HumanReadables.describe(view)) .withCause(new IllegalStateException("No view holder at position: " + position)) .build(); } else { View viewAtPosition = viewHolderForPosition.itemView; itemViewAssertion.check(item, viewAtPosition, e); } }
Example 8
Source File: SuspensionDecoration.java From Focus with GNU General Public License v3.0 | 4 votes |
@Override public void onDrawOver(Canvas c, final RecyclerView parent, RecyclerView.State state) {//最后调用 绘制在最上层 int pos = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition(); pos -= getHeaderViewCount(); //pos为1,size为1,1>0? true if (mDatas == null || mDatas.isEmpty() || pos > mDatas.size() - 1 || pos < 0 || !mDatas.get(pos).isShowSuspension()) { return;//越界 } String tag = mDatas.get(pos).getSuspensionTag(); //View child = parent.getChildAt(pos); View child = parent.findViewHolderForLayoutPosition(pos + getHeaderViewCount()).itemView;//出现一个奇怪的bug,有时候child为空,所以将 child = parent.getChildAt(i)。-》 parent.findViewHolderForLayoutPosition(pos).itemView boolean flag = false;//定义一个flag,Canvas是否位移过的标志 if ((pos + 1) < mDatas.size()) {//防止数组越界(一般情况不会出现) if (null != tag && !tag.equals(mDatas.get(pos + 1).getSuspensionTag())) {//当前第一个可见的Item的tag,不等于其后一个item的tag,说明悬浮的View要切换了 Log.d("zxt", "onDrawOver() called with: c = [" + child.getTop());//当getTop开始变负,它的绝对值,是第一个可见的Item移出屏幕的距离, if (child.getHeight() + child.getTop() < mTitleHeight) {//当第一个可见的item在屏幕中还剩的高度小于title区域的高度时,我们也该开始做悬浮Title的“交换动画” c.save();//每次绘制前 保存当前Canvas状态, flag = true; //一种头部折叠起来的视效,个人觉得也还不错~ //可与123行 c.drawRect 比较,只有bottom参数不一样,由于 child.getHeight() + child.getTop() < mTitleHeight,所以绘制区域是在不断的减小,有种折叠起来的感觉 /* c.clipRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + child.getHeight() + child.getTop());*/ //类似饿了么点餐时,商品列表的悬停头部切换“动画效果” //上滑时,将canvas上移 (y为负数) ,所以后面canvas 画出来的Rect和Text都上移了,有种切换的“动画”感觉 c.translate(0, child.getHeight() + child.getTop() - mTitleHeight); } } } mPaint.setColor(COLOR_TITLE_BG); c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + mTitleHeight, mPaint); mPaint.setColor(COLOR_TITLE_FONT); mPaint.getTextBounds(tag, 0, tag.length(), mBounds); c.drawText(tag, child.getPaddingLeft() + 30, parent.getPaddingTop() + mTitleHeight - (mTitleHeight / 2 - mBounds.height() / 2), mPaint);//滚动的过程中绘制title if (flag) c.restore();//恢复画布到之前保存的状态 }
Example 9
Source File: SuspensionDecoration.java From Focus with GNU General Public License v3.0 | 4 votes |
@Override public void onDrawOver(Canvas c, final RecyclerView parent, RecyclerView.State state) {//最后调用 绘制在最上层 int pos = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition(); pos -= getHeaderViewCount(); //pos为1,size为1,1>0? true if (mDatas == null || mDatas.isEmpty() || pos > mDatas.size() - 1 || pos < 0 || !mDatas.get(pos).isShowSuspension()) { return;//越界 } String tag = mDatas.get(pos).getSuspensionTag(); //View child = parent.getChildAt(pos); View child = parent.findViewHolderForLayoutPosition(pos + getHeaderViewCount()).itemView;//出现一个奇怪的bug,有时候child为空,所以将 child = parent.getChildAt(i)。-》 parent.findViewHolderForLayoutPosition(pos).itemView boolean flag = false;//定义一个flag,Canvas是否位移过的标志 if ((pos + 1) < mDatas.size()) {//防止数组越界(一般情况不会出现) if (null != tag && !tag.equals(mDatas.get(pos + 1).getSuspensionTag())) {//当前第一个可见的Item的tag,不等于其后一个item的tag,说明悬浮的View要切换了 Log.d("zxt", "onDrawOver() called with: c = [" + child.getTop());//当getTop开始变负,它的绝对值,是第一个可见的Item移出屏幕的距离, if (child.getHeight() + child.getTop() < mTitleHeight) {//当第一个可见的item在屏幕中还剩的高度小于title区域的高度时,我们也该开始做悬浮Title的“交换动画” c.save();//每次绘制前 保存当前Canvas状态, flag = true; //一种头部折叠起来的视效,个人觉得也还不错~ //可与123行 c.drawRect 比较,只有bottom参数不一样,由于 child.getHeight() + child.getTop() < mTitleHeight,所以绘制区域是在不断的减小,有种折叠起来的感觉 /* c.clipRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + child.getHeight() + child.getTop());*/ //类似饿了么点餐时,商品列表的悬停头部切换“动画效果” //上滑时,将canvas上移 (y为负数) ,所以后面canvas 画出来的Rect和Text都上移了,有种切换的“动画”感觉 c.translate(0, child.getHeight() + child.getTop() - mTitleHeight); } } } mPaint.setColor(COLOR_TITLE_BG); c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(), parent.getPaddingTop() + mTitleHeight, mPaint); mPaint.setColor(COLOR_TITLE_FONT); mPaint.getTextBounds(tag, 0, tag.length(), mBounds); c.drawText(tag, child.getPaddingLeft() + 30, parent.getPaddingTop() + mTitleHeight - (mTitleHeight / 2 - mBounds.height() / 2), mPaint);//滚动的过程中绘制title if (flag) c.restore();//恢复画布到之前保存的状态 }
Example 10
Source File: ItemHeaderDecoration.java From AndroidSamples with Apache License 2.0 | 4 votes |
/** * 可以绘制在内容的上面,覆盖内容 * * @param c * @param parent * @param state */ @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onDrawOver(Canvas c, final RecyclerView parent, RecyclerView.State state) { // 当前屏幕的第一个item int pos = ((LinearLayoutManager) (parent.getLayoutManager())).findFirstVisibleItemPosition(); String tag = mDatas.get(pos).getTag(); Log.e(TAG, "onDrawOver: pos:" + pos + "\ttag:" + tag); View child = parent.findViewHolderForLayoutPosition(pos).itemView; // 当发现下一个title顶上来的时候,将canvas向上平移,产生一种向上挤压的动画效果 boolean flag = false; if ((pos + 1) < mDatas.size()) { String suspensionTag = mDatas.get(pos + 1).getTag(); if (null != tag && !tag.equals(suspensionTag)) { if (child.getHeight() + child.getTop() < mTitleHeight) { c.save(); flag = true; c.translate(0, child.getHeight() + child.getTop() - mTitleHeight); } } } // 绘制一个与列表中标题一样的东西(吸顶效果) View topTitleView = mInflater.inflate(R.layout.item_title, parent, false); TextView tvTitle = (TextView) topTitleView.findViewById(R.id.tv_title); tvTitle.setText("测试数据" + tag); int toDrawWidthSpec;//用于测量的widthMeasureSpec int toDrawHeightSpec;//用于测量的heightMeasureSpec RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) topTitleView.getLayoutParams(); if (lp == null) { lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//这里是根据复杂布局layout的width height,new一个Lp // topTitleView.setLayoutParams(lp); } topTitleView.setLayoutParams(lp); if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) { //如果是MATCH_PARENT,则用父控件能分配的最大宽度和EXACTLY构建MeasureSpec。 toDrawWidthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight(), View.MeasureSpec.EXACTLY); } else if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) { //如果是WRAP_CONTENT,则用父控件能分配的最大宽度和AT_MOST构建MeasureSpec。 toDrawWidthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight(), View.MeasureSpec.AT_MOST); } else { //否则则是具体的宽度数值,则用这个宽度和EXACTLY构建MeasureSpec。 toDrawWidthSpec = View.MeasureSpec.makeMeasureSpec(lp.width, View.MeasureSpec.EXACTLY); } //高度同理 if (lp.height == ViewGroup.LayoutParams.MATCH_PARENT) { toDrawHeightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom(), View.MeasureSpec.EXACTLY); } else if (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) { toDrawHeightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom(), View.MeasureSpec.AT_MOST); } else { toDrawHeightSpec = View.MeasureSpec.makeMeasureSpec(mTitleHeight, View.MeasureSpec.EXACTLY); } //依次调用 measure,layout,draw方法,将复杂头部显示在屏幕上。 topTitleView.measure(toDrawWidthSpec, toDrawHeightSpec); topTitleView.layout(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getPaddingLeft() + topTitleView.getMeasuredWidth(), parent.getPaddingTop() + topTitleView.getMeasuredHeight()); topTitleView.draw(c);//Canvas默认在视图顶部,无需平移,直接绘制 if (flag) c.restore();//恢复画布到之前保存的状态 if (!TextUtils.equals(tag, currentTag)) { currentTag = tag; Integer integer = Integer.valueOf(currentTag); Log.e(TAG, "onDrawOver: " + integer); mCheckListener.check(integer); } }