当前位置: 首页 > 知识库问答 >
问题:

Android implementing ScrollView setOnScrollChangeListener

暴向笛
2023-03-14

在我的显示和隐藏一些小部件的应用程序中,我在这个站点上找到了下面的代码,但我无法在我的片段上实现它,例如:

public abstract class HidingScrollListener extends RecyclerView.OnScrollListener {
    private static final int HIDE_THRESHOLD = 20;
    private int mScrolledDistance = 0;
    private boolean mControlsVisible = true;
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        int firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();

        if (firstVisibleItem == 0) {
            if(!mControlsVisible) {
                onShow();
                mControlsVisible = true;
            }
        } else {
            if (mScrolledDistance > HIDE_THRESHOLD && mControlsVisible) {
                onHide();
                mControlsVisible = false;
                mScrolledDistance = 0;
            } else if (mScrolledDistance < -HIDE_THRESHOLD && !mControlsVisible) {
                onShow();
                mControlsVisible = true;
                mScrolledDistance = 0;
            }
        }
        if((mControlsVisible && dy>0) || (!mControlsVisible && dy<0)) {
            mScrolledDistance += dy;
        }
    }

    public abstract void onHide();
    public abstract void onShow();
}

我的碎片:

public class FragmentMarketDetail extends Fragment implements ObservableHorizontalScrollView.OnScrollListener {

    private ScrollView scrollViewTest;
    private Context    context;

    public static FragmentMarketDetail newInstance() {
        FragmentMarketDetail fragmentFirst = new FragmentMarketDetail();
        return fragmentFirst;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_online_categories, container, false);
        scrollViewTest = (ScrollView) view.findViewById(R.id.scrollViewTest);
        scrollViewTest.setOnScrollChangeListener(context);
        return view;
    }

    @Override
    public void onScrollChanged(ObservableHorizontalScrollView scrollView, int x, int y, int oldX, int oldY) {
        Log.e("-----> onScrollChanged", x + "");
    }

    @Override
    public void onEndScroll(ObservableHorizontalScrollView scrollView) {
        Log.e("----->onEndScroll ", "");

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }
}

此行出现错误:scrollViewTest。setOnScrollChangeListener(上下文)


共有1个答案

顾恺
2023-03-14
匿名用户

不能将onScrollChangedListener设置为API23正下方的ScrollView。但可以使用

scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {

    @Override
    public void onScrollChanged() {

        int scrollX = rootScrollView.getScrollX(); //for horizontalScrollView
        int scrollY = rootScrollView.getScrollY(); //for verticalScrollView
        //DO SOMETHING WITH THE SCROLL COORDINATES

    }
});

有关滚动视图,请参阅onScrollListener

scrollViewTest.setOnScrollChangeListener(context);

在上面的行中,您试图将< code>Context设置为侦听器,但这是不可能的,因为您的< code>Fragment实现了< code > ObservableHorizontalScrollView。OnScrollListener您应该打电话

scrollViewTest.setOnScrollChangeListener(this);

 类似资料:

相关问答

相关文章

相关阅读