最近在找如何实现上/下拉加载更多的操作..
看了那么多.没有好的现成可以利用过的.
所以还是自己做下吧..
下面放出几张效果图片
代码地址 : http://download.csdn.net/detail/f112122/7633523
大概的思路:
把上面和下面的加载当成一个VIEW.
通过 addHeaderView(mHeaderView) 和addFooterView(mFooterView) 方法.
固定到ListView的头部和尾部.然后通过OnScrollListener 监听手指在屏幕的动作来进行相应的操作
如果为顶部的加载更多.则调用onHeadRefresh();
如果为底部的加载更多,则调用onFootRefresh();
通过这两个回调接口.在调用的界面进行相应的处理..如下
所以仅需要修改下loadNewRecord.变成你要的加载数据方法.
就可以快速的加入到你的项目中
public class MainActivity extends Activity implements OnRefreshListener { private final static String TAG = "ChrisLV"; private ListViewExt mListView = null; private String[] mList = { "数据项1", "数据项2", "数据项3", "数据项4", "数据项5", "数据项6", "数据项7", "数据项8", "数据项9", "数据项10", "数据项11", "数据项12", "数据项13", "数据项14", "数据项15", "数据项16", "数据项17", "数据项18", "数据项19", "数据项20", "数据项21", "数据项22", "数据项23", "数据项24" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListViewExt) findViewById(R.id.listview); mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mList)); mListView.setonRefreshListener(this); } @Override public void onHeadRefresh() { // TODO Auto-generated method stub Log.i(TAG, "on head refresh"); loadNewRecord loadNewRecord = new loadNewRecord(); loadNewRecord.execute(); Log.i(TAG, "on head refresh complete"); } @Override public void onFootRefresh() { // TODO Auto-generated method stub Log.i(TAG, "on foot refresh"); loadNewRecord loadNewRecord = new loadNewRecord(); loadNewRecord.execute(); } class loadNewRecord extends AsyncTask<Void, Void, Void> { protected Void doInBackground(Void... params) { // 在这里添加要加载数据的操作. try { Thread.sleep(4000); } catch (Exception e) { // TODO: handle exception } Log.i(TAG, "finish update the object "); return null; } @Override protected void onPostExecute(Void voids) { mListView.onRefreshComplete(); } } }
/*
public void addHeadView (View v)
Add a fixed view to appear at the top of the list. If addHeaderView is called more than once,
the views will appear in the order they were added. Views added using this call can take focus if they want.
*/
public void addFooterView(View v)
Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
代码地址 : http://download.csdn.net/detail/f112122/7633523
转载请声明:http://write.blog.csdn.net/postedit/37764699