效果图
下拉刷新:
加载更多:
CustomListView.java
[java] view plain copy
- package
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- /**
- * ListView下拉刷新
- *
- */
- public class CustomListView extends ListView implements
- private final static int RELEASE_To_REFRESH = 0;
- private final static int PULL_To_REFRESH = 1;
- private final static int REFRESHING = 2;
- private final static int DONE = 3;
- private final static int LOADING = 4;
- // 实际的padding的距离与界面上偏移距离的比例
- private final static int RATIO = 3;
- private
- private
- private
- private
- private
- private
- private
- private
- // 用于保证startY的值在一个完整的touch事件中只被记录一次
- private boolean
- private int
- private int
- private int
- private int
- private int
- private boolean
- private
- private
- private boolean
- private
- private
- private
- public
- super(context);
- init(context);
- }
- public
- super(context, attrs);
- init(context);
- }
- private void
- setCacheColorHint(context.getResources().getColor(R.color.transparent));
- inflater = LayoutInflater.from(context);
- null);
- arrowImageView = (ImageView) headView.findViewById(R.id.head_arrowImageView);
- 70);
- 50);
- progressBar = (ProgressBar) headView.findViewById(R.id.head_progressBar);
- tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
- lastUpdatedTextView = (TextView) headView.findViewById(R.id.head_lastUpdatedTextView);
- measureView(headView);
- headContentHeight = headView.getMeasuredHeight();
- headContentWidth = headView.getMeasuredWidth();
- 0, -1 * headContentHeight, 0, 0);
- headView.invalidate();
- "size", "width:" + headContentWidth + " height:"
- null, false);
- this);
- new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
- new
- 250);
- true);
- new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
- new
- 200);
- true);
- state = DONE;
- false;
- null);
- moreView.setVisibility(View.VISIBLE);
- moreProgressBar = (ProgressBar) moreView.findViewById(R.id.pull_to_refresh_progress);
- loadMoreView = (TextView) moreView.findViewById(R.id.load_more);
- new
- @Override
- public void
- onLoad();
- }
- });
- addFooterView(moreView);
- }
- public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2, int
- firstItemIndex = firstVisiableItem;
- }
- public void onScrollStateChanged(AbsListView arg0, int
- }
- public boolean
- if
- switch
- case
- if (firstItemIndex == 0
- true;
- int) event.getY();
- }
- break;
- case
- if
- if
- }
- if
- state = DONE;
- changeHeaderViewByState();
- }
- if
- state = REFRESHING;
- changeHeaderViewByState();
- onRefresh();
- }
- }
- false;
- false;
- break;
- case
- int tempY = (int) event.getY();
- if (!isRecored && firstItemIndex == 0) {
- true;
- startY = tempY;
- }
- if
- // 保证在设置padding的过程中,当前的位置一直是在head,否则如果当列表超出屏幕的话,当在上推的时候,列表会同时进行滚动
- // 可以松手去刷新了
- if
- 0);
- // 往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步
- if (((tempY - startY) / RATIO < headContentHeight) && (tempY - startY) > 0) {
- state = PULL_To_REFRESH;
- changeHeaderViewByState();
- }
- // 一下子推到顶了
- else if (tempY - startY <= 0) {
- state = DONE;
- changeHeaderViewByState();
- }
- // 往下拉了,或者还没有上推到屏幕顶部掩盖head的地步
- }
- // 还没有到达显示松开刷新的时候,DONE或者是PULL_To_REFRESH状态
- if
- 0);
- // 下拉到可以进入RELEASE_TO_REFRESH的状态
- if
- state = RELEASE_To_REFRESH;
- true;
- changeHeaderViewByState();
- }
- else if (tempY - startY <= 0) {
- state = DONE;
- changeHeaderViewByState();
- }
- }
- if
- if (tempY - startY > 0) {
- state = PULL_To_REFRESH;
- changeHeaderViewByState();
- }
- }
- if
- 0, -1 * headContentHeight + (tempY - startY) / RATIO, 0, 0);
- }
- if
- 0, (tempY - startY) / RATIO - headContentHeight, 0, 0);
- }
- }
- break;
- }
- }
- return super.onTouchEvent(event);
- }
- // 当状态改变时候,调用该方法,以更新界面
- private void
- switch
- case
- arrowImageView.setVisibility(View.VISIBLE);
- progressBar.setVisibility(View.GONE);
- tipsTextview.setVisibility(View.VISIBLE);
- lastUpdatedTextView.setVisibility(View.VISIBLE);
- arrowImageView.clearAnimation();
- arrowImageView.startAnimation(animation);
- "松开刷新");
- break;
- case
- progressBar.setVisibility(View.GONE);
- tipsTextview.setVisibility(View.VISIBLE);
- lastUpdatedTextView.setVisibility(View.VISIBLE);
- arrowImageView.clearAnimation();
- arrowImageView.setVisibility(View.VISIBLE);
- // 是由RELEASE_To_REFRESH状态转变来的
- if
- false;
- arrowImageView.clearAnimation();
- arrowImageView.startAnimation(reverseAnimation);
- "下拉刷新");
- else
- "下拉刷新");
- }
- break;
- case
- 0, 0, 0, 0);
- progressBar.setVisibility(View.VISIBLE);
- arrowImageView.clearAnimation();
- arrowImageView.setVisibility(View.GONE);
- "正在刷新...");
- lastUpdatedTextView.setVisibility(View.VISIBLE);
- break;
- case
- 0, -1 * headContentHeight, 0, 0);
- progressBar.setVisibility(View.GONE);
- arrowImageView.clearAnimation();
- arrowImageView.setImageResource(R.drawable.arrow);
- "下拉刷新");
- lastUpdatedTextView.setVisibility(View.VISIBLE);
- break;
- }
- }
- public void
- this.refreshListener = refreshListener;
- true;
- }
- public void
- this.loadListener = loadListener;
- }
- public interface
- public void
- }
- public interface
- public void
- }
- @SuppressWarnings("deprecation")
- public void
- state = DONE;
- "最近更新:" + new
- changeHeaderViewByState();
- }
- private void
- if (loadListener != null) {
- moreProgressBar.setVisibility(View.VISIBLE);
- loadMoreView.setText(getContext().getString(R.string.load_more));
- loadListener.onLoad();
- }
- }
- public void
- // moreView.setVisibility(View.GONE);
- moreProgressBar.setVisibility(View.GONE);
- loadMoreView.setText(getContext().getString(R.string.more_data));
- }
- private void
- if (refreshListener != null) {
- refreshListener.onRefresh();
- }
- }
- private void
- ViewGroup.LayoutParams p = child.getLayoutParams();
- if (p == null) {
- new
- }
- int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
- int
- int
- if (lpHeight > 0) {
- childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
- else
- 0, MeasureSpec.UNSPECIFIED);
- }
- child.measure(childWidthSpec, childHeightSpec);
- }
- @SuppressWarnings("deprecation")
- public void
- "最近更新:" + new
- super.setAdapter(adapter);
- }
- }
在 CustomListView 中有2个回调接口,OnRefreshListener 和 OnLoadListener ,分别对应 下拉和点击加载更多 时候的回调函数。在下拉刷新完成之后要调用 mListView.onRefreshComplete(); 来隐藏掉 头部,调用 mListView.onLoadComplete(); 隐藏掉 底部的加载view。
header.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <!-- ListView的头部 -->
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <!-- 内容 -->
- <RelativeLayout
- android:id="@+id/head_contentLayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="30dp" >
- <!-- 箭头图像、进度条 -->
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true" >
- <!-- 箭头 -->
- <ImageView
- android:id="@+id/head_arrowImageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:contentDescription="@string/app_name"
- android:src="@drawable/arrow" />
- <!-- 进度条 -->
- <ProgressBar
- android:id="@+id/head_progressBar"
- style="?android:attr/progressBarStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:indeterminateDrawable="@drawable/progressbar_bg"
- android:visibility="gone" />
- </FrameLayout>
- <!-- 提示、最近更新 -->
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <!-- 提示 -->
- <TextView
- android:id="@+id/head_tipsTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/pull_to_refresh_pull_label"
- android:textColor="@color/pull_refresh_textview"
- android:textSize="20sp" />
- <!-- 最近更新 -->
- <TextView
- android:id="@+id/head_lastUpdatedTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/pull_to_refresh_refresh_lasttime"
- android:textColor="@color/gold"
- android:textSize="10sp" />
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
listfooter_more.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_horizontal"
- android:orientation="horizontal"
- android:padding="15dp"
- >
- <ProgressBar
- android:id="@+id/pull_to_refresh_progress"
- style="@android:style/Widget.ProgressBar.Small.Inverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:indeterminate="true"
- android:visibility="gone" >
- </ProgressBar>
- <TextView
- android:id="@+id/load_more"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10.0dp"
- android:gravity="center"
- android:text="@string/more_data"
- android:textColor="@color/black" >
- </TextView>
- </LinearLayout>
MainActivity.java
[java] view plain copy
- package
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- import
- public class MainActivity extends
- private static final String TAG = MainActivity.class.getSimpleName();
- private static final int LOAD_DATA_FINISH = 10;
- private static final int REFRESH_DATA_FINISH = 11;
- private List<AppInfo> mList = new
- private
- private
- private int count = 10;
- private Handler handler = new
- public void
- switch
- case
- if(mAdapter!=null){
- mAdapter.notifyDataSetChanged();
- }
- //下拉刷新完成
- break;
- case
- if(mAdapter!=null){
- mAdapter.notifyDataSetChanged();
- }
- //加载更多完成
- break;
- default:
- break;
- }
- };
- };
- @Override
- protected void
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- buildAppData();
- new CustomListAdapter(this);
- mListView = (CustomListView) findViewById(R.id.mListView);
- mListView.setAdapter(mAdapter);
- new
- @Override
- public void
- //TODO 下拉刷新
- "onRefresh");
- 0);
- }
- });
- new
- @Override
- public void
- //TODO 加载更多
- "onLoad");
- 1);
- }
- });
- new
- @Override
- public void
- int position, long
- "click position:"
- }
- });
- }
- public void loadData(final int
- new
- @Override
- public void
- for(int i=count;i<count+10;i++){
- new
- ai.setAppIcon(BitmapFactory.decodeResource(getResources(),
- R.drawable.ic_launcher));
- "应用Demo_"
- "版本: " + (i % 10 + 1) + "." + (i % 8 + 2) + "."
- 6 + 3));
- "大小: " + i * 10 + "MB");
- mList.add(ai);
- }
- 10;
- try
- 300);
- catch
- e.printStackTrace();
- }
- if(type==0){ //下拉刷新
- // Collections.reverse(mList); //逆序
- handler.sendEmptyMessage(REFRESH_DATA_FINISH);
- else if(type==1){
- handler.sendEmptyMessage(LOAD_DATA_FINISH);
- }
- }
- }.start();
- }
- /**
- * 初始化应用数据
- */
- private void
- for (int i = 0; i < 10; i++) {
- new
- ai.setAppIcon(BitmapFactory.decodeResource(getResources(),
- R.drawable.ic_launcher));
- "应用Demo_"
- "版本: " + (i % 10 + 1) + "." + (i % 8 + 2) + "."
- 6 + 3));
- "大小: " + i * 10 + "MB");
- mList.add(ai);
- }
- }
- @Override
- public boolean
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- public class CustomListAdapter extends
- private
- public
- mInflater = LayoutInflater.from(context);
- }
- @Override
- public int
- return
- }
- @Override
- public Object getItem(int
- return
- }
- @Override
- public long getItemId(int
- return
- }
- @Override
- public View getView(int
- if (getCount() == 0) {
- return null;
- }
- null;
- if (convertView == null) {
- null);
- new
- holder.ivImage = (ImageView) convertView
- .findViewById(R.id.ivIcon);
- holder.tvName = (TextView) convertView
- .findViewById(R.id.tvName);
- holder.tvVer = (TextView) convertView.findViewById(R.id.tvVer);
- holder.tvSize = (TextView) convertView
- .findViewById(R.id.tvSize);
- convertView.setTag(holder);
- else
- holder = (ViewHolder) convertView.getTag();
- }
- AppInfo ai = mList.get(position);
- holder.ivImage.setImageBitmap(ai.getAppIcon());
- holder.tvName.setText(ai.getAppName());
- holder.tvVer.setText(ai.getAppVer());
- holder.tvSize.setText(ai.getAppSize());
- return
- }
- }
- public static class
- private
- private
- private
- private
- }
- }
list_item.xml
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/ivIcon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:contentDescription="@string/image_desc"
- android:src="@drawable/ic_launcher" />
- <LinearLayout
- android:id="@+id/appInfo"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dip"
- android:layout_toRightOf="@id/ivIcon"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/name"
- android:textColor="#000000"
- android:textSize="16sp" />
- <TextView
- android:id="@+id/tvVer"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/ver"
- android:textColor="#666666"
- android:textSize="13sp" />
- <TextView
- android:id="@+id/tvSize"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/size"
- android:textColor="#666666"
- android:textSize="13sp" />
- </LinearLayout>
- <Button
- android:id="@+id/btnClick"
- android:layout_width="80dip"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:focusable="false"
- android:text="@string/mgr"
- android:textColor="#000000"
- android:textSize="16sp" />
- </RelativeLayout>