- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我今天有一个很奇怪的问题:我有一个水平显示一些缩略图的 RecyclerView,我使用 smoothScrollToPosition
导航到我需要的项目,但我注意到一个问题:它不会滚动到最后一项。进一步调试后,我发现我的 recyclerview 的子项比它显示的要少!我看到五个项目,一次显示 3 个。但是 child 计数(通过 recyclerView.getChildCount
获得)返回 4 而不是 5,我无法通过 getChildAt(4)
获得最后一个 child 。更奇怪的是,adapter.getItemCount()
返回 5,而 recyclerView.getAdapter().getItemCount()
返回 5...
检查 layoutAdapter
后,recyclerView.getLayoutManager().getItemCount()
返回 5,`recyclerView..getLayoutManager().getChildCount() 返回 4.. .
我需要两件事:首先,我需要我的 recyclerView 滚动到最后一项,其次:我需要能够获取所有子项,因为我对它们做了一些修改,因为用户与项目互动。
部分代码:
// Scrolling and editing the item inside the recyclerView
// It doesn't scroll to position 4, even having a item there...
mLayoutManager.smoothScrollToPosition(recyclerView, null, position);
// the code bellow returns null if position = 4, even if the recyclerView adapter has a item at index 4, and it is visible...
ImageView iv = (ImageView) recyclerView.getChildAt(position);
if (iv != null) {
iv.setPadding(10, 10, 10, 10);
iv.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
ImageView ivo = (ImageView) recyclerView.getChildAt(position - 1);
if (position - 1 == 0) {
ivo.setPadding(10, 10, 10, 10);
} else {
ivo.setPadding(0, 10, 10, 10);
}
ivo.setBackgroundColor(getResources().getColor(R.color.transparent));
}
smoothScrollToPosition 实现:
public class SnappingLinearLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 150f;
public SnappingLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext()){
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return new PointF(0, 1);
}
//This returns the milliseconds it takes to scroll one pixel.
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private class TopSnappedSmoothScroller extends LinearSmoothScroller {
public TopSnappedSmoothScroller(Context context) {
super(context);
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SnappingLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected int getVerticalSnapPreference() {
return SNAP_TO_ANY;
}
}
}
最佳答案
1) 要滚动到列表中的某个位置,您可以使用 scrollToPosition(position)
.您传入的位置将是最后一项的索引,如下所示:
recyclerView.smoothScrollToPosition(adapter.getItemCount())
我能够使用 v7 RecyclerView 和 android 的 LinearLayoutManager 使用这种方法。
构建.gradle
...
dependencies {
...
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
...
}
RecyclerView布局代码:
<android.support.v7.widget.RecyclerView android:id="@+id/item_list"
android:name="fbalashov.spikeapp.ItemListFragment"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager" />
然后当我想滚动到列表的顶部时,我只需调用我的 scrollToBottom 方法:
private void scrollToBottom() {
recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount());
}
这会导致 RecyclerView 平滑地滚动到列表中的最后一项。
2) 正如 Rafal 所说,RecyclerView(和 ListView)仅绘制屏幕上可见的项目 + 顶部和底部的一个或两个缓冲区以使滚动看起来不错。当您滚动时,它将“回收” View 并使用来自您的适配器的数据填充它们。因此,滚动到顶部的 View 将在列表底部重新使用,数据来自下一个项目。
因此,您无法同时获取适配器中项目的所有 View 。但是,您可以获取适配器中的所有项目,修改它们,然后触发适配器使用 adapter.notifyDataSetChanged() 重绘 recyclerView 中的 View 。 .示例:
// getItems() is a method you will have to add to you adapter to get the items from your
// adapter. Otherwise you can add a method in your adapter to update the items.
List<DataType> items = adapter.getItems();
for (DataType item : items) {
// make some change to each item
}
// this will force the recycler view to redraw its views
adapter.notifyDataSetChanged();
您还可以通过更新适配器中的项目并使用 notifyItemChanged(position) 对适配器中的项目进行更有针对性的更改。 .
关于android - RecyclerView Children 计数小于 children 数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536755/
假设我想在存储在变量中的特定表中导航。 我想在表的 thead 中找到 tr 中的所有输入。 如果我有表的 ID,我会这样做 - $('#mytable thead tr input') 但是假设我已
我是 React 新手,我正在尝试了解一般语法的工作原理,但不确定在这里提问是否合适。以下是我的一个简单标题组件的代码。 export default class Title extends Comp
我正在尝试实现在树结构中获取 child 的 child 的可能性。 这是我想要的例子。 到目前为止我做了什么。 class Children(list): def __init__(self
我一直在尝试让它工作一段时间,但不确定如何执行以下操作。我的表单组件有包含常规 html 标记和输入的子组件。如果 child 是输入,我想添加 attachToForm 和 detachFromFo
在本文档中 https://flutter.dev/docs/development/ui/layout#nesting-rows-and-columns 他们使用 children : 但是在 An
即使我只向表中添加一项内容,我似乎也无法弄清楚为什么会显示重复的子项添加错误。 有 3 个主要类: WindowTease:加载舞台并调用 loadTable() 方法 InventoryContro
我一直在查看以前提出的问题,但似乎找不到适合我的情况的解决方案... 我希望能够循环遍历所有 child 和 child 的 child 等等...... 设计中的标记看起来与此类似
这个问题在这里已经有了答案: How can I use jQuery methods on elements accessed by brackets notation? (2 个答案) 关闭 5
我有一个场景,我知道我正在寻找的 div 恰好是两层深。 使用效率是否更高: $('#mydiv').find('.myselector') 或 $('#mydiv').children().chil
我今天有一个很奇怪的问题:我有一个水平显示一些缩略图的 RecyclerView,我使用 smoothScrollToPosition 导航到我需要的项目,但我注意到一个问题:它不会滚动到最后一项。进
只有 JS,没有 Jquery。 如何获取容器的所有子节点,然后为每个子节点获取子节点? 我不想将 ID 添加到作为网格行子级的每个包装器,我尝试使用“this”或此索引进行定位。该脚本应该是动态的,
任务:在“注释”类的任何段落内的有序列表中强调强调的文本。这是我目前所拥有的... .note>p>ol>em{text-decoration:underline;} 我正在努力让它继续下去。我的代码
我有两个类如下: --- CSS: .shw-intro { width: 250px; height: 150px; background: rgb(95, 190, 0);
当前的 Firebase 数据库结构: 我要实现的目标: 在我所有位置的任何“促销”子项中搜索等于某物的优惠券。 如果找到,将其“limit”参数减少 -1 来更新它 我当前的代码: mDat
我有一个 html 表,想要获取所有行。但是,在 DebugElement 或 NativeElement 上调用 .children 会返回不同的顺序。 我的 table : 1
我在使用 Element.children 时注意到一个奇怪的问题并且似乎没有找到好的解决方法。 示例 1(预期行为) 拿这个 HTML: 还有这个 JS const formElem
目标是选择任何后代 - 无论直系后代指示如何 - 但不选择他们的 child 。换句话说,如果我从文档中搜索,我希望找到目标选择器未包含的所有子元素: test t
当我尝试执行 ParentNode.children 时,显示无法读取未定义的属性“children”。尝试让父节点的所有子节点的背景颜色为粉红色。 现在,我有一个以半小时为增量的表格,根据上午 8
我不确定这是否可行,但这是我想在 Reactjs 中实现的目标: 所以我想做的是,我想使用 React.cloneElement API 将 Comp1 组件的 pro
更新:我在另一台安装更干净的机器上试过这个。我无法在那台机器上重现这个。如果我发现是什么有问题的 (VSStudio) 组件导致了这种情况,我会告诉你的。 我从后面的代码创建了一些 UIElement
我是一名优秀的程序员,十分优秀!