在使用listview时,滑动加载可以提高效率,增加用户体验。

主要用到一个OnScrollListener
   /**         * The view is not scrolling. Note navigating the list using the trackball counts as         * being in the idle state since these transitions are not animated.         */        public static int SCROLL_STATE_IDLE = 0;        /**         * The user is scrolling using touch, and their finger is still on the screen         */        public static int SCROLL_STATE_TOUCH_SCROLL = 1;        /**         * The user had previously been scrolling using touch and had performed a fling. The         * animation is now coasting to a stop         */        public static int SCROLL_STATE_FLING = 2;        /**         * Callback method to be invoked while the list view or grid view is being scrolled. If the         * view is being scrolled, this method will be called before the next frame of the scroll is         * rendered. In particular, it will be called before any calls to         * {@link Adapter#getView(int, View, ViewGroup)}.         *         * @param view The view whose scroll state is being reported         *         * @param scrollState The current scroll state. One of {@link #SCROLL_STATE_IDLE},         * {@link #SCROLL_STATE_TOUCH_SCROLL} or {@link #SCROLL_STATE_IDLE}.         */        public void onScrollStateChanged(AbsListView view, int scrollState);        /**         * Callback method to be invoked when the list or grid has been scrolled. This will be         * called after the scroll has completed         * @param view The view whose scroll state is being reported         * @param firstVisibleItem the index of the first visible cell (ignore if         *        visibleItemCount == 0)         * @param visibleItemCount the number of visible cells         * @param totalItemCount the number of items in the list adaptor         */        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,                int totalItemCount);

package com.nico.listviewload;import java.util.ArrayList;import java.util.List;import com.nico.bean.MsgInfo;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;import android.widget.AbsListView.OnScrollListener;public class MainActivity extends Activity {List<MsgInfo> infoList = null;private LayoutInflater inflater = null;private ListView listview = null;private myAdapter adapter = null;private LinearLayout ftview = null;private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what){case 1: adapter.notifyDataSetChanged();try {Thread.sleep(2000l);} catch (InterruptedException e) {e.printStackTrace();}setAdapter();break;}}};/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);inflater = this.getLayoutInflater();listview = (ListView) findViewById(R.id.listview);ftview = (LinearLayout) inflater.inflate(R.layout.ftview, null);listview.addFooterView(ftview, null, false);listview.setAdapter(new myAdapter());adapter = new myAdapter();ftview.setVisibility(View.VISIBLE);listview.setOnScrollListener(new OnScrollListener() {@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {if (scrollState == SCROLL_STATE_IDLE&& view.getLastVisiblePosition() == view.getCount() - 1){handler.sendEmptyMessage(1);}}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {}});}public List<MsgInfo> getList() {List<MsgInfo> infoList = new ArrayList<MsgInfo>();for (int i = 0; i < 7; i++) {MsgInfo info = new MsgInfo();info.id = i;info.detailinfo = "这是第" + i + "条信息";info.name = "第" + i + "项";info.size = 1024 * i;infoList.add(info);}return infoList;}public void setData() {infoList = getList();}public void setAdapter() {for (int i = 0; i < 7; i++) {MsgInfo info = new MsgInfo();info.id = i;info.detailinfo = "这是第" + i + "条信息";info.name = "第" + i + "项";info.size = 1024 * i;infoList.add(info);}adapter.notifyDataSetChanged();}public class myAdapter extends BaseAdapter {@Overridepublic void notifyDataSetChanged() {if(ftview.getVisibility()==View.VISIBLE){ftview.setVisibility(View.GONE);}else if(ftview.getVisibility()==View.GONE){ftview.setVisibility(View.VISIBLE);}super.notifyDataSetChanged();}TextView name, detail, size;public myAdapter() {setData();}@Overridepublic int getCount() {return infoList.size();}@Overridepublic Object getItem(int position) {return infoList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {convertView = inflater.inflate(R.layout.item, null, false);}name = (TextView) convertView.findViewById(R.id.name);detail = (TextView) convertView.findViewById(R.id.detail);size = (TextView) convertView.findViewById(R.id.size);name.setText(infoList.get(position).name);detail.setText(infoList.get(position).detailinfo);size.setText(infoList.get(position).size + "");return convertView;}}public class LoadAsyTask extends AsyncTask<String, String, String> {@Overrideprotected String doInBackground(String... params) {for (int i = 0; i < 7; i++) {MsgInfo info = new MsgInfo();info.id = i;info.detailinfo = "这是第" + i + "条信息";info.name = "第" + i + "项";info.size = 1024 * i;infoList.add(info);}return null;}@Overrideprotected void onPostExecute(String result) {super.onPostExecute(result);ftview.setVisibility(View.GONE);adapter.notifyDataSetChanged();}}}

<?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"><LinearLayout android:layout_width="fill_parent" android:gravity="center_horizontal"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"><ImageView android:layout_width="50dip"android:layout_height="50dip" android:src="@drawable/progressbar" android:id="@+id/pbdrawable" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="请稍后..." android:id="@+id/pbtxt" /></LinearLayout></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"android:toDegrees="360"><shape android:shape="ring" android:innerRadiusRatio="3"android:thicknessRatio="8" android:useLevel="false"><gradient android:type="sweep" android:useLevel="false"android:startColor="#000000" android:centerColor="#FFFFFF"android:centerY="0.50" android:endColor="#000000" /></shape></rotate>

更多相关文章

  1. Android在WebView加载数据时展示loading的Dialog
  2. android 加载sd卡的图片
  3. android studio加载so包问题
  4. Android实现滑动加载数据的方法
  5. Android 获取 联系人信息
  6. android加载文件的方式,路径的写法
  7. 收藏一个 漂亮的 Android加载中动画AVLoadingIndicatorView
  8. 使用dumpsys查看android系统服务信息
  9. Android自定义Dialog网络加载等待弹窗

随机推荐

  1. android 自定义图片剪裁
  2. android开机logo制作
  3. android照相及照片上传
  4. Android(安卓)Service 示例
  5. 使用程序创建Android桌面快捷方式
  6. android属性
  7. android 安卓事件处理示例
  8. TextView跑马灯
  9. android customactivityoncrashchau 程序
  10. 手工下载android sdk或者system images等