我有我的 ListFragment 并且想知道当我到达底部时在列表中加载更多内容的最佳方法是什么,我相信我需要实现一个 Scroll Listener 但我对何时何地执行调用有点困惑,如果有人可以提供一些非常感谢的信息,我知道我的代码现在也有点混乱,因为我只是在测试它,所以对此表示歉意。
代码: TestListFragment
package com.fragments;
public class TestListFragment extends ListFragment
implements
android.support.v4.app.LoaderManager.LoaderCallbacks<List<TestItemModel>> {
TestCustomArrayAdapter _adapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println("TestListFragment.onActivityCreated");
// Initially there is no data
setEmptyText("Refresh: No Data Here");
// Create an empty adapter we will use to display the loaded data.
_adapter = new TestCustomArrayAdapter(getActivity());
setListAdapter(_adapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("TestListFragment", "Item clicked: " + id);
TestItemModel ti = this._adapter.getItem(position);
System.out.println(ti.getId());
}
@Override
public Loader<List<TestItemModel>> onCreateLoader(int arg0, Bundle arg1) {
System.out.println("TestListFragment.onCreateLoader");
return new TestListLoaderAsync(getActivity());
}
@Override
public void onLoadFinished(Loader<List<TestItemModel>> arg0,
List<TestItemModel> data) {
_adapter.setData(data);
System.out.println("TestListFragment.onLoadFinished");
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}
@Override
public void onLoaderReset(Loader<List<TestItemModel>> arg0) {
_adapter.setData(null);
}
}
TestCustomArrayAdapter
package com.adapters;
public class TestCustomArrayAdapter extends ArrayAdapter<TestItemModel> {
private final LayoutInflater _inflater;
public OnItemClickListener _ol;
public ImageLoader _imageLoader;
public TestCustomArrayAdapter(Context context) {
super(context, R.layout.test_list_fragment);
_inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Load up Image Loader
_imageLoader = new ImageLoader(context);
}
public void setData(List<TestItemModel> data) {
clear();
if (data != null) {
for (TestItemModel appEntry : data) {
add(appEntry);
}
}
}
/**
* Populate new items in the list.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// create viewHolder
View view;
// Check Views
if (convertView == null) {
view = _inflater.inflate(R.layout.test_single_item, parent, false);
} else {
view = convertView;
}
// Create Items
TestItemModel item = getItem(position);
TextView itemLabel = (TextView) view.findViewById(R.id.item_label);
TextView itemId = (TextView) view.findViewById(R.id.item_id);
// Create ImageViews
ImageView image = (ImageView) view.findViewById(R.id.image_id);
// Create Buttons
Button btn1 = (Button) view.findViewById(R.id.button_id_1);
Button btn2 = (Button) view.findViewById(R.id.button_id_2);
Button btn3 = (Button) view.findViewById(R.id.button_id_3);
// Create Resources
Resources resources = this.getContext().getResources();
// Create Listeners
_ol = new OnItemClickListener(position, item);
// Setup TextViews
itemLabel.setText(item.getName());
itemId.setText(item.getName());
// Setup Images
_imageLoader.DisplayImage(item.getBm(), image);
// Setup Buttons
btn1.setOnClickListener(_ol);
btn1.setTag(1);
btn2.setOnClickListener(_ol);
btn2.setTag(2);
btn3.setOnClickListener(_ol);
btn3.setTag(3);
return view;
}
private class OnItemClickListener implements OnClickListener {
private int _position;
private TestItemModel _testItem;
public OnItemClickListener(int position, TestItemModel ti) {
_position = position;
_testItem = ti;
}
// TODO
// provide functionality for which button was clicked then pass the item
// to which it was clicked in.
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_id_1:
// btn1 clicked
Toast.makeText(getContext(),
" Button1 clicked at positon" + v.getTag(),
Toast.LENGTH_SHORT).show();
break;
case R.id.button_id_2:
// btn2 clicked
Toast.makeText(getContext(),
" Button2 clicked at positon" + v.getTag(),
Toast.LENGTH_SHORT).show();
break;
case R.id.button_id_3:
Toast.makeText(getContext(),
" Button3 clicked at positon" + v.getTag(),
Toast.LENGTH_SHORT).show();
// btn 3 clciked
break;
}
// the view is the button, so you get get the tag it has set with
// v.getTag() to know what button is pressed.
Log.v("YOOO",
"Button Click at position " + _position + " " + v.getTag()
+ " Item ID = " + _testItem.getId());
}
}
}
TestListLoaderAsync
包 com.tasks;
公共类 TestListLoaderAsync 扩展 AsyncTaskLoader> {
private List<TestItemModel> _models;
// Test URLS
private final String[] URLS = {
"http://www.pressurewasherparts.com.au/upimages/image/Water_Broom/Water%20Broom%203%20Noz%20upright.jpg",
"http://www.pressurewasherparts.com.au/upimages/image/Water_Broom/Water%20Broom%203%20Noz%20upright.jpg",
"http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg",
"http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg",
"http://lh3.ggpht.com/_lLj6go_T1CQ/TCD8PW09KBI/AAAAAAAAQdc/AqmOJ7eg5ig/s144-c/Juvenile%20Gannet%20despute.jpg",
"http://lh6.ggpht.com/_ZN5zQnkI67I/TCFFZaJHDnI/AAAAAAAABVk/YoUbDQHJRdo/s144-c/P9250508.JPG",
"http://lh4.ggpht.com/_XjNwVI0kmW8/TCOwNtzGheI/AAAAAAAAC84/SxFJhG7Scgo/s144-c/0014.jpg",
"http://lh6.ggpht.com/_lnDTHoDrJ_Y/TBvKsJ9qHtI/AAAAAAAAG6g/Zll2zGvrm9c/s144-c/000007.JPG",
"http://lh6.ggpht.com/_qvCl2efjxy0/TCIVI-TkuGI/AAAAAAAAOUY/vbk9MURsv48/s144-c/DSC_0844.JPG",
"http://lh4.ggpht.com/_TPlturzdSE8/TBv4ugH60PI/AAAAAAAAMsI/p2pqG85Ghhs/s144-c/_MG_3963.jpg",
"http://lh4.ggpht.com/_4f1e_yo-zMQ/TCe5h9yN-TI/AAAAAAAAXqs/8X2fIjtKjmw/s144-c/IMG_1786.JPG",
"http://lh6.ggpht.com/_iFt5VZDjxkY/TB9rQyWnJ4I/AAAAAAAADpU/lP2iStizJz0/s144-c/DSCF1014.JPG",
"http://lh5.ggpht.com/_hepKlJWopDg/TB-_WXikaYI/AAAAAAAAElI/715k4NvBM4w/s144-c/IMG_0075.JPG",
"http://lh6.ggpht.com/_OfRSx6nn68g/TCzsQic_z3I/AAAAAAABOOI/5G4Kwzb2qhk/s144-c/EASTER%20ISLAND_Hanga%20Roa_31.5.08_46.JPG",
"http://lh6.ggpht.com/_ZGv_0FWPbTE/TB-_GLhqYBI/AAAAAAABVxs/cVEvQzt0ke4/s144-c/IMG_1288_hf.jpg",
"http://lh6.ggpht.com/_a29lGRJwo0E/TBqOK_tUKmI/AAAAAAAAVbw/UloKpjsKP3c/s144-c/31012332.jpg",
"http://lh3.ggpht.com/_55Lla4_ARA4/TB6xbyxxJ9I/AAAAAAABTWo/GKe24SwECns/s144-c/Bluebird%20049.JPG",
"http://lh3.ggpht.com/_iVnqmIBYi4Y/TCaOH6rRl1I/AAAAAAAA1qg/qeMerYQ6DYo/s144-c/Kwiat_100626_0016.jpg",
"http://lh6.ggpht.com/_QFsB_q7HFlo/TCItd_2oBkI/AAAAAAAAFsk/4lgJWweJ5N8/s144-c/3705226938_d6d66d6068_o.jpg",
"http://lh5.ggpht.com/_JTI0xxNrKFA/TBsKQ9uOGNI/AAAAAAAChQg/z8Exh32VVTA/s144-c/CRW_0015-composite.jpg",
"http://lh6.ggpht.com/_loGyjar4MMI/S-InVNkTR_I/AAAAAAAADJY/Fb5ifFNGD70/s144-c/Moving%20Rock.jpg",
"http://lh4.ggpht.com/_L7i4Tra_XRY/TBtxjScXLqI/AAAAAAAAE5o/ue15HuP8eWw/s144-c/opera%20house%20II.jpg",
"http://lh3.ggpht.com/_rfAz5DWHZYs/S9cstBTv1iI/AAAAAAAAeYA/EyZPUeLMQ98/s144-c/DSC_6425.jpg",
"http://lh6.ggpht.com/_iGI-XCxGLew/S-iYQWBEG-I/AAAAAAAACB8/JuFti4elptE/s144-c/norvig-polar-bear.jpg",
"http://lh3.ggpht.com/_M3slUPpIgmk/SlbnavqG1cI/AAAAAAAACvo/z6-CnXGma7E/s144-c/mf_003.jpg",
"http://lh4.ggpht.com/_loGyjar4MMI/S-InQvd_3hI/AAAAAAAADIw/dHvCFWfyHxQ/s144-c/Rainbokeh.jpg",
"http://lh4.ggpht.com/_yy6KdedPYp4/SB5rpK3Zv0I/AAAAAAAAOM8/mokl_yo2c9E/s144-c/Point%20Reyes%20road%20.jpg",
"http://lh5.ggpht.com/_6_dLVKawGJA/SMwq86HlAqI/AAAAAAAAG5U/q1gDNkmE5hI/s144-c/mobius-glow.jpg",
"http://lh3.ggpht.com/_QFsB_q7HFlo/TCItc19Jw3I/AAAAAAAAFs4/nPfiz5VGENk/s144-c/4551649039_852be0a952_o.jpg",
"http://lh6.ggpht.com/_TQY-Nm7P7Jc/TBpjA0ks2MI/AAAAAAAABcI/J6ViH98_poM/s144-c/IMG_6517.jpg",
"http://lh3.ggpht.com/_rfAz5DWHZYs/S9cLAeKuueI/AAAAAAAAeYU/E19G8DOlJRo/s144-c/DSC_4397_8_9_tonemapped2.jpg",
"http://lh4.ggpht.com/_TQY-Nm7P7Jc/TBpi6rKfFII/AAAAAAAABbg/79FOc0Dbq0c/s144-c/david_lee_sakura.jpg",
"http://lh3.ggpht.com/_TQY-Nm7P7Jc/TBpi8EJ4eDI/AAAAAAAABb0/AZ8Cw1GCaIs/s144-c/Hokkaido%20Swans.jpg",
"http://lh3.ggpht.com/_1aZMSFkxSJI/TCIjB6od89I/AAAAAAAACHM/CLWrkH0ziII/s144-c/079.jpg",
"http://lh5.ggpht.com/_loGyjar4MMI/S-InWuHkR9I/AAAAAAAADJE/wD-XdmF7yUQ/s144-c/Colorado%20River%20Sunset.jpg",
"http://lh3.ggpht.com/_0YSlK3HfZDQ/TCExCG1Zc3I/AAAAAAAAX1w/9oCH47V6uIQ/s144-c/3138923889_a7fa89cf94_o.jpg",
"http://lh6.ggpht.com/_K29ox9DWiaM/TAXe4Fi0xTI/AAAAAAAAVIY/zZA2Qqt2HG0/s144-c/IMG_7100.JPG",
"http://lh6.ggpht.com/_0YSlK3HfZDQ/TCEx16nJqpI/AAAAAAAAX1c/R5Vkzb8l7yo/s144-c/4235400281_34d87a1e0a_o.jpg",
"http://lh4.ggpht.com/_8zSk3OGcpP4/TBsOVXXnkTI/AAAAAAAAAEo/0AwEmuqvboo/s144-c/yosemite_forrest.jpg",
"http://lh4.ggpht.com/_6_dLVKawGJA/SLZToqXXVrI/AAAAAAAAG5k/7fPSz_ldN9w/s144-c/coastal-1.jpg",
"http://lh4.ggpht.com/_WW8gsdKXVXI/TBpVr9i6BxI/AAAAAAABhNg/KC8aAJ0wVyk/s144-c/IMG_6233_1_2-2.jpg",
"http://lh3.ggpht.com/_loGyjar4MMI/S-InS0tJJSI/AAAAAAAADHU/E8GQJ_qII58/s144-c/Windmills.jpg",
"http://lh4.ggpht.com/_loGyjar4MMI/S-InbXaME3I/AAAAAAAADHo/4gNYkbxemFM/s144-c/Frantic.jpg",
"http://lh5.ggpht.com/_loGyjar4MMI/S-InKAviXzI/AAAAAAAADHA/NkyP5Gge8eQ/s144-c/Rice%20Fields.jpg",
"http://lh3.ggpht.com/_loGyjar4MMI/S-InZA8YsZI/AAAAAAAADH8/csssVxalPcc/s144-c/Seahorse.jpg",
"http://lh3.ggpht.com/_syQa1hJRWGY/TBwkCHcq6aI/AAAAAAABBEg/R5KU1WWq59E/s144-c/Antelope.JPG",
"http://lh5.ggpht.com/_MoEPoevCLZc/S9fHzNgdKDI/AAAAAAAADwE/UAno6j5StAs/s144-c/c84_7083.jpg",
"http://lh4.ggpht.com/_DJGvVWd7IEc/TBpRsGjdAyI/AAAAAAAAFNw/rdvyRDgUD8A/s144-c/Free.jpg",
"http://lh6.ggpht.com/_iO97DXC99NY/TBwq3_kmp9I/AAAAAAABcz0/apq1ffo_MZo/s144-c/IMG_0682_cp.jpg",
"http://lh4.ggpht.com/_7V85eCJY_fg/TBpXudG4_PI/AAAAAAAAPEE/8cHJ7G84TkM/s144-c/20100530_120257_0273-Edit-2.jpg" };
public TestListLoaderAsync(Context context) {
super(context);
}
@Override
public List<TestItemModel> loadInBackground() {
System.out.println("TestListLoader.loadInBackground");
// You should perform the heavy task of getting data from
// Internet or database or other source
// Here, we are generating some Sample data
// Create corresponding array of entries and load with data.
List<TestItemModel> entries = new ArrayList<TestItemModel>(5);
entries.add(new TestItemModel("Java", "1", URLS[1]));
entries.add(new TestItemModel("C++", "2", URLS[2]));
entries.add(new TestItemModel("Python", "3", URLS[3]));
entries.add(new TestItemModel("JavaScript", "4", URLS[4]));
entries.add(new TestItemModel("D", "5", URLS[5]));
entries.add(new TestItemModel("C", "6", URLS[6]));
entries.add(new TestItemModel("Perl", "7", URLS[7]));
entries.add(new TestItemModel("Fortran", "8", URLS[8]));
entries.add(new TestItemModel("Cobalt", "9", URLS[9]));
entries.add(new TestItemModel("Ruby", "10", URLS[10]));
entries.add(new TestItemModel("Pascal", "11", URLS[11]));
entries.add(new TestItemModel("HTML", "12", URLS[12]));
entries.add(new TestItemModel("CSS", "13", URLS[13]));
entries.add(new TestItemModel("PHP", "14", URLS[14]));
entries.add(new TestItemModel("MYSQL", "15", URLS[15]));
entries.add(new TestItemModel("Fortran", "8", URLS[16]));
entries.add(new TestItemModel("Cobalt", "9", URLS[17]));
entries.add(new TestItemModel("Ruby", "10", URLS[18]));
entries.add(new TestItemModel("Pascal", "11", URLS[19]));
entries.add(new TestItemModel("HTML", "12", URLS[20]));
entries.add(new TestItemModel("CSS", "13", URLS[21]));
entries.add(new TestItemModel("PHP", "14", URLS[22]));
entries.add(new TestItemModel("MYSQL", "15", URLS[23]));
TestItemModel lastItem = new TestItemModel("C#", "16", URLS[0]);
lastItem.setId("TestingId");
entries.add(lastItem);
return entries;
}
/**
* Called when there is new data to deliver to the client. The super class
* will take care of delivering it; the implementation here just adds a
* little more logic.
*/
@Override
public void deliverResult(List<TestItemModel> listOfData) {
if (isReset()) {
// An async query came in while the loader is stopped. We
// don't need the result.
if (listOfData != null) {
onReleaseResources(listOfData);
}
}
List<TestItemModel> oldApps = listOfData;
_models = listOfData;
if (isStarted()) {
// If the Loader is currently started, we can immediately
// deliver its results.
super.deliverResult(listOfData);
}
// At this point we can release the resources associated with
// 'oldApps' if needed; now that the new result is delivered we
// know that it is no longer in use.
if (oldApps != null) {
onReleaseResources(oldApps);
}
}
/**
* Handles a request to start the Loader.
*/
@Override
protected void onStartLoading() {
if (_models != null) {
// If we currently have a result available, deliver it
// immediately.
deliverResult(_models);
}
if (takeContentChanged() || _models == null) {
// If the data has changed since the last time it was loaded
// or is not currently available, start a load.
forceLoad();
}
}
/**
* Handles a request to stop the Loader.
*/
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
/**
* Handles a request to cancel a load.
*/
@Override
public void onCanceled(List<TestItemModel> apps) {
super.onCanceled(apps);
// At this point we can release the resources associated with 'apps'
// if needed.
onReleaseResources(apps);
}
/**
* Handles a request to completely reset the Loader.
*/
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
// At this point we can release the resources associated with 'apps'
// if needed.
if (_models != null) {
onReleaseResources(_models);
_models = null;
}
}
/**
* Helper function to take care of releasing resources associated with an
* actively loaded data set.
*/
protected void onReleaseResources(List<TestItemModel> apps) {
}
}