3

I am a New Android Application Developer. I would like to know how to create endless horizontal scroll view. For example, there are three buttons (Button1, Button2 and Button3). When user scroll the view, I still want to display Button1 again after Button3. Could you please provide any sample code or any idea?

Thanks.

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
NML
  • 105
  • 1
  • 11
  • Hope this help: http://stackoverflow.com/questions/19469054/androidhow-to-make-infinite-scrollview-with-endless-scrolling – Neo Nov 30 '15 at 03:24
  • Short way - duplicate contents and rewind scroll offset at specific points. True way - override some dispatch* methods of ScrollView for correct event handling and content rendering. And rewind scroll offset. xD – weaknespase Dec 14 '15 at 17:38

1 Answers1

2

You could check if your button view is still visible. First check if button one is visible:

private boolean isViewVisible(View view) {
    Rect scrollBounds = new Rect();
    mScrollView.getDrawingRect(scrollBounds);

    float top = view.getY();
    float bottom = top + view.getHeight();

    if (scrollBounds.top < top && scrollBounds.bottom > bottom) {
        return true;
    } else {
        return false;
    }
}

If it is not visible, then add button one again. Call this method in a scroll listener every time user scrolls, to check if the button is not visible. If the button is not visible, then add it again.

If you want to make it endless itself, try this:

public class Test extends ListActivity implements OnScrollListener {

    Aleph0 adapter = new Aleph0();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(adapter); 
        getListView().setOnScrollListener(this);
    }

    public void onScroll(AbsListView view,
        int firstVisible, int visibleCount, int totalCount) {

        boolean loadMore = /* maybe add a padding */
            firstVisible + visibleCount >= totalCount;

        if(loadMore) {
            adapter.count += visibleCount; // or any other amount
            adapter.notifyDataSetChanged();
        }
    }

    public void onScrollStateChanged(AbsListView v, int s) { }    

    class Aleph0 extends BaseAdapter {
        int count = 40; /* starting amount */

        public int getCount() { return count; }
        public Object getItem(int pos) { return pos; }
        public long getItemId(int pos) { return pos; }

        public View getView(int pos, View v, ViewGroup p) {
                TextView view = new TextView(Test.this);
                view.setText("entry " + pos);
                return view;
        }
    }
}

And take a look at this:

Android Endless List

android:how to make infinite scrollview with endless scrolling

Thats it. Just see when the view is out of bounds when the user is scrolling.

And when it is out of view, just re add it.

Community
  • 1
  • 1
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • Hi Ruchir, Thanks you for your answer. My question is about endless horizontal scroll view. Your answer for endless list view. Is there any way to set the button position dynamically while scrolling? – NML Dec 02 '15 at 03:30
  • I like the Aleph0 name :-) – Ridcully May 13 '16 at 05:41