I have 2 horizontal and 1 vertical (Grid layout) in a single Scrollview.
<ScrollView>
<Horizontal RecyclerView/>
<Horizontal RecyclerView/>
<Vertical RecyclerView/>
</ScrollView>
Above is the schematic of my view.
I have to load data on once the vertical recyclerviews last item is visible, but i'm unable to get the scroll event for my vertical view.
Here is my scrollviewlistener.
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = gridLayoutManager.getItemCount();
lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if(onLoadMoreListener!=null) {
onLoadMoreListener.onLoadMore(lastVisibleItem);
}
loading = true;
}
}
});
but this setonscrollListener is never fired. How do I get scroll event for above case.
Thanks in advance:)
I had the problem here is how to solve it:
First create an Interface for scroll listener
public interface EndlessScrollListener {
void onScrollChanged(EndlessScrollView scrollView,int x, int y, int oldx, int oldy);
}
Create a Custom ScrollView by extending the ScrollView
public class EndlessScrollView extends ScrollView
{
private EndlessScrollListener endlessScrollListener = null;
public EndlessScrollView(Context context) {
super(context);
}
public EndlessScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EndlessScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(EndlessScrollListener endlessScrollListener) {
this.endlessScrollListener = endlessScrollListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (endlessScrollListener != null) {
endlessScrollListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
}
Then in your Fragment/Activity layout use the EndlessScrollView instead of Default one:
<YOUR_PACKAGE_NAME.EndlessScrollView
android:id="@+id/my_scroll_view">
<!-- your recycler views ... -->
</YOUR_PACKAGE_NAME.EndlessScrollView>
Next your Activity/Fragment should implement the Interface your created in step 1.
set your Activity/Fragment as Scroll Listener and then use like this:
public class YourActivity extends Activity implements EndlessScrollListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
EndlessScrollView myScrollView = (EndlessScrollView) findViewById(R.id.my_scroll_view);
myScrollView.setScrollViewListener(this);
}
@Override
public void onScrollChanged(EndlessScrollView scrollView, int x, int y, int oldx, int oldy)
{
// We take the last son in the scrollview
View view = scrollView.getChildAt(scrollView.getChildCount() - 1);
int distanceToEnd = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));
// if diff is zero, then the bottom has been reached
if (distanceToEnd == 0) {
// do stuff your load more stuff
}
}
}
Use the gesture detector to get the scroll event in RecyclerView.
EDIT
Replacing ScrollView with NestedScrollView solved my problem of recyclerview scrolling.
final NestedScrollView parentScrollView=(NestedScrollView)view.findViewById (R.id.parentScrollView);
parentScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
// Log.d("ScrollView","scrollX_"+scrollX+"_scrollY_"+scrollY+"_oldScrollX_"+oldScrollX+"_oldScrollY_"+oldScrollY);
//Do something
}
}
});
when you use tow scrollble element inside each other you are in hot water! you should calculate the Recycler item height and then find the whole recycler height. look at below link, I explain completely this problem.
Use RecyclerView indie ScrollView with flexible recycler item height
I hope it help you