Skip to content

Instantly share code, notes, and snippets.

@antoniolg
Last active July 29, 2019 11:59
Show Gist options
  • Save antoniolg/9837398 to your computer and use it in GitHub Desktop.
Save antoniolg/9837398 to your computer and use it in GitHub Desktop.
An activity that uses a SwipeRefreshLayout as a container for the layout of the classes that extend it.
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
/*
* Copyright (C) 2014 Antonio Leiva Gordillo.
*
* 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.
*/
package com.antonioleiva.swiperefreshlayoutexample.app.R;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import com.antonioleiva.swiperefreshlayoutexample.app.R;
public abstract class SwipeRefreshActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout refreshLayout;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_swipe_refresh);
refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
}
@Override public void setContentView(int layoutResID) {
View v = getLayoutInflater().inflate(layoutResID, refreshLayout, false);
setContentView(v);
}
@Override public void setContentView(View view) {
setContentView(view, view.getLayoutParams());
}
@Override public void setContentView(View view, ViewGroup.LayoutParams params) {
refreshLayout.addView(view, params);
initSwipeOptions();
}
private void initSwipeOptions() {
refreshLayout.setOnRefreshListener(this);
setAppearance();
disableSwipe();
}
private void setAppearance() {
refreshLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
/**
* It shows the SwipeRefreshLayout progress
*/
public void showSwipeProgress() {
refreshLayout.setRefreshing(true);
}
/**
* It shows the SwipeRefreshLayout progress
*/
public void hideSwipeProgress() {
refreshLayout.setRefreshing(false);
}
/**
* Enables swipe gesture
*/
public void enableSwipe() {
refreshLayout.setEnabled(true);
}
/**
* Disables swipe gesture. It prevents manual gestures but keeps the option tu show
* refreshing programatically.
*/
public void disableSwipe() {
refreshLayout.setEnabled(false);
}
/**
* It must be overriden by parent classes if manual swipe is enabled.
*/
@Override public void onRefresh() {
// Empty implementation
}
}
@KarthickCk
Copy link

swipe refresh is not working,when i set empty view for list view which is child of swipe refresh layout

@FabianFrank
Copy link

Swipe to refresh only works when the view that is set using ListView.setEmptyView() is not clickable, by setting android:clickable="false"

@pranavlathigara
Copy link

getting error........
java.lang.InstantiationException: can't instantiate class com.example.SwipeRefreshActivity

@JerabekJakub
Copy link

Same here: "can't instantiate class..."

@shkschneider
Copy link

Yeah, this snippet was great!
Until I found myself using a ListView as sole View inside it... I now cannot scroll up: it refreshes...

@shkschneider
Copy link

Found a solution to my above problem: http://nlopez.io/swiperefreshlayout-with-listview-done-right/
Hope it helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment