Code example for LinearLayout

Methods: getBottom, getPaddingBottom, getPaddingLeft, getPaddingRight, setPadding, setVisibility

0
            // 更新処理していないかどうかをチェック 
            // している場合は無視 
            if (mRefreshState != REFRESHING) {
                if (getFirstVisiblePosition() == 0) { 
                    // TopPaddingを制御してヘッダの高さを伸ばす処理 
                    if (!isPullHeaderVisible && (mRefreshView.getBottom() > 20)) {
                        headerVisivle(); 
                        isPullHeaderVisible = true;
                    } 
                    applyHeaderPadding(event);
                } else if (getLastVisiblePosition() >= mTotalItemCount - 2) {
                    // BottomPaddingを制御してフッタの高さを伸ばす処理 
                   if (!isPullFooterVisible && (mRefreshViewFT.getTop() <= getHeight())) {
                        footerVisibleWithoutLoading(); 
                        isPullFooterVisible = true;
                    } 
                    applyFooterPadding(event);
                } 
            } 
 
//            Log.d("junapp2", "lastpos:" + getLastVisiblePosition()); 
//            Log.d("junapp2", "mTotalItemCount:" + mTotalItemCount); 
//            Log.d("junapp2", "height:" + getHeight()); 
//            Log.d("junapp2", "top:" + mRefreshView.getBottom()); 
            break; 
        } 
        return super.onTouchEvent(event);
    } 
 
    /** 
     * ヘッダを伸縮する処理 
     * 
     * @param ev 
     */ 
    private void applyHeaderPadding(MotionEvent ev) {
        int topPadding = (int) (((ev.getY() - mLastMotionY) - mRefreshViewHeight) * 0.3f);
        mRefreshView.setPadding(mRefreshView.getPaddingLeft(), topPadding, mRefreshView.getPaddingRight(), mRefreshView.getPaddingBottom());
    } 
 
    /** 
     * フッタを伸縮する処理 
     * 
     * @param ev 
     */ 
    private void applyFooterPadding(MotionEvent ev) {
 
        int bottomPadding = (int) ((-(ev.getY() - mLastMotionY) - mRefreshViewFTHeight) * 0.3f);
//        Log.d("junapp2", "bottomPadding:" + bottomPadding); 
//        Log.d("junapp2", "getY:" + ev.getY()); 
//        Log.d("junapp2", "lastY:" + mLastMotionY); 
        mRefreshViewFT.setPadding(mRefreshViewFT.getPaddingLeft(), mRefreshViewFT.getPaddingTop(), mRefreshViewFT.getPaddingRight(), bottomPadding);
    } 
 
    /** 
     * ヘッダのPadding値を初期状態に戻します。 
     */ 
    private void resetHeaderPadding() { 
        mRefreshView.setPadding(mRefreshView.getPaddingLeft(), mRefreshOriginalTopPadding, mRefreshView.getPaddingRight(), mRefreshOriginalBottomPadding);
    } 
 
    /** 
     * フッタのPadding値を初期状態に戻します。 
     */ 
    private void resetFooterPadding() { 
        mRefreshViewFT.setPadding(mRefreshViewFT.getPaddingLeft(), mRefreshFTOriginalTopPadding, mRefreshViewFT.getPaddingRight(), mRefreshFTOriginalBottomPadding);
    } 
 
    /** 
     * ヘッダを初期状態に戻します。 
     */ 
    private void resetHeader() { 
        if (mRefreshState != TAP_TO_REFRESH) {
            mRefreshState = TAP_TO_REFRESH;
 
            //resetHeaderPadding(); 
 
            // Set refresh view text to the pull label 
            mRefreshViewText.setText(R.string.pull_to_refresh_tap_label);
            // Replace refresh drawable with arrow drawable 
            mRefreshViewImage.setImageResource(R.drawable.ic_pulltorefresh_arrow);
            // Clear the full rotation animation 
            mRefreshViewImage.clearAnimation();
            // Hide progress bar and arrow. 
            mRefreshViewImage.setVisibility(View.GONE);
            mRefreshViewProgress.setVisibility(View.GONE);
 
            headerInvisible(); 
        } 
    } 
 
    /** 
     * フッタを初期状態に戻します。 
     */ 
    private void resetFooter() { 
 
        if (mRefreshState != TAP_TO_REFRESH) {
            mRefreshState = TAP_TO_REFRESH;
            footerInvisible(); 
        } 
    } 
 
    /** 
     * Viewの高さを計算します。 
     * 
     * @param child 
     */ 
    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) {
 
        mTotalItemCount = totalItemCount;
        mVisibleItemCount = visibleItemCount;
 
        Log.d("junapp","scrollState:" + mCurrentScrollState);
 
        // 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);
 
                // ヘッダのステートをPullからReleaseに変化させる座標のしきい値 
                int th = getHeight()/7 + mRefreshViewHeight;
 
                if ((mRefreshView.getBottom() < th) && mRefreshState != PULL_TO_REFRESH && mRefreshState != RELEASE_TO_REFRESH) {
                    mRefreshViewText.setText(R.string.pull_to_refresh_pull_label);
                    mRefreshState = PULL_TO_REFRESH;
                } 
                else if ((mRefreshView.getBottom() > th) && mRefreshState != RELEASE_TO_REFRESH) {
                    // 更新処理突入 
                    mRefreshViewText.setText(R.string.pull_to_refresh_release_label);
                    mRefreshState = RELEASE_TO_REFRESH;
                    prepareForRefresh(); 
                } 
            } 
            // フッタステート管理 
            // 最後までスクロールしたかどうかの判定 
            else if (totalItemCount == firstVisibleItem + visibleItemCount) {
                int th = getHeight() - (mRefreshViewFTHeight + getHeight()/7);
 
                if ((mRefreshViewFT.getTop() < th) && mRefreshState != RELEASE_TO_REFRESH_FT) {
                    // 更新処理突入 
                    footerVisibleWithLoading(); 
                    mRefreshState = RELEASE_TO_REFRESH_FT;
                } 
            } 
 
        } else if (mCurrentScrollState == SCROLL_STATE_FLING && firstVisibleItem == 0 && mRefreshState != REFRESHING) {
            setSelection(1);
        } 
 
        if (mOnScrollListener != null) {
            mOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
        } 
    } 
 
    /** 
     * スクロールステートが変化したときに呼び出されます。 
     */ 
    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) {
 
        mCurrentScrollState = scrollState;
 
        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(R.string.pull_to_refresh_refreshing_label);
    } 
 
    /** 
     * 上方更新処理キック 
     */ 
    public void onRefresh() { 
 
        Log.d(TAG, "onRefresh");
 
        if (mOnRefreshListener != null) {
            mOnRefreshListener.onRefresh();
        } 
    } 
 
    /** 
     * 下方更新処理のための前準備 
     */ 
    public void prepareForRefreshFT() { 
        resetFooterPadding(); 
        invalidateViews();
 
        mRefreshState = REFRESHING;
    } 
 
    /** 
     * 下方更新処理をキック 
     */ 
    public void onRefreshFT() { 
        Log.d("junapp2", "onRefreshFT");
 
        if (mOnRefreshListener != null) {
            mOnRefreshListener.onRefreshFT();
        } 
    } 
 
    /** 
     * 上方更新終了処理 
     * @param lastUpdated Last updated at. 
     */ 
    public void onRefreshComplete(CharSequence lastUpdated) {
        setLastUpdated(lastUpdated);
        onRefreshComplete(); 
    } 
 
    /** 
     * 下方更新終了処理 
     * @param lastUpdated 
     */ 
    public void onRefreshCompleteFT(CharSequence lastUpdated) {
        //setLastUpdated(lastUpdated); 
        onRefreshCompleteFT(); 
    } 
 
    public void onRefreshComplete() { 
        Log.d(TAG, "onRefreshComplete");
 
        isLastPosition = false;
 
        resetHeader(); 
 
        if (mRefreshView.getBottom() > 0) {
            invalidateViews();
            setSelection(1);
        } 
    } 
 
    public void onRefreshCompleteFT() { 
        Log.d(TAG, "onRefreshCompleteFT");
 
        isLastPosition = false;
 
        resetFooter(); 
 
        invalidateViews();
    } 
 
    /** 
     * Invoked when the refresh view is clicked on. This is mainly used when 
     * there's only a few items in the list and it's not possible to drag the 
     * list. 
     */ 
    private class OnClickRefreshListener implements OnClickListener {
        @Override 
        public void onClick(View v) {
            if (mRefreshState != REFRESHING) {
                prepareForRefresh(); 
                onRefresh(); 
            } 
        } 
    } 
 
    /** 
     * Interface definition for a callback to be invoked when list should be 
     * refreshed. 
     */ 
    public interface OnRefreshListener { 
        /** 
         * Called when the list should be refreshed. 
         * <p> 
         * A call to {@link PullToRefreshListView #onRefreshComplete()} is 
         * expected to indicate that the refresh has completed. 
         */ 
        public void onRefresh(); 
        public void onRefreshFT(); 
    } 
 
    @Override 
    public boolean performItemClick(View view, int position, long id) {
        // - 1 means that this list has dummy (for refresh) item at index 0 
Log.d("PullToRefreshListView", "count: " + getAdapter().getCount() + ", position: " + position + " -> " + (position - 1) + ", adapter: " + getAdapter());
        return super.performItemClick(view, position - 1, id);
    } 
 
    /** Gets the adapter you set */ 
    public ListAdapter getWrappedAdapter() {
        // HeaderViewListAdapter { YourAdapter } 
Log.d("PullToRefreshListView", "wrapped adapter: " + ((HeaderViewListAdapter) getAdapter()).getWrappedAdapter());
        return ((HeaderViewListAdapter) getAdapter()).getWrappedAdapter();
    } 
 
    /** 
     * ヘッダ非表示処理 
     */ 
    private void headerInvisible() { 
        mRefreshView.setVisibility(View.GONE);
        mRefreshViewLastUpdated.setVisibility(View.GONE);
        mRefreshViewProgress.setVisibility(View.GONE);
        mRefreshViewImage.setVisibility(View.GONE);
        mRefreshViewText.setVisibility(View.GONE);
        mRefreshView.setPadding(mRefreshView.getPaddingLeft(), 0, mRefreshView.getPaddingRight(), 0);
    } 
 
    /** 
     * ヘッダ表示処理 
     */ 
    private void headerVisivle() { 
        resetHeaderPadding(); 
        mRefreshView.setVisibility(View.VISIBLE);
        mRefreshViewText.setVisibility(View.VISIBLE);
        mRefreshViewImage.setVisibility(View.VISIBLE);
        mRefreshViewImage.clearAnimation();
        mRefreshView.setPadding(mRefreshView.getPaddingLeft(), mRefreshOriginalTopPadding, mRefreshView.getPaddingRight(), mRefreshOriginalBottomPadding);
    } 
 
    /** 
     * フッタ表示(ローディング表示なし) 
     */