簡體   English   中英

如何在結束時將項目添加到ListView

[英]How to add items to a ListView when it hits the end

現在,我有一個ListView,里面充滿了我的“ Location”對象。 我的清單最初有10個項目。 當我結束時,我想再添加10個項目。

碼:

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

    }
}

}

所以基本上我想要這樣的東西:1.檢測何時到達終點2.將10添加到loopStart和loopEnd 3.調用我的FillLocations任務

關於如何實現這一點的任何想法? 謝謝

您要么想要:

  1. 填充新項目時的加載文本/動畫動畫: 如何為ListView檢索項目如何顯示“正在加載...”文本

  2. 一個簡單的無盡列表,沒有任何奇特的東西: Android中的無盡滾動列表視圖

  3. 用戶通過拉動刷新機制觸發填充: 如何實現Android Pull-to-Refresh

嘗試將此檢查添加到onScroll()方法中:

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

這應該用於確定列表是否滾動到底部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM