现在我有一个ListView,里面装满了我的“位置”对象。我的列表中最初有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任务
关于如何实现这一点的任何想法?感谢
答案 0 :(得分:1)
你要么:
填充新项目时加载文字/动画动画:How to display a "Loading..." text while retrieving items for a ListView
一个简单无穷无尽的清单,没有花哨的shucy东西:endless scroll list view in android
用户触发人口,通过拉动刷新机制:How to implement Android Pull-to-Refresh
答案 1 :(得分:0)
尝试将此检查添加到onScroll()方法:
if (yourListView.getLastVisiblePosition() == yourListView.getAdapter().getCount() -1 &&
yourListView.getChildAt(yourListView.getChildCount() - 1).getBottom() <= yourListView.getHeight())
{
// the list is scrolled to the bottom
}
这应该用于确定列表是否滚动到底部。