package com.marshalchen.common.usefulModule; 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 android.widget.RelativeLayout; import com.marshalchen.common.R; /** * An abstract activity which contains SwipeRefresh and load more. * You can simply extend it like an activity */ public abstract class AbstractSwipeRefreshLoadMoreActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener { protected SwipeRefreshLayout refreshLayout; protected RelativeLayout swipeReaLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.swipe_refresh_list_more_layout); refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container); swipeReaLayout = (RelativeLayout) findViewById(R.id.swipeReaLayout); } @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) { swipeReaLayout.addView(view, params); initSwipeOptions(); } private void initSwipeOptions() { refreshLayout.setOnRefreshListener(this); setAppearance(); disableSwipe(); } private void setAppearance() { refreshLayout.setColorSchemeResources(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 } }