The author’s original task was to add items to the ListView when it was scrolled.
Let int mEventPosition be the number of the ListView element from the end, at the appearance of which it is necessary to perform some actions ( 0 is the last element, 1 is the penultimate and so on).
public class MainActivity extends AppCompatActivity { private ListView mMyListView; private ArrayAdapter<String> mAdapter; private int mEventPosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEventPosition = 0; mMyListView = (ListView) findViewById(R.id.my_list_view); mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[]{"one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one"}); mMyListView.setAdapter(mAdapter); mMyListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) {} @Override public void onScroll(AbsListView absListView, int i, int i1, int i2) { if(mAdapter.getCount() - 1 - mMyListView.getLastVisiblePosition() == mEventPosition) Log.d("MY_TAG", "Your action"); } }); } }
Please note that in the if in the onScroll method onScroll still need to add a check for the need to trigger it (now it is executed multiple times when scrolling on a given element ).
SwipeRefreshLayoutnot suitable? - s8amListViewis an obsolete component that has been replaced byRecyclerView. However, as you wish. By the way,PullToRefreshListViewfrom Chris Banes is also not supported for a long time. - s8am