ListView上拉加载更多数据原理是:
1.继承ListView,完成自定义的ListView;
2.给ListView添加一个正在加载时ListView底部的提示布局,判断当ListView条目在最底部时显示,否则影藏。
package com.example.chl.myapplication; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.AbsListView; import android.widget.ListView; /** * Created by chl on 16-4-12. */ public class DropListView extends ListView implements AbsListView.OnScrollListener { private View moreView; private Context mContext; private boolean isLoading=false; private ILoadListener iLoadListener;//数据加载接口 public DropListView(Context context) { super(context); mContext=context; initView(); } public DropListView(Context context, AttributeSet attrs) { super(context, attrs); mContext=context; initView(); } public DropListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mContext=context; initView(); } private void initView(){ moreView= LayoutInflater.from(mContext).inflate(R.layout.drop_load_more_layout,null);//加载底部的正在加载的信息提示布局 moreView.findViewById(R.id.load_layout).setVisibility(View.GONE);//默认影藏正在加载的提示信息 this.addFooterView(moreView);//把提示布局添加到ListView中 this.setOnScrollListener(this); } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (view.getLastVisiblePosition()==view.getCount()-1 && scrollState==SCROLL_STATE_IDLE){//判断是否滑到最后一条 if (!isLoading){ isLoading=true; moreView.findViewById(R.id.load_layout).setVisibility(View.VISIBLE); iLoadListener.onLoad(); } }
} @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (isLoading) { moreView.findViewById(R.id.load_layout).setVisibility(View.VISIBLE); } } /** * 当数据加载完成时调用此方法 */ public void loadComplete(){ isLoading = false; moreView.findViewById(R.id.load_layout).setVisibility( View.GONE); } /** * 设置数据加载时的接口对象 * @param iLoadListener */ public void setInterface(ILoadListener iLoadListener){ this.iLoadListener = iLoadListener; } //数据加载接口 public interface ILoadListener{ public void onLoad(); } }
底部提示布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/load_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:paddingBottom="10dip" android:paddingTop="10dip" > <ProgressBar style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在加载..." /> </LinearLayout> </LinearLayout>
MainActivity extends Activity implements DropListView.ILoadListener
dropListView.setInterface(this);//设置回调接口
@Override public void onLoad() { //加载更多数据在此方法 }
package com.example.chl.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.os.Handler; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity implements DropListView.ILoadListener { private DropListView dropListView; private List<String> list = new ArrayList<String>(); private MyAdapter adapter; private boolean isFirst=true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initDate(); initView(); } private void initDate() { for (int i = 0; i <= 10; i++) { list.add(i + "我是数据"); } } private void initView() { if (adapter==null) { dropListView = (DropListView) findViewById(R.id.list_view_id); dropListView.setInterface(this);//设置回调接口 adapter = new MyAdapter(); dropListView.setAdapter(adapter); }else { adapter.onDateChange(); } } @Override public void onLoad() { //加载更多数据在此方法 Handler handler=new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { getMoreDate(); initView(); dropListView.loadComplete(); // adapter.notifyDataSetChanged(); } },2000); } public void getMoreDate() { if (isFirst) { isFirst=false; for (int i = 0; i <= 5; i++) { list.add(i + "我是新数据"); } } } class MyAdapter extends BaseAdapter { public void onDateChange() { this.notifyDataSetChanged(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, null); TextView textView= (TextView) convertView.findViewById(R.id.item_textview); textView.setText(list.get(position)); return convertView; } } }