5

in my application for show and hide some widgets i found this below code on this site, but i cant implementing that on my fragment, for example:

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

My Fragment:

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

I get error for this line: scrollViewTest.setOnScrollChangeListener(context);

DolDurma
  • 15,753
  • 51
  • 198
  • 377

1 Answers1

14

You cannot set a onScrollChangedListener to a ScrollView directly below API23. you may however use this

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

    }
});

see onScrollListener for a ScrollView

Edit

scrollViewTest.setOnScrollChangeListener(context);

In the above line you are attempting to set the Context as a listener which is not possible, as you Fragment implements ObservableHorizontalScrollView.OnScrollListener you should call

scrollViewTest.setOnScrollChangeListener(this);
Community
  • 1
  • 1
Sakchham
  • 1,689
  • 12
  • 22
  • I'm trying to use this class as Singelton class in all of my application. i think my set listener is wrong for implementing that, could you fix my code? Thanks – DolDurma Jul 01 '16 at 06:33
  • 1
    ScrollView#setOnScrollChangeListener takes in an `onScrollChangeListener` as argument and you are passing in a `Context` instead – Sakchham Jul 01 '16 at 06:42
  • and how can i attach this listener for ScrollView? – DolDurma Jul 01 '16 at 06:46
  • problem is this line `scrollViewTest.getViewTreeObserver().addOnScrollChangedListener();` how can i set listener on this line after implementing class on fragment signature? – DolDurma Jul 01 '16 at 06:51
  • @Mahdi.Pishguy if the above soulution does not work, post the complete code for FragmentMarketDetail along with the imports – Sakchham Jul 01 '16 at 18:21