【问题标题】:Error related to constructor calling. and passing of parameters与构造函数调用相关的错误。和传递参数
【发布时间】:2019-08-29 16:21:46
【问题描述】:

我试图在 android 中为我的 recyclerview 制作自定义布局管理器。名为 CustomLinearLayoutManager。并尝试将构造函数和 this 的类调用到我的主片段中。但失败了。并得到一些错误。

CustomLinearLayoutManager 类

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);
    }
}

首页片段

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);

我面临的错误..

public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }
Error: Constructor 'CustomLinearLayoutManager(android.content.Context, int, boolean)' is never used
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
 Error: Constructor 'CustomLinearLayoutManager(android.content.Context, android.util.AttributeSet, int, int)' is never used

在家庭片段中:

customLinearLayoutManager.smoothScrollToPosition();
Error: smoothScrollToPosition() in CustomLinearLayoutManager cannot be 
applied to:
Expected Parameters:     Actual Arguments:
recyclerView:               RecyclerView
state:                         State
position:                       int

【问题讨论】:

    标签: java android-studio android-recyclerview


    【解决方案1】:

    首先,如果您定义了这些构造函数并且没有使用它们,那么:

    错误:构造函数 'CustomLinearLayoutManager(android.content.Context, android.util.AttributeSet, int, int)' 从未使用过

    这根本不是一个错误。这是一个warning,因为它从未被使用过。

    其次,如果调用方法,请确保参数添加正确,否则会显示此消息。

    错误:CustomLinearLayoutManager 中的 smoothScrollToPosition() 无法应用于:预期参数:实际参数:

    那么你应该调用这个方法smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position)添加:

    第一个参数:对象为RecyclerView

    第二个参数:一个枚举为RecyclerView.State

    第三个​​参数:位置为整数

    【讨论】:

      猜你喜欢
      • 2015-09-03
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多