Browse files

Removed drag to refresh

  • Loading branch information...
1 parent 288ede6 commit 32b5a2f0195916015abf1db473c1d0fc204eb405 @scruffyfox committed Oct 16, 2012
Showing with 0 additions and 661 deletions.
  1. +0 −70 res/layout/pull_to_refresh_header.xml
  2. +0 −5 res/values/attr.xml
  3. +0 −5 res/values/strings.xml
  4. +0 −581 src/x/ui/XUIDragRefreshListView.java
View
70 res/layout/pull_to_refresh_header.xml
@@ -1,70 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 Johan Nilsson <http://markupartist.com>
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
--->
-
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/header"
- android:padding="15dp"
- >
- <ProgressBar
- android:id="@+id/pull_to_refresh_progress"
- android:indeterminate="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:visibility="gone"
- android:layout_centerVertical="true"
- style="?android:attr/progressBarStyleSmall"
- />
- <ImageView
- android:id="@+id/pull_to_refresh_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:visibility="gone"
- android:layout_gravity="center"
- android:layout_marginLeft="15dp"
- android:gravity="center"
- android:src="@drawable/ic_pulltorefresh_arrow"
- />
- <TextView
- android:id="@+id/pull_to_refresh_text"
- android:text="@string/pull_to_refresh_tap_label"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:textStyle="bold"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:textColor="#000000"
- android:gravity="center"
- />
- <TextView
- android:id="@+id/pull_to_refresh_updated_at"
- android:layout_below="@+id/pull_to_refresh_text"
- android:visibility="gone"
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:textColor="#000000"
- android:gravity="center"
- />
- </LinearLayout>
-</FrameLayout>
View
5 res/values/attr.xml
@@ -77,11 +77,6 @@
<enum name="step" value="0x01" />
<enum name="none" value="0x00" />
</attr>
- </declare-styleable>
- <declare-styleable name="PullToRefreshListView">
- <attr name="textColor" format="color"/>
- <attr name="subTextColor" format="color"/>
- <attr name="headerBackgroundColor" format="color"/>
</declare-styleable>
<declare-styleable name="XUITitleBar">
<!-- Sets the text of the titlebar -->
View
5 res/values/strings.xml
@@ -1,8 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
- <string name="app_name">pulltorefresh</string>
- <string name="pull_to_refresh_pull_label">Pull to refresh...</string>
- <string name="pull_to_refresh_release_label">Release to refresh...</string>
- <string name="pull_to_refresh_refreshing_label">Loading...</string>
- <string name="pull_to_refresh_tap_label">Tap to refresh...</string>
</resources>
View
581 src/x/ui/XUIDragRefreshListView.java
@@ -1,581 +0,0 @@
-/**
- * @cond DEV
- * @brief x ui is the library which includes the commonly used views in 3 Sided Cube Android applications
- */
-package x.ui;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
-import android.content.Context;
-import android.media.MediaPlayer;
-import android.util.AttributeSet;
-import android.util.Log;
-import android.view.LayoutInflater;
-import android.view.MotionEvent;
-import android.view.View;
-import android.view.ViewGroup;
-import android.view.animation.LinearInterpolator;
-import android.view.animation.RotateAnimation;
-import android.widget.AbsListView;
-import android.widget.FrameLayout;
-import android.widget.ImageView;
-import android.widget.LinearLayout;
-import android.widget.ListAdapter;
-import android.widget.ListView;
-import android.widget.ProgressBar;
-import android.widget.RelativeLayout;
-import android.widget.TextView;
-import android.widget.AbsListView.OnScrollListener;
-
-/**
- * @brief The drag to refresh list view
- * Still in development
- */
-public class XUIDragRefreshListView extends ListView implements OnScrollListener
-{
- private static final int TAP_TO_REFRESH = 1;
- private static final int PULL_TO_REFRESH = 2;
- private static final int RELEASE_TO_REFRESH = 3;
- private static final int REFRESHING = 4;
-
- private static final String TAG = "PullToRefreshListView";
- private OnRefreshListener mOnRefreshListener;
- private LayoutInflater mInflater;
- private Context mContext;
-
- private FrameLayout mRefreshView;
- private RelativeLayout mFooterView;
- private TextView mRefreshViewText;
- private ImageView mRefreshViewImage;
- private ProgressBar mRefreshViewProgress;
- private TextView mRefreshViewLastUpdated;
-
- private int mCurrentScrollState;
- private int mRefreshState;
-
- private RotateAnimation mFlipAnimation;
- private RotateAnimation mReverseFlipAnimation;
-
- private int mRefreshViewHeight;
- private int mRefreshOriginalTopPadding;
- private int mLastMotionY;
-
- /**
- * Default Constructor
- * @param context The application's context
- */
- public XUIDragRefreshListView(Context context)
- {
- super(context);
- init(context);
- }
-
- /**
- * Default constructor
- * @param context The context of the application/activity
- * @param attrs The attribute set gathered from the XML
- */
- public XUIDragRefreshListView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- init(context);
- }
-
- /**
- * Default Constructor
- * @param context The application's context
- * @param attrs The attribute set passed via the SAX parser
- * @param defStyle
- */
- public XUIDragRefreshListView(Context context, AttributeSet attrs, int defStyle)
- {
- super(context, attrs, defStyle);
- init(context);
- }
-
- private void init(Context context)
- {
- mContext = context;
-
- // Load all of the animations we need in code rather than through XML
- mFlipAnimation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
- mFlipAnimation.setInterpolator(new LinearInterpolator());
- mFlipAnimation.setDuration(250);
- mFlipAnimation.setFillAfter(true);
- mReverseFlipAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
- mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
- mReverseFlipAnimation.setDuration(250);
- mReverseFlipAnimation.setFillAfter(true);
-
- mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
- mRefreshView = (FrameLayout)mInflater.inflate(R.layout.pull_to_refresh_header, null);
-
- mRefreshViewText = (TextView)mRefreshView.findViewById(R.id.pull_to_refresh_text);
- mRefreshViewImage = (ImageView)mRefreshView.findViewById(R.id.pull_to_refresh_image);
- mRefreshViewProgress = (ProgressBar)mRefreshView.findViewById(R.id.pull_to_refresh_progress);
- mRefreshViewLastUpdated = (TextView)mRefreshView.findViewById(R.id.pull_to_refresh_updated_at);
-
- mRefreshViewImage.setMinimumHeight(50);
- mRefreshView.setOnClickListener(new OnClickRefreshListener());
- mRefreshOriginalTopPadding = mRefreshView.getPaddingTop();
-
- mRefreshState = TAP_TO_REFRESH;
-
- mRefreshView.setBackgroundDrawable(getBackground());
-
- addHeaderView(mRefreshView);
-
- LinearLayout t = (LinearLayout)mRefreshView.findViewById(R.id.header);
- //t.setVisibility(View.GONE);
-
- mFooterView = new RelativeLayout(context);
- mFooterView.setLayoutParams(new LayoutParams(-1, -1));
- //mFooterView.setPadding(0, 800 + mRefreshView.getHeight(), 0, 0);
- mFooterView.setBackgroundDrawable(getBackground());
-
- TextView a = new TextView(context);
- a.setText("");
-
- mFooterView.addView(a);
-
- addFooterView(mFooterView);
-
- setOnScrollListener(this);
-
- measureView(mRefreshView);
- mRefreshViewHeight = mRefreshView.getMeasuredHeight();
- }
-
- @Override
- protected void onAttachedToWindow()
- {
- setSelection(1);
- }
-
- @Override
- public void setAdapter(ListAdapter adapter)
- {
- super.setAdapter(adapter);
-
- if (adapter.getCount() > 7)
- {
- mFooterView.setPadding(0, 0, 0, 0);
- setVerticalScrollBarEnabled(true);
- }
- else
- {
- setVerticalScrollBarEnabled(false);
- }
-
- setSelection(1);
- }
-
- /**
- * Register a callback to be invoked when this list should be refreshed.
- *
- * @param onRefreshListener The callback to run.
- */
- public void setOnRefreshListener(OnRefreshListener onRefreshListener)
- {
- mOnRefreshListener = onRefreshListener;
- }
-
- /**
- * Set a text to represent when the list was last updated.
- *
- * @param lastUpdated Last updated at.
- */
- public void setLastUpdated(CharSequence lastUpdated)
- {
- if (lastUpdated != null)
- {
- mRefreshViewLastUpdated.setVisibility(View.VISIBLE);
- mRefreshViewLastUpdated.setText(lastUpdated);
- }
- else
- {
- mRefreshViewLastUpdated.setVisibility(View.GONE);
- }
- }
-
- /**
- * Smoothly scroll by distance pixels over duration milliseconds.
- *
- * Using reflection internally to call smoothScrollBy for API Level 8
- * otherwise scrollBy is called.
- *
- * @param distance Distance to scroll in pixels.
- * @param duration Duration of the scroll animation in milliseconds.
- */
- private void scrollListBy(int distance, int duration)
- {
- try
- {
- Method method = ListView.class.getMethod("smoothScrollBy", Integer.TYPE, Integer.TYPE);
- method.invoke(this, distance + 1, duration);
- }
- catch (NoSuchMethodException e)
- {
- // If smoothScrollBy is not available (< 2.2)
- setSelection(1);
- }
- catch (IllegalArgumentException e)
- {
- throw e;
- }
- catch (IllegalAccessException e)
- {
- System.err.println("unexpected " + e);
- }
- catch (InvocationTargetException e)
- {
- System.err.println("unexpected " + e);
- }
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event)
- {
- final int y = (int) event.getY();
-
- switch (event.getAction())
- {
- case MotionEvent.ACTION_UP:
- {
- if (!isVerticalScrollBarEnabled())
- {
- setVerticalScrollBarEnabled(true);
- }
-
- if (getFirstVisiblePosition() == 0 && mRefreshState != REFRESHING)
- {
- if (mRefreshView.getBottom() > mRefreshViewHeight || mRefreshView.getTop() >= 0 && mRefreshState == RELEASE_TO_REFRESH)
- {
- // Initiate the refresh
- mRefreshState = REFRESHING;
- prepareForRefresh();
- onRefresh();
- }
- else if (mRefreshView.getBottom() < mRefreshViewHeight)
- {
- // Abort refresh and scroll down below the refresh view
- resetHeader();
- setSelection(1);
- }
- }
-
- if (getAdapter() != null)
- {
- // We want to see if the user has scrolled inside the footer, if they have then
- // rebound back to a view full of items
- if (getFirstVisiblePosition() + 1 == getAdapter().getCount())
- {
- //
- try
- {
- // smoothScrollToPosition(getAdapter().getCount() - 1);
- }
- catch (NoSuchMethodError e)
- {
- // setSelection(getAdapter().getCount() - 1);
- }
- }
- }
-
- break;
- }
- case MotionEvent.ACTION_DOWN:
- {
- mLastMotionY = y;
-
- break;
- }
- case MotionEvent.ACTION_MOVE:
- {
- applyHeaderPadding(event);
-
- break;
- }
- }
-
- return super.onTouchEvent(event);
- }
-
- private void applyHeaderPadding(MotionEvent ev)
- {
- //if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO) return;
-
- final int historySize = ev.getHistorySize();
-
- // Workaround for getPointerCount() which is unavailable in 1.5
- // (it's always 1 in 1.5)
- int pointerCount = 1;
-
- try
- {
- Method method = MotionEvent.class.getMethod("getPointerCount");
- pointerCount = (Integer)method.invoke(ev);
- }
- catch (NoSuchMethodException e)
- {
- pointerCount = 1;
- }
- catch (IllegalArgumentException e)
- {
- throw e;
- }
- catch (IllegalAccessException e)
- {
- System.err.println("unexpected " + e);
- }
- catch (InvocationTargetException e)
- {
- System.err.println("unexpected " + e);
- }
-
- for (int h = 0; h < historySize; h++)
- {
- for (int p = 0; p < pointerCount; p++)
- {
- if (mRefreshState == RELEASE_TO_REFRESH)
- {
- if (isVerticalFadingEdgeEnabled())
- {
- setVerticalScrollBarEnabled(false);
- }
-
- int historicalY = 0;
- try
- {
- // For Android > 2.0
- Method method = MotionEvent.class.getMethod("getHistoricalY", Integer.TYPE, Integer.TYPE);
- historicalY = ((Float) method.invoke(ev, p, h)).intValue();
- }
- catch (NoSuchMethodException e)
- {
- // For Android < 2.0
- historicalY = (int) (ev.getHistoricalY(h));
- }
- catch (IllegalArgumentException e)
- {
- throw e;
- }
- catch (IllegalAccessException e)
- {
- System.err.println("unexpected " + e);
- }
- catch (InvocationTargetException e)
- {
- System.err.println("unexpected " + e);
- }
-
- // Calculate the padding to apply, we divide by 1.7 to
- // simulate a more resistant effect during pull.
- int topPadding = (int) (((historicalY - mLastMotionY) - mRefreshViewHeight) / 1.7);
-
- mRefreshView.setPadding(mRefreshView.getPaddingLeft(), topPadding, mRefreshView.getPaddingRight(), mRefreshView.getPaddingBottom());
- }
- }
- }
- }
-
- /**
- * 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(R.string.pull_to_refresh_tap_label);
- // Replace refresh drawable with arrow drawable
- mRefreshViewImage.setImageResource(R.drawable.pull_arrow);
- // 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);
- }
-
- 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)
- {
- MediaPlayer mp = MediaPlayer.create(mContext, R.raw.psst1);
- mp.start();
-
- mRefreshViewText.setText(R.string.pull_to_refresh_release_label);
- mRefreshViewImage.clearAnimation();
- mRefreshViewImage.startAnimation(mFlipAnimation);
- mRefreshState = RELEASE_TO_REFRESH;
- }
- else if (mRefreshView.getBottom() < mRefreshViewHeight + 20 && mRefreshState != PULL_TO_REFRESH)
- {
- MediaPlayer mp = MediaPlayer.create(mContext, R.raw.psst2);
- mp.start();
-
- mRefreshViewText.setText(R.string.pull_to_refresh_pull_label);
- 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)))// || (firstVisibleItem + 1 == getAdapter().getCount())))
- {
- setSelection(1);
- }
- }
-
- public void onScrollStateChanged(AbsListView view, int scrollState)
- {
- mCurrentScrollState = 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);
-
- mRefreshState = REFRESHING;
- }
-
- public void onRefresh()
- {
- Log.d(TAG, "onRefresh");
-
- if (mOnRefreshListener != null)
- {
- MediaPlayer mp = MediaPlayer.create(mContext, R.raw.pop);
- mp.start();
-
- 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(true);
- }
-
- /**
- * Resets the list to a normal state after a refresh.
- */
- public void onRefreshComplete(boolean setToTop)
- {
- Log.d(TAG, "onRefreshComplete");
- if (setToTop)
- {
- resetHeader();
-
- // If refresh view is visible when loading completes, scroll down to
- // the next item.
- if (mRefreshView.getBottom() > 0)
- {
- invalidateViews();
- setSelection(1);
- }
- }
- }
-
- /**
- * @brief 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
- {
- public void onClick(View v)
- {
- if (mRefreshState != REFRESHING)
- {
- prepareForRefresh();
- onRefresh();
- }
- }
- }
-
- /**
- * @brief Interface definition for a callback to be invoked when list should be
- * refreshed.
- */
- public interface OnRefreshListener
- {
- /**
- * Called when the list should be refreshed.
- * A call to {@link PullToRefreshListView #onRefreshComplete()} is
- * expected to indicate that the refresh has completed.
- */
- public void onRefresh();
- }
-}
-/**
- * @endcond
- */

0 comments on commit 32b5a2f

Please sign in to comment.