12043

Android implementing ScrollView setOnScrollChangeListener

<h3>Question</h3>

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


<h3>Answer1:</h3>

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

<h3>Edit</h3> 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);

来源:https://stackoverflow.com/questions/38138331/android-implementing-scrollview-setonscrollchangelistener

Recommend

  • Android implementing ScrollView setOnScrollChangeListener
  • XML pretty print fails in Python lxml
  • nested scrollview inside recyclerview not triggering onscrolllistener when scrolling down
  • p-dialog onHide not working in angular 2 component - primeng
  • Pandas time series data Index from a string to float [duplicate]
  • Boolean filter using a timestamp value on a dataframe in Python
  • Checking for valid enum types from protobufs
  • Controller or RestController
  • LINQ to populate treeview based upon grouping
  • Compiling multiple source files in Rcpp
  • Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)
  • 'Edit' function for forum posts and such
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • how to specify different css for ie
  • Year over Year Stats from a Crossfilter Dataset
  • When to use the tag in the head and body section of a html page? [duplicate]
  • Ruby regex for matching simpliest Ruby's regexes
  • Why is ordered choice in pyparsing failing for my use case?
  • Calculate time from document
  • Conflicting declaration using constexpr and auto in C++11
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • DataTables move rows between tables
  • How to specify generic type when the type is only known at runtime?
  • How to change user identity when git pushing via ssh?
  • Google App Engine Datastore: Dealing with eventual consistency
  • read part of h5 dataset python
  • Debug `Unexpected end of JSON input Error` on content script
  • Update cell query for Excel ADO from Delphi
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • How to call different template for different category archive page in woocommerce
  • customize soft keyboard key preview
  • Using Service Component Runtime
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web