简体   繁体   English

与构造函数调用相关的错误。 和参数传递

[英]Error related to constructor calling. and passing of parameters

I have trying to make custom layout manager for my recyclerview in android.我试图在 android 中为我的 recyclerview 制作自定义布局管理器。 by the name CustomLinearLayoutManager.名称为 CustomLinearLayoutManager。 and trying to calling constructor and class of this into my home fragment.并尝试将 this 的构造函数和类调用到我的主片段中。 but failed.但失败了。 and getting some errors.并得到一些错误。

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

Home Fragment家庭片段

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

Errors which i am facing..我面临的错误..

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

In Home Fragment:在家庭片段中:

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

Firs, if you defined those constructors and you are not using them then this:首先,如果你定义了这些构造函数并且你没有使用它们,那么这个:

Error: Constructor 'CustomLinearLayoutManager(android.content.Context, android.util.AttributeSet, int, int)' is never used错误:从不使用构造函数 'CustomLinearLayoutManager(android.content.Context, android.util.AttributeSet, int, int)'

This is not an error in all.这完全不是错误。 This is an warning since it is never used.这是一个warning因为它从未被使用过。

Second, if you call a method, please make sure that the parameters are added properly else then this message is displayed.其次,如果你调用一个方法,请确保参数添加正确,否则会显示此消息。

Error: smoothScrollToPosition() in CustomLinearLayoutManager cannot be applied to: Expected Parameters: Actual Arguments:错误:CustomLinearLayoutManager 中的 smoothScrollToPosition() 不能应用于:预期参数:实际参数:

Then you should call this method smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) by adding:然后你应该通过添加以下内容来调用此方法smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position)

First parameter : an object as RecyclerView第一个参数:一个对象作为RecyclerView

Second parameter : an enum as RecyclerView.State第二个参数:枚举作为RecyclerView.State

Third parameter : a position as integer第三个参数:作为整数的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM