java - 如何实现recyclerview的水平自动滚动
问题描述
我正在尝试在 android 中实现自定义线性布局管理器。用于获得水平自动滑动回收器视图。但是当我尝试将我的自定义类调用到我的主 java 类中时,我遇到了一些问题。
下面列出了我的代码面临的问题。
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
我得到的错误是:从未使用构造函数'CustomLinearLayoutManager(android.content.Context,int,boolean)'
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
我得到的错误是:Constructor 'CustomLinearLayoutManager(android.content.Context, android.util.AttributeSet, int, int)' is never used
customLinearLayoutManager.smoothScrollToPosition();
我在这条线上面临的一个错误是:
CustomLinearLayoutManager 中的 smoothScrollToPosition() 不能应用于:预期参数:实际参数:recyclerView:RecyclerView 状态:状态位置:int
自定义 Java 类
public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager (Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller =
new LinearSmoothScroller(recyclerView.getContext()) {
private static final float MILLISECONDS_PER_INCH = 200f;
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return CustomLinearLayoutManager.this
.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
主要Java类:
CustomLinearLayoutManager customLinearLayoutManager = new CustomLinearLayoutManager(getContext());
customLinearLayoutManager.smoothScrollToPosition();
recyclerViewHeaderSlider = view.findViewById(R.id.bannerSlider);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerViewHeaderSlider);
recyclerViewHeaderSlider.setHasFixedSize(true);
recyclerViewHeaderSlider.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
headerSliderAdapter.setOnClick(this);
recyclerViewHeaderSlider.setAdapter(headerSliderAdapter);
请告知上述错误的解决方法。另外,请精确代码以实现水平自动滑动回收器视图。使用我提到的自定义线性布局管理器。
解决方案
为什么不使用正常的线性布局管理器并将方向设置为水平
推荐阅读
- typescript - object foo 有两个 props bar 和 baz ,如何通过 bar 的值推断 baz 类型
- excel - 在工作表 B 中存在的 B 列中的工作表 A 中的 A 列中查找值并更改颜色
- google-apps-script - Google Sheet Script Editor - 如果在数组中找不到选项卡名称,则删除多个选项卡的方法
- ajax - 将表格从 ajax 发送到 Django Rest 框架
- javascript - 从一个 json 文件创建多个 markdown 文件的脚本正在添加 http: 和 https: 前缀到链接。为什么?
- sql - 如何比较 2 个表并使用 1 个表中相似的数据返回一列
- python - GraphKeys.TRAINABLE_VARIABLES 与 tf.trainable_variables()
- docker - 如何使用自定义配置启动 apache Storm 容器?
- angular - Angular如何避免选中复选框?
- html - 在右下角没有“位置:绝对”