Code example for RelativeLayout

Methods: getBottom, getPaddingBottom, getPaddingLeft, getPaddingRight, setPadding

0
 
    /** 
     * Sets the header padding back to original size. 
     */ 
    private void resetHeaderPadding() { 
        mRefreshView.setPadding(
                mRefreshView.getPaddingLeft(),
                mRefreshOriginalTopPadding,
                mRefreshView.getPaddingRight(),
                mRefreshView.getPaddingBottom());
    } 
 
    /** 
     * Resets the header to the original state. 
     */ 
    private void resetHeader() { 
        if (mRefreshState != TAP_TO_REFRESH) {
            mRefreshState = TAP_TO_REFRESH;
 
            resetHeaderPadding(); 
 
            // Set refresh view text to the pull label 
            mRefreshViewText.setText(tapLabel);
            // Replace refresh drawable with arrow drawable refreshImage 
            mRefreshViewImage.setImageResource(refreshImage);
            // Clear the full rotation animation 
            mRefreshViewImage.clearAnimation();
            // Hide progress bar and arrow. 
            mRefreshViewImage.setVisibility(View.GONE);
            mRefreshViewProgress.setVisibility(View.GONE);
        } 
    } 
 
    private void measureView(View child) {
        ViewGroup.LayoutParams p = child.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        } 
 
        int childWidthSpec = ViewGroup.getChildMeasureSpec(0,
                0 + 0, p.width);
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
        } else { 
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        } 
        child.measure(childWidthSpec, childHeightSpec);
    } 
 
    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // When the refresh view is completely visible, change the text to say 
        // "Release to refresh..." and flip the arrow drawable. 
        if (mCurrentScrollState == SCROLL_STATE_TOUCH_SCROLL
                && mRefreshState != REFRESHING) {
            if (firstVisibleItem == 0) {
                mRefreshViewImage.setVisibility(View.VISIBLE);
                if ((mRefreshView.getBottom() >= mRefreshViewHeight + 20
                        || mRefreshView.getTop() >= 0)
                        && mRefreshState != RELEASE_TO_REFRESH) {
                    mRefreshViewText.setText(releaseLabel);
                    mRefreshViewImage.clearAnimation();
                    mRefreshViewImage.startAnimation(mFlipAnimation);
                    mRefreshState = RELEASE_TO_REFRESH;
                } else if (mRefreshView.getBottom() < mRefreshViewHeight + 20
                        && mRefreshState != PULL_TO_REFRESH) {
                    mRefreshViewText.setText(pullLabel);
                    if (mRefreshState != TAP_TO_REFRESH) {
                        mRefreshViewImage.clearAnimation();
                        mRefreshViewImage.startAnimation(mReverseFlipAnimation);
                    } 
                    mRefreshState = PULL_TO_REFRESH;
                } 
            } else { 
                mRefreshViewImage.setVisibility(View.GONE);
                resetHeader(); 
            } 
        } else if (mCurrentScrollState == SCROLL_STATE_FLING
                && firstVisibleItem == 0
                && mRefreshState != REFRESHING) {
            setSelection(1);
            mBounceHack = true;
        } else if (mBounceHack && mCurrentScrollState == SCROLL_STATE_FLING) {
            setSelection(1);
        } 
 
        if (mOnScrollListener != null) {
            mOnScrollListener.onScroll(view, firstVisibleItem,
                    visibleItemCount, totalItemCount);
        } 
    } 
 
    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        mCurrentScrollState = scrollState;
 
        if (mCurrentScrollState == SCROLL_STATE_IDLE) {
            mBounceHack = false;
        } 
 
        if (mOnScrollListener != null) {
            mOnScrollListener.onScrollStateChanged(view, scrollState);
        } 
    } 
 
    public void prepareForRefresh() { 
        resetHeaderPadding(); 
 
        mRefreshViewImage.setVisibility(View.GONE);
        // We need this hack, otherwise it will keep the previous drawable. 
        mRefreshViewImage.setImageDrawable(null);
        mRefreshViewProgress.setVisibility(View.VISIBLE);
 
        // Set refresh view text to the refreshing label 
        mRefreshViewText.setText(refreshingLabel);
 
        mRefreshState = REFRESHING;
    } 
 
    public void onRefresh() { 
        Log.d(TAG, "onRefresh");
 
        if (mOnRefreshListener != null) {
            mOnRefreshListener.onRefresh();
        } 
    } 
 
    /** 
     * Resets the list to a normal state after a refresh. 
     * @param lastUpdated Last updated at. 
     */ 
    public void onRefreshComplete(CharSequence lastUpdated) {
        setLastUpdated(lastUpdated);
        onRefreshComplete(); 
    } 
 
    /** 
     * Resets the list to a normal state after a refresh. 
     */ 
    public void onRefreshComplete() {         
        Log.d(TAG, "onRefreshComplete");
 
        resetHeader(); 
 
        // If refresh view is visible when loading completes, scroll down to 
        // the next item. 
        if (mRefreshView.getBottom() > 0) {
            invalidateViews();
            setSelection(1);
        } 
    }