Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load more data when ScrollView is scrolled to bottom

I've got a scrollView with dinamically loading content. Sometimes there can be a lot of content, so I would like to load more when user scrolls to bottom.

I searced for suitable metods and found two:

onScrollChanged() 

and

getScrollY()

But I dont know how to use it for my purposes. Please give me some advice.

like image 274
TpoM6oH Avatar asked Feb 08 '13 13:02

TpoM6oH


2 Answers

Scroll view does not provide any method to check if you've reached bottom of the view so the best technique is to extend your own custom view with scroll view . This is how i implemented in my app.

Step 1:Create a custom class for scroll view

public class ObservableScrollView extends ScrollView {

private ScrollViewListener scrollViewListener = null;

public ObservableScrollView(Context context) {
    super(context);
}

public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public ObservableScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setScrollViewListener(ScrollViewListener scrollViewListener) {
    this.scrollViewListener = scrollViewListener;
}

@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {

    View view = (View) getChildAt(getChildCount() - 1);
    int diff = (view.getBottom() - (getHeight() + getScrollY()));
    if (diff == 0) { // if diff is zero, then the bottom has been reached
        if (scrollViewListener != null) {
            scrollViewListener.onScrollEnded(this, x, y, oldx, oldy);
        }
    }
    super.onScrollChanged(x, y, oldx, oldy);
}

}

Step 2:Create a scroll view Listener Interface

public interface ScrollViewListener {

void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);



 }

Step 3:Add the view in your layout

   <com.platinumapps.facedroid.ObservableScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.platinumapps.facedroid.ObservableScrollView>

Step 4:Implement that interface in your class

  public class MyFragment extends Fragment implements ScrollViewListener 

@Override
public void onScrollEnded(ObservableScrollView scrollView, int x, int y, int oldx,   int oldy) {

           //Perform your action
}

And you are done

like image 91
Jan Avatar answered Nov 17 '22 11:11

Jan


I think the better way would be to use ListView.But if you only required ScrollView.

First you fix a threshold height of your ScrollView so whenever you crosses that limit load new data & append to ScrollView & reset your threshold height.

This is how you can try..

Create a customized ScrollView like this.

public class ObservableScrollView extends ScrollView
{

    private ScrollViewListener scrollViewListener = null;

    public ObservableScrollView(Context context)
    {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public ObservableScrollView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener)
    {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy)
    {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null)
        {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

}

& then create an interface

public interface ScrollViewListener 
{
    void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}

& your XML layout should be like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.solve.stackoverflow.ObservableScrollView
        android:id="@+id/endless_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/endless_scrollview_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" />
    </com.solve.stackoverflow.ObservableScrollView>

</LinearLayout>

& finally use all these in your Activity

public class EndLessActivity extends Activity implements ScrollViewListener
{
    ObservableScrollView scrollView;
    LinearLayout layout;

    int threshold = 20; //Setting threshold 

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.endless_scrollview);
        scrollView = (ObservableScrollView) findViewById(R.id.endless_scrollview);
        layout = (LinearLayout) findViewById(R.id.endless_scrollview_layout);
        scrollView.setScrollViewListener(this);
        appendData();
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy)
    {
        // TODO Auto-generated method stub
        if (y > threshold)
             appendData();
    }

    void appendData()
    {
        for (int i = 0; i < 15; i++)
        {
            threshold += 10;//Reseting threshold.
            TextView tv = new TextView(this);
            tv.setBackgroundResource(R.drawable.ic_launcher);
            layout.addView(tv);
        }
    }
}

Try this & let me know, whether it solve your problem or not.

Thanks

like image 7
Arun Badole Avatar answered Nov 17 '22 09:11

Arun Badole