Load more data when ScrollView scrolls down.

I have a scrollView with dynamic content loading. Sometimes there can be a lot of content, so I would like to load more when the user scrolls down.

I searched for suitable methods and found two:

onScrollChanged() 

and

getScrollY()

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

+5
source share
2 answers

The scroll view does not provide any method for checking whether you have reached the bottom of the view, so the best way is to expand your own custom view with a scroll view. This is how I implemented in my application.

Step 1. Create your own class to view the scroll.

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

}

2. . .

public interface ScrollViewListener {

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



 }

3:

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

4:

  public class MyFragment extends Fragment implements ScrollViewListener 

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

           //Perform your action
}

+11

, - ListView. ScrollView.

ScrollView, , , ScrollView reset .

.

ScrollView, .

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

}

&

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

& XML-

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

& ,

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

, .

+6

All Articles