I have a tabbed activity which contains a ListFragment in each tab (Tabs with ListFragments image). In the main layout I have implemented a SwipeRefreshLayout that performs a network task and syncs some databases which both fragments rely on.
I have found that I can scroll to the bottom of my ListFragment list but I am unable to scroll back to the top of the list without causing the SwipeRefreshLayout to refresh. Is it possible to fix my activity so that it only refreshes once I reach the top of the List, and how would this be implemented?
I followed this tutorial to implement the ViewPager with Lists.
My Code-
Main XML Layout-
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
</TabHost>
</android.support.v4.widget.SwipeRefreshLayout>
MainActivity-
public class MainActivity extends ActionBarActivity implements
TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
{
// Handles ViewPager
private MyPageAdapter fPageAdapter;
private ViewPager fViewPager;
private SwipeRefreshLayout fSwipeRefreshLayout;
// Handles Tabs
private TabHost fTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fViewPager = (ViewPager)findViewById(R.id.viewpager);
// Tab Initialisation
initialiseTabHost();
// Fragments and ViewPager Initialisation
List<Fragment> fragments = getFragments();
fPageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
fViewPager.setAdapter(fPageAdapter);
fViewPager.setOnPageChangeListener(MainActivity.this);
fSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container);
fSwipeRefreshLayout.setOnRefreshListener(swipeRefreshListener);
}
/**
* Swipe Refresh Listener
*/
private SwipeRefreshLayout.OnRefreshListener swipeRefreshListener = new SwipeRefreshLayout.OnRefreshListener()
{
@Override
public void onRefresh()
{
fSwipeRefreshLayout.setRefreshing(false);
/**
* Calls Async Task to perform network
* operation and update database.
*/
}
};
/**
* Other Functions Removed
*/
/** Fragment Construction */
/**
* @brief create a list of default fragments to display
* @return List Fragments - containing default fragments
*/
private List<Fragment> getFragments()
{
String[] numbers_text = new String[] { "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen" };
String[] colours = new String[] { "RED", "BLACK", "BLUE", "WHITE", "GREEN", "BROWN",
"GREY", "YELLOW", "PINK", "PURPLE", "ORANGE", "VIOLET", "INDIGO"};
List<Fragment>fList = new ArrayList<Fragment>();
MyListFragment f1 = MyListFragment.newInstance(numbers_text);
MyListFragment f2 = MyListFragment.newInstance(colours);
fList.add(f1);
fList.add(f2);
return fList;
}
}