@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { handler = new Handler(); rootView = inflater.inflate(R.layout.fragment_notifications, container, false); viewContext = rootView.getContext(); subsView = (ListView) rootView.findViewById(R.id.notificationList); swipeContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeNotificationsContainer); swipeContainer.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { // Your code to refresh the list here. // Make sure you call swipeContainer.setRefreshing(false) // once the network request has completed successfully. handler.post(refreshing); } }); // Configure the refreshing colors swipeContainer.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); subsView.setOnScrollListener( new ListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) {} @Override public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { boolean enable = false; if (subsView != null && subsView.getChildCount() > 0) { // check if the first item of the list is visible boolean firstItemVisible = subsView.getFirstVisiblePosition() == 0; // check if the top of the first item is visible boolean topOfFirstItemVisible = subsView.getChildAt(0).getTop() == 0; // enabling or disabling the refresh layout enable = firstItemVisible && topOfFirstItemVisible; } swipeContainer.setEnabled(enable); } }); displayMessages(); return rootView; }
public void run() { try { displayMessages(); // Toast.makeText(mContext, "Refreshed!", Toast.LENGTH_SHORT).show(); swipeContainer.setRefreshing(false); } catch (Exception e) { e.printStackTrace(); } }