XListview是一个非常受欢迎的下拉刷新控件,但是已经停止维护了。之前写过一篇XListview的使用介绍,用起来非常简单,这两天放假无聊,研究了下XListview的实现原理,学到了很多,今天分享给大家。
提前声明,为了让代码更好的理解,我对代码进行了部分删减和重构,如果大家想看原版代码,请去github自行下载。
Xlistview项目主要是三部分:XlistView,XListViewHeader,XListViewFooter,分别是XListView主体、header、footer的实现。下面我们分开来介绍。
下面是修改之后的XListViewHeader代码
[java] view plaincopy
- public class XListViewHeader extends LinearLayout {
- private static final String HINT_NORMAL = "下拉刷新";
- private static final String HINT_READY = "松开刷新数据";
- private static final String HINT_LOADING = "正在加载...";
- // 正常状态
- public final static int STATE_NORMAL = 0;
- // 准备刷新状态,也就是箭头方向发生改变之后的状态
- public final static int STATE_READY = 1;
- // 刷新状态,箭头变成了progressBar
- public final static int STATE_REFRESHING = 2;
- // 布局容器,也就是根布局
- private LinearLayout container;
- // 箭头图片
- private ImageView mArrowImageView;
- // 刷新状态显示
- private ProgressBar mProgressBar;
- // 说明文本
- private TextView mHintTextView;
- // 记录当前的状态
- private int mState;
- // 用于改变箭头的方向的动画
- private Animation mRotateUpAnim;
- private Animation mRotateDownAnim;
- // 动画持续时间
- private final int ROTATE_ANIM_DURATION = 180;
- public XListViewHeader(Context context) {
- super(context);
- initView(context);
- }
- public XListViewHeader(Context context, AttributeSet attrs) {
- super(context, attrs);
- initView(context);
- }
- private void initView(Context context) {
- mState = STATE_NORMAL;
- // 初始情况下,设置下拉刷新view高度为0
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
- LayoutParams.MATCH_PARENT, 0);
- container = (LinearLayout) LayoutInflater.from(context)www.qinlinyule.cn .inflate(
- R.layout.xlistview_header, null);
- addView(container, lp);
- // 初始化控件
- mArrowImageView = (ImageView) findViewById(R.id.xlistview_header_arrow);
- mHintTextView = (TextView) findViewById(R.id.xlistview_header_hint_textview);
- mProgressBar = (ProgressBar) findViewById(R.id.xlistview_header_progressbar);
- // 初始化动画
- mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
- Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
- 0.5f);
- mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
- mRotateUpAnim.setFillAfter(true);
- mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
- Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
- 0.5f);
- mRotateDownAnim.setDuration(ROTATE_www.thd178.com/ ANIM_DURATION);
- mRotateDownAnim.setFillAfter(true);
- }
- // 设置header的状态
- public void setState(int state) {
- if (state == mState)
- return;
- // 显示进度
- if (state == STATE_REFRESHING) www.taohuayuan178.com{
- mArrowImageView.clearAnimation(www.feifanyule.cn);
- mArrowImageView.setVisibility(View.INVISIBLE);
- mProgressBar.setVisibility(www.120xh.cn View.VISIBLE);
- } else {
- // 显示箭头
- mArrowImageView.setVisibility(View.VISIBLE);
- mProgressBar.setVisibility(View.www.vboyule66.cn/ INVISIBLE);
- }
- switch (state) {
- case STATE_NORMAL:
- if (mState == STATE_READY) {
- mArrowImageView.startAnimation(mRotateDownAnim);
- }
- if (mState == STATE_REFRESHING) {
- mArrowImageView.clearAnimation();
- }
- mHintTextView.setText(HINT_NORMAL);
- break;
- case STATE_READY:
- if (mState != STATE_READY) {
- mArrowImageView.clearAnimation();
- mArrowImageView.startAnimation(mRotateUpAnim);
- mHintTextView.setText(HINT_READY);
- }
- break;
- case STATE_REFRESHING:
- mHintTextView.setText(HINT_LOADING);
- break;
- }
- mState = state;
- }
- public void setVisiableHeight(int height) {
- if (height < 0)
- height = 0;
- LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) container
- .getLayoutParams();
- lp.height = height;
- container.setLayoutParams(lp);
- }
- public int getVisiableHeight() {
- return container.getHeight();
- }
- public void show() {
- container.setVisibility(View.VISIBLE);
- }
- public void hide() {
- container.setVisibility(View.INVISIBLE);
- }
- }
XListViewHeader继承自linearLayout,用来实现下拉刷新时的界面展示,可以分为三种状态:正常、准备刷新、正在加载。
在Linearlayout布局里面,主要有指示箭头、说明文本、圆形加载条三个控件。在构造函数中,调用了initView()进行控件的初始化操作。在添加布局文件的时候,指定高度为0,这是为了隐藏header,然后初始化动画,是为了完成箭头的旋转动作。
setState()是设置header的状态,因为header需要根据不同的状态,完成控件隐藏、显示、改变文字等操作,这个方法主要是在XListView里面调用。除此之外,还有setVisiableHeight()和getVisiableHeight(),这两个方法是为了设置和获取Header中根布局文件的高度属性,从而完成拉伸和收缩的效果,而show()和hide()则显然就是完成显示和隐藏的效果。
下面是Header的布局文件
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="bottom" >
- <RelativeLayout
- android:id="@+id/xlistview_header_content"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- tools:ignore="UselessParent" >
- <TextView
- android:id="@+id/xlistview_header_hint_textview"
- android:layout_width="100dp"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:gravity="center"
- android:text="正在加载"
- android:textColor="@android:color/black"
- android:textSize="14sp" />
- <ImageView
- android:id="@+id/xlistview_header_arrow"
- android:layout_width="30dp"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_toLeftOf="@id/xlistview_header_hint_textview"
- android:src="@drawable/xlistview_arrow" />
- <ProgressBar
- android:id="@+id/xlistview_header_progressbar"
- style="@style/progressbar_style"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_centerVertical="true"
- android:layout_toLeftOf="@id/xlistview_header_hint_textview"
- android:visibility="invisible" />
- </RelativeLayout>
- </LinearLayout>
原文地址:https://www.cnblogs.com/qwangxiao/p/9080104.html