Skip to content

Add the hide header #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 44 additions & 74 deletions pulltorefresh/res/layout/pull_to_refresh_header.xml
Original file line number Diff line number Diff line change
@@ -1,75 +1,45 @@
<?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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="10dip"
android:paddingBottom="15dip"
android:gravity="center_vertical|center_horizontal"
android:background="@drawable/pull_to_refresh_header_background"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
>
<ProgressBar
android:id="@+id/pull_to_refresh_progress"
android:indeterminate="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="20dip"
android:layout_marginTop="10dip"
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:layout_marginLeft="30dip"
android:layout_marginRight="20dip"
android:visibility="gone"
android:layout_gravity="center"
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:paddingTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
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:gravity="center"
/>
</RelativeLayout>
</LinearLayout>
<!-- 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:background="@drawable/pull_to_refresh_header_background">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:paddingTop="10dip" android:paddingBottom="15dip"
android:gravity="center_vertical|center_horizontal"
android:id="@+id/pull_to_refresh_view"
android:background="@drawable/pull_to_refresh_header_background">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal">
<ProgressBar android:id="@+id/pull_to_refresh_progress"
android:indeterminate="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="30dip"
android:layout_marginRight="20dip" android:layout_marginTop="10dip"
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:layout_marginLeft="30dip" android:layout_marginRight="20dip"
android:visibility="gone" android:layout_gravity="center"
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:paddingTop="5dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_gravity="center" 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:gravity="center" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
Expand All @@ -35,21 +34,25 @@ public class PullToRefreshListView extends ListView implements OnScrollListener
private OnRefreshListener mOnRefreshListener;
private LayoutInflater mInflater;

private LinearLayout mRefreshView;
private View mRefreshView;
private TextView mRefreshViewText;
private ImageView mRefreshViewImage;
private ProgressBar mRefreshViewProgress;
private TextView mRefreshViewLastUpdated;
private View mRefreshInnerView;

private int mCurrentScrollState;
private int mRefreshState;

private boolean hideHeader;

private RotateAnimation mFlipAnimation;
private RotateAnimation mReverseFlipAnimation;

private int mRefreshViewHeight;
private int mRefreshOriginalTopPadding;
private int mLastMotionY;


public PullToRefreshListView(Context context) {
super(context);
Expand All @@ -67,6 +70,10 @@ public PullToRefreshListView(Context context, AttributeSet attrs, int defStyle)
}

private void init(Context context) {

//Setting the header to hidden
setHeaderToHidden(true);

// Load all of the animations we need in code rather than through XML
mFlipAnimation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
Expand All @@ -84,8 +91,10 @@ private void init(Context context) {
mInflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);

mRefreshView = (LinearLayout) mInflater.inflate(
mRefreshView = (View) mInflater.inflate(
R.layout.pull_to_refresh_header, null);

mRefreshInnerView = (View)mRefreshView.findViewById(R.id.pull_to_refresh_view);

mRefreshViewText =
(TextView) mRefreshView.findViewById(R.id.pull_to_refresh_text);
Expand All @@ -108,6 +117,11 @@ private void init(Context context) {

measureView(mRefreshView);
mRefreshViewHeight = mRefreshView.getMeasuredHeight();

if (hideHeader){
mRefreshInnerView.setVisibility(View.GONE);
}

}

@Override
Expand All @@ -118,9 +132,20 @@ protected void onAttachedToWindow() {
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);

setSelection(1);
}

@Override
public void onSizeChanged(int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
setSelection(1);
}

@Override
public void onWindowFocusChanged(boolean hasWindowFocus){
super.onWindowFocusChanged(hasWindowFocus);
this.setSelection(1);
}

/**
* Register a callback to be invoked when this list should be refreshed.
Expand Down Expand Up @@ -173,15 +198,13 @@ private void scrollListBy(int distance, int duration) {
@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
if (getFirstVisiblePosition() == 0 && mRefreshState != REFRESHING) {
if (mRefreshView.getBottom() >= mRefreshViewHeight
|| mRefreshView.getTop() >= 0
&& mRefreshState == RELEASE_TO_REFRESH) {
// Initiate the refresh
Expand Down Expand Up @@ -291,8 +314,14 @@ private void resetHeader() {
mRefreshViewImage.clearAnimation();
// Hide progress bar and arrow.
mRefreshViewImage.setVisibility(View.GONE);
mRefreshViewProgress.setVisibility(View.GONE);
mRefreshViewProgress.setVisibility(View.GONE);

}

if (hideHeader){
mRefreshInnerView.setVisibility(View.GONE);
}

}

private void measureView(View child) {
Expand All @@ -318,20 +347,24 @@ private void measureView(View child) {
@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) {
&& mRefreshState != REFRESHING) {
if (firstVisibleItem == 0) {
if (mRefreshInnerView.getVisibility()==View.GONE){
mRefreshInnerView.setVisibility(View.VISIBLE);
}
mRefreshViewImage.setVisibility(View.VISIBLE);
if ((mRefreshView.getBottom() > mRefreshViewHeight + 20
|| mRefreshView.getTop() >= 0)
&& mRefreshState != RELEASE_TO_REFRESH) {
&& mRefreshState != RELEASE_TO_REFRESH) {
mRefreshViewText.setText(R.string.pull_to_refresh_release_label);
mRefreshViewImage.clearAnimation();
mRefreshViewImage.startAnimation(mFlipAnimation);
mRefreshState = RELEASE_TO_REFRESH;
} else if (mRefreshView.getBottom() < mRefreshViewHeight + 20
} else if (mRefreshView.getBottom() < mRefreshViewHeight + 7
&& mRefreshState != PULL_TO_REFRESH) {
mRefreshViewText.setText(R.string.pull_to_refresh_pull_label);
if (mRefreshState != TAP_TO_REFRESH) {
Expand All @@ -357,8 +390,8 @@ public void onScrollStateChanged(AbsListView view, int scrollState) {
}

public void prepareForRefresh() {

resetHeaderPadding();

mRefreshViewImage.setVisibility(View.GONE);
// We need this hack, otherwise it will keep the previous drawable.
mRefreshViewImage.setImageDrawable(null);
Expand Down Expand Up @@ -433,4 +466,13 @@ public interface OnRefreshListener {
*/
public void onRefresh();
}

/**
* Method that sets flag to determine if header is to be hidden in an empty list or list with
* elements less than the visible elements
* @param isHidden
*/
public void setHeaderToHidden(boolean isHidden){
hideHeader = isHidden;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PullToRefreshActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pull_to_refresh);

// Set a listener to be invoked when the list should be refreshed.
((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
@Override
Expand All @@ -28,7 +28,6 @@ public void onRefresh() {
new GetDataTask().execute();
}
});

mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));

Expand Down Expand Up @@ -67,4 +66,8 @@ protected void onPostExecute(String[] result) {
"Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
"Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
"Allgauer Emmentaler"};

// private String[] mStrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", };

/* private String[] mStrings = {};*/
}