效果:
当快到底的时候,程序会自动加载后面的列表
这篇较上篇的改动:
1、去掉了线程互斥加载,直接开线程加载当前IMG,即不判断当前用户是不是在划屏了啥啥的,只要调用到getView()一概加载;
2、重写了ImageAndTextListAdapter类;
一、先看ImageAndTextListAdapter类
全部代码:
- package com.example.try_simpleadapter_new;
- import java.util.ArrayList;
- import java.util.List;
- import com.example.try_simpleadapter_new.AsyncImageLoader.ImageCallback;
- import com.handmark.pulltorefresh.library.PullToRefreshListView;
- import android.R.bool;
- import android.app.Activity;
- import android.graphics.drawable.Drawable;
- import android.graphics.drawable.Drawable.Callback;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ScaleGestureDetector.OnScaleGestureListener;
- import android.widget.AbsListView;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- public class ImageAndTextListAdapter extends BaseAdapter{
- private LayoutInflater inflater;
- private ListView listView;
- private AsyncImageLoader asyncImageLoader;
- private List<ImageAndText> dataArray=new ArrayList<ImageAndText>();
- private ScrollToLastCallBack mScrollToLastCallBack=null;
- public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts,
- ListView listView,final ScrollToLastCallBack scrollToLastCallBack) {
- this.listView = listView;
- asyncImageLoader = new AsyncImageLoader();
- inflater = activity.getLayoutInflater();
- dataArray=imageAndTexts;
- mScrollToLastCallBack=scrollToLastCallBack;
- }
- public interface ScrollToLastCallBack
- {
- public void onScrollToLast(Integer pos);
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return dataArray.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- if(position >= getCount()){
- return null;
- }
- return dataArray.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView == null) {
- convertView = inflater.inflate(R.layout.item, null);
- }
- convertView.setTag(position);
- ImageAndText imageAndText = (ImageAndText) getItem(position);
- String imageUrl = imageAndText.getImageUrl();
- TextView nameView = (TextView) convertView.findViewById(R.id.name);
- nameView.setText(imageAndText.getName());
- TextView infoView = (TextView) convertView.findViewById(R.id.info);
- infoView.setText(imageAndText.getInfo());
- ImageView iv = (ImageView) convertView.findViewById(R.id.img);
- iv.setBackgroundResource(R.drawable.rc_item_bg);
- // 加载IMG,并设定到ImageView中
- asyncImageLoader.loadDrawable(position,imageUrl, new ImageCallback() {
- @Override
- public void onImageLoad(Integer pos, Drawable drawable) {
- Log.d("msg",pos+ "正在贴");
- View view = listView.findViewWithTag(pos);
- if(view != null){
- ImageView iv = (ImageView) view.findViewById(R.id.img);
- iv.setBackgroundDrawable(drawable);
- Log.d("msg","贴成功了");
- }
- }
- //加载不成功的图片处理
- @Override
- public void onError(Integer pos) {
- View view = listView.findViewWithTag(pos);
- if(view != null){
- ImageView iv = (ImageView) view.findViewById(R.id.img);
- iv.setBackgroundResource(R.drawable.rc_item_bg);
- }
- Log.d("msg","没贴成功");
- }
- });
- //判断当前列表所在位置,当到最后两项时就加载
- int end=listView.getLastVisiblePosition();
- if(getCount()-2<=end&&end<=getCount())
- {
- mScrollToLastCallBack.onScrollToLast(position);
- }
- return convertView;
- }
- }
- public interface ScrollToLastCallBack
- {
- public void onScrollToLast(Integer pos);
- }
- public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts,
- ListView listView,final ScrollToLastCallBack scrollToLastCallBack) {
- this.listView = listView;
- asyncImageLoader = new AsyncImageLoader();
- inflater = activity.getLayoutInflater();
- dataArray=imageAndTexts;
- mScrollToLastCallBack=scrollToLastCallBack;
- }
3、调用位置public View getView(……)
- //判断当前列表所在位置,当到最后两项时就加载
- int end=listView.getLastVisiblePosition();
- if(getCount()-2<=end&&end<=getCount())
- {
- mScrollToLastCallBack.onScrollToLast(position);
- }
二、MainActivity.java
这里我们只看OnCreate()函数,其它地方都没变
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
- //设定下拉监听函数
- mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
- @Override
- public void onRefresh(PullToRefreshBase<ListView> refreshView) {
- String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
- DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
- // Update the LastUpdatedLabel
- refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
- // Do work to refresh the list here.
- new GetDataTask().execute();
- }
- });
- // 当用户拉到底时调用
- mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
- @Override
- public void onLastItemVisible() {
- Toast.makeText(MainActivity.this, "End of List!", Toast.LENGTH_SHORT).show();
- // // 到底时加载任务
- // new GetDataTask().execute();
- }
- }); //PullToRefreshBase
- mPullRefreshListView.setMode(Mode.PULL_FROM_START);// 设置底部下拉刷新模式
- //传参生成适配器
- mData = getData();
- ListView actualListView = mPullRefreshListView.getRefreshableView();
- //自己写的回调函数,监听当前列表是否到了倒数第二个列表项
- ScrollToLastCallBack scrollToLastCallBack=new ScrollToLastCallBack(){
- @Override
- public void onScrollToLast(Integer pos) {
- // TODO Auto-generated method stub
- Toast.makeText(MainActivity.this, "到倒数第二个了,加载哈", Toast.LENGTH_SHORT).show();
- // 到底时加载任务
- new GetDataTask().execute();
- }
- };
- adapter = new ImageAndTextListAdapter(this,mData,actualListView,scrollToLastCallBack);
- // 设置适配器
- actualListView.setAdapter(adapter);
- }
- //自己写的回调函数,监听当前列表是否到了倒数第二个列表项
- ScrollToLastCallBack scrollToLastCallBack=new ScrollToLastCallBack(){
- @Override
- public void onScrollToLast(Integer pos) {
- // TODO Auto-generated method stub
- Toast.makeText(MainActivity.this, "到倒数第二个了,加载哈", Toast.LENGTH_SHORT).show();
- // 到底时加载任务
- new GetDataTask().execute();
- }
- };
- adapter = new ImageAndTextListAdapter(this,mData,actualListView,scrollToLastCallBack);
OK,到这就结束了,其实就是一个回调函数的书写,难度不大,只是在涉及代码量大了的话就相对来讲有点小难度了。
最后,源码来啦:http://download.csdn.net/detail/harvic880925/6804687 (不要分,仅供分享)
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/17792755 ,谢谢!!