|
| 1 | +/** |
| 2 | + * @file PullRefreshView.java |
| 3 | + * @package com.tencent.weibu.view |
| 4 | + * @create Apr 18, 2012 5:22:27 PM |
| 5 | + * @author maxiezhang |
| 6 | + * @description TODO |
| 7 | + */ |
| 8 | +package me.maxwin.view; |
| 9 | + |
| 10 | +import me.maxwin.R; |
| 11 | +import android.content.Context; |
| 12 | +import android.util.AttributeSet; |
| 13 | +import android.view.Gravity; |
| 14 | +import android.view.LayoutInflater; |
| 15 | +import android.view.View; |
| 16 | +import android.view.animation.Animation; |
| 17 | +import android.view.animation.RotateAnimation; |
| 18 | +import android.widget.ImageView; |
| 19 | +import android.widget.LinearLayout; |
| 20 | +import android.widget.ProgressBar; |
| 21 | +import android.widget.TextView; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author maxiezhang |
| 25 | + * |
| 26 | + */ |
| 27 | +public class XListViewHeader extends LinearLayout { |
| 28 | +// private final static String TAG = "PullRefreshView"; |
| 29 | + private LinearLayout mContainer; |
| 30 | + private ImageView mArrowImageView; |
| 31 | + private ProgressBar mProgressBar; |
| 32 | + private TextView mHintTextView; |
| 33 | + private int mState = STATE_NORMAL; |
| 34 | + |
| 35 | + private Animation mRotateUpAnim; |
| 36 | + private Animation mRotateDownAnim; |
| 37 | + |
| 38 | + private final int ROTATE_ANIM_DURATION = 180; |
| 39 | + |
| 40 | + public final static int STATE_NORMAL = 0; |
| 41 | + public final static int STATE_READY = 1; |
| 42 | + public final static int STATE_REFRESHING = 2; |
| 43 | + |
| 44 | + public XListViewHeader(Context context) { |
| 45 | + super(context); |
| 46 | + initView(context); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param context |
| 51 | + * @param attrs |
| 52 | + */ |
| 53 | + public XListViewHeader(Context context, AttributeSet attrs) { |
| 54 | + super(context, attrs); |
| 55 | + initView(context); |
| 56 | + } |
| 57 | + |
| 58 | + private void initView(Context context) { |
| 59 | + // 初始情况,设置下拉刷新view高度为0 |
| 60 | + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( |
| 61 | + LayoutParams.FILL_PARENT, 0); |
| 62 | + mContainer = (LinearLayout) LayoutInflater.from(context).inflate( |
| 63 | + R.layout.xlistview_header, null); |
| 64 | + addView(mContainer, lp); |
| 65 | + setGravity(Gravity.BOTTOM); |
| 66 | + |
| 67 | + mArrowImageView = (ImageView)findViewById(R.id.xlistview_header_arrow); |
| 68 | + mHintTextView = (TextView)findViewById(R.id.xlistview_header_hint_textview); |
| 69 | + mProgressBar = (ProgressBar)findViewById(R.id.xlistview_header_progressbar); |
| 70 | + |
| 71 | + mRotateUpAnim = new RotateAnimation(0.0f, -180.0f, |
| 72 | + Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, |
| 73 | + 0.5f); |
| 74 | + mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION); |
| 75 | + mRotateUpAnim.setFillAfter(true); |
| 76 | + mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f, |
| 77 | + Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, |
| 78 | + 0.5f); |
| 79 | + mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION); |
| 80 | + mRotateDownAnim.setFillAfter(true); |
| 81 | + } |
| 82 | + |
| 83 | + public void setState(int state) { |
| 84 | + if (state == mState) return ; |
| 85 | + |
| 86 | + if (state == STATE_REFRESHING) { // 显示进度 |
| 87 | + mArrowImageView.clearAnimation(); |
| 88 | + mArrowImageView.setVisibility(View.INVISIBLE); |
| 89 | + mProgressBar.setVisibility(View.VISIBLE); |
| 90 | + } else { // 显示箭头图片 |
| 91 | + mArrowImageView.setVisibility(View.VISIBLE); |
| 92 | + mProgressBar.setVisibility(View.INVISIBLE); |
| 93 | + } |
| 94 | + |
| 95 | + switch(state){ |
| 96 | + case STATE_NORMAL: |
| 97 | + if (mState == STATE_READY) { |
| 98 | + mArrowImageView.startAnimation(mRotateDownAnim); |
| 99 | + } |
| 100 | + if (mState == STATE_REFRESHING) { |
| 101 | + mArrowImageView.clearAnimation(); |
| 102 | + } |
| 103 | + mHintTextView.setText("下拉载入更多"); |
| 104 | + break; |
| 105 | + case STATE_READY: |
| 106 | + if (mState != STATE_READY) { |
| 107 | + mArrowImageView.clearAnimation(); |
| 108 | + mArrowImageView.startAnimation(mRotateUpAnim); |
| 109 | + mHintTextView.setText("松开刷新"); |
| 110 | + } |
| 111 | + break; |
| 112 | + case STATE_REFRESHING: |
| 113 | + mHintTextView.setText("正在载入..."); |
| 114 | + break; |
| 115 | + default: |
| 116 | + } |
| 117 | + |
| 118 | + mState = state; |
| 119 | + } |
| 120 | + |
| 121 | + public void setVisiableHeight(int height) { |
| 122 | + // Qzlog.d(TAG, "header height:" + height); |
| 123 | + if (height < 0) |
| 124 | + height = 0; |
| 125 | + LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContainer |
| 126 | + .getLayoutParams(); |
| 127 | + lp.height = height; |
| 128 | + mContainer.setLayoutParams(lp); |
| 129 | + } |
| 130 | + |
| 131 | + public int getVisiableHeight() { |
| 132 | + return mContainer.getHeight(); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments