Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demostration of the implementation of an auto-scroll ListView in Android. This gist is a reference for the blog post How to autoscroll a ListView at http://blog.jpardogo.com/autoscroll-a-listview-with-listviewautoscrollhelper/
package com.jpardogo.android.myapplication.app;
import android.app.ListActivity;
import android.support.v4.widget.AutoScrollHelper;
import android.support.v4.widget.ListViewAutoScrollHelper;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
public class AutoScrollListActivity extends ListActivity implements View.OnTouchListener, AbsListView.OnScrollListener {
private ListViewAutoScrollHelper mScrollHelper;
private ListView mListView;
private int TOTAL_ITEMS = 2000;
private boolean mActionDown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
mListView.setOnScrollListener(this);
mListView.setOnTouchListener(this);
mListView.setSelection(TOTAL_ITEMS / 2);
initScrollHelper();
startAutoScroll();
}
private void initList() {
final ArrayList<String> items = new ArrayList<String>();
for (int i = 0; i < TOTAL_ITEMS; ++i) {
items.add("position: " + i);
}
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
mListView = getListView();
mListView.setAdapter(adapter);
}
private void initScrollHelper() {
mScrollHelper = new ListViewAutoScrollHelper (mListView) {
@Override
public void scrollTargetBy(int deltaX, int deltaY) {
mListView.smoothScrollBy(2, 0);
}
};
mScrollHelper.setEnabled(true);
mScrollHelper.setEdgeType(AutoScrollHelper.EDGE_TYPE_OUTSIDE);
}
private void startAutoScroll() {
getListView().post(new Runnable() {
@Override
public void run() {
forceScroll();
}
});
}
private void forceScroll() {
MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_MOVE, mListView.getX(), -1, 0);
mScrollHelper.onTouch(getListView(), event);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mActionDown=true;
break;
case MotionEvent.ACTION_UP:
mActionDown=false;
break;
}
return mScrollHelper.onTouch(v, event);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case SCROLL_STATE_IDLE:
if(!mActionDown){
forceScroll();
}
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
}
@Kisty

This comment has been minimized.

Copy link

@Kisty Kisty commented Feb 15, 2018

Thanks for this! Link is broken. Working on is https://medium.com/@jpardogo/how-to-autoscroll-a-listview-c78bf54702e4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment