简体   繁体   中英

How to add items to a ListView when it hits the end

Right now I have a ListView which is full of my "Location" objects. I have initially 10 items in my list. When I hit the end, I want to add 10 more items.

Code:

public class MainActivity extends ActionBarActivity {

ListView listView;
int startLoop, endLoop;
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
Button refresh;
private ProgressDialog progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    startLoop = 0;
    endLoop = 10;
    listView = (ListView) findViewById(R.id.listView1);
    progress = new ProgressDialog(this);
    listView.setOnScrollListener((OnScrollListener) this);

    // Construct the data source
    arrayOfLocations = new ArrayList<Location>();

    // Create the adapter to convert the array to views
    adapter = new LocationAdapter(this, arrayOfLocations);

    FillLocations myFill = new FillLocations();
    myFill.execute();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private class FillLocations extends AsyncTask<Integer, Void, ArrayList<Location>> {

    protected void onPreExecute() {

    }

    @Override
    protected ArrayList<Location> doInBackground(Integer... params) {

            for (int i = startLoop; i < endLoop; i++) {

                                    String title = "Place " + Integer.toString(i);
                locArray.add(new Location(title, "details","hours","distance:)));

            }

        return locArray;
    }

    protected void onPostExecute(ArrayList<Location> listOfLocs) {
        // Attach the adapter to a ListView

        for (Location location : listOfLocs)
            adapter.add(location);

        listView.setAdapter(adapter);

        progress.dismiss();

    }
}

}

So basically I want something like this: 1. Detect when it hits the end 2. add 10 to loopStart and loopEnd 3. call my FillLocations task

Any ideas on how I can implement this? Thanks

You either want:

  1. A loading text/animation animation when populating new items: How to display a "Loading..." text while retrieving items for a ListView

  2. A simple endless list, no fancy shmancy stuff: endless scroll list view in android

  3. User triggered population, via a pull to refresh mechanism: How to implement Android Pull-to-Refresh

Try adding this check to your onScroll() method:

if (yourListView.getLastVisiblePosition() == yourListView.getAdapter().getCount() -1 &&
yourListView.getChildAt(yourListView.getChildCount() - 1).getBottom() <= yourListView.getHeight())
{
    // the list is scrolled to the bottom
}

This should work for determining if the the list is scrolled to the bottom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM