title: On Scroll up SwipetoRefresh enables in android?
body: <p>I have a SwipeToRefresh when I scroll down listview and when scroll up Swipe to refresh gets enable without going on top of listview and also want to clear all data from listview and add new data it works fine but when I scroll down and send request it only show progress at bottom not adding data .How can I avoid this problem.pls let me know .</p>
  
  <p>code:-</p>
  
  <pre><code>private void getDetails() {// get details of user from shared preference...
      CLoginSessionManagement m_oSessionManagement = new CLoginSessionManagement(getActivity());// crating object of Login Session
      HashMap&lt;String, String&gt; user = m_oSessionManagement.getLoginDetails();// get String from Login Session
      m_szMobileNumber = user.get(CLoginSessionManagement.s_szKEY_MOBILE).trim();// getting password from saved preferences..........
      m_szEncryptedPassword = user.get(CLoginSessionManagement.s_szKEY_PASSWORD).trim();// getting mobile num from shared preferences...
      sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// increment of record count
      sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// increment of last count...
      s_oDataset = new ArrayList&lt;&gt;();// making object of Arraylist
  
  }
  
  private void init() {// initialize controls
  
  
      m_ProgressBar = (ProgressBar) m_Main.findViewById(R.id.progressBar1);// finding Id of progressview
      m_ProgressBar.setVisibility(View.GONE);// make profressView Invisible first time
      m_ListView = (ListView) m_Main.findViewById(R.id.dealList);// findind Id of Listview
      m_ListView.setFadingEdgeLength(0);
      /*Swipe to refresh code*/
      mSwipeRefresh = (SwipeRefreshLayout) m_Main.findViewById(R.id.activity_main_swipe_refresh_layout);
  
  
      if (NetworkUtil.isConnected(getActivity())) {
          postDealListingDatatoServer();
      } else {
  
          Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
      }
      mSwipeRefresh.setColorSchemeResources(R.color.refresh_progress_1);
  
      mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
          @Override
          public void onRefresh() {
              /*Here check net connection avialable or not */
              if (NetworkUtil.isConnected(getActivity())) {
  
                  s_oDataset.clear();
                  m_ListView.removeFooterView(mFooter);
                  m_ListView.invalidate();
                  m_n_DeafalutLastCount = 0;
                  sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
                  swipeData();
  
  
              } else {
                  Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
                  if (mSwipeRefresh.isRefreshing()) {
  
                      mSwipeRefresh.setRefreshing(false);
                  }
              }
          }
      });
      m_ListView.setOnScrollListener(new AbsListView.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 (m_ListView != null &amp;&amp; m_ListView.getChildCount() &gt; 0) {
                  // check if the first item of the list is visible
                  boolean firstItemVisible = m_ListView.getFirstVisiblePosition() == 0;
                  // check if the top of the first item is visible
                  boolean topOfFirstItemVisible = m_ListView.getChildAt(0).getTop() == 0;
                  // enabling or disabling the refresh layout
                  enable = firstItemVisible &amp;&amp; topOfFirstItemVisible;
              }
              mSwipeRefresh.setEnabled(enable);
          }
      });
      m_n_FormImage = new int[]{// defining Images in Integer array
              R.drawable.amazon,
              R.drawable.whatsapp,
              R.drawable.zorpia,
              R.drawable.path,
              R.drawable.app_me,
              R.drawable.evernote,
              R.drawable.app_me};
  
  
  }
  
      /*This is new changes in code ....using Volley instead of AsynkTask*/
  
  /*This method send request to server for deallisting*/
  // this method send request to server for deal list....
  public void postDealListingDatatoServer() {
      try {
          String json;
          // 3. build jsonObject
          JSONObject jsonObject = new JSONObject();// making object of Jsons.
          jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
          jsonObject.put("pin", m_szEncryptedPassword);// put password
          jsonObject.put("recordcount", sz_RecordCount);// put record count
          jsonObject.put("lastcountvalue", sz_LastCount);// put last count
          // 4. convert JSONObject to JSON to String
          json = jsonObject.toString();// convert Json object to string
  
          System.out.println("Request:-" + json);
          m_Dialog = DialogUtils.showProgressDialog(getActivity(), "Please wait while loading deals...");
          JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_DealListingURL, jsonObject, new Response.Listener&lt;JSONObject&gt;() {
              @Override
              public void onResponse(JSONObject response) {
                  System.out.println("Response:-" + response);
                  m_Dialog.dismiss();
                  try {
                      JSONArray posts = response.optJSONArray("dealList");// get Deal list in array from response
                      for (int i = 0; i &lt; posts.length(); i++) {// loop for counting deals from server
                          JSONObject post = posts.getJSONObject(i);// counting deal based on index
                          item = new CDealAppDatastorage();// creating object of DealAppdata storage
                          item.setM_szHeaderText(post.getString("dealname"));// get deal name from response
                          item.setM_szsubHeaderText(post.getString("dealcode"));// get dealcode from response
                          item.setM_szDealValue(post.getString("dealvalue"));// get deal value from response
                          item.setM_n_Image(m_n_FormImage[i]);//set Image Index wise(Dummy)
                          s_oDataset.add(item);// add all items in ArrayList
  
                      }
                      mFooter = View.inflate(getActivity(), R.layout.footer, null);
  
  
                      if (!s_oDataset.isEmpty()) {// condition if data in arraylist is not empty
                          // Adding Load More button to lisview at bottom
                          m_ListView.addFooterView(mFooter);// add footer in listview
                          m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
                          m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
                      } else {
                          mFooter.setVisibility(View.GONE);// else Load buttonvisibility set to Gone
                      }
                      m_ListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                          @Override
                          public void onScrollStateChanged(AbsListView view, int scrollState) {
  
  
                          }
  
                          @Override
                          public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                              if (firstVisibleItem + visibleItemCount == totalItemCount &amp;&amp; totalItemCount != 0) {
                                  if (!isLoading) {
                                      isLoading = true;
                                      if (NetworkUtil.isConnected(getActivity())) {
                                          m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data
                                          m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above
                                          String itemscount = String.valueOf(m_ListView.getAdapter().getCount());
                                          System.out.println("Toatal item:-" + itemscount);
  
                                          sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
                                          sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
                                          loadmoreData();
                                      } else {
                                          Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
                                      }
                                  }
                              } else {
                                  isLoading = false;
                              }
                          }
                      });
  
                      if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
                      } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
                          mFooter.setVisibility(View.GONE);
                      } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) {
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity());
                      }
  
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                  System.out.println("Errror:-" + error);
                  m_Dialog.dismiss();
                  if (error instanceof TimeoutError) {
                      CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "Connection lost ! Please try again", getActivity());
                  } else if (error instanceof NetworkError) {
                      CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "No internet connection", getActivity());
                  }
  
              }
          });
          RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
          requestQueue.add(jsonObjectRequest);
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
  }
  
  /*This method send request to server for more deals*/
  //this method post request to server to fetch more deal
  public void loadmoreData() {
  
      try {
          String json;
          // 3. build jsonObject
          JSONObject jsonObject = new JSONObject();// making object of Jsons.
          jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
          jsonObject.put("pin", m_szEncryptedPassword);// put password
          jsonObject.put("recordcount", sz_RecordCount);// put record count
          jsonObject.put("lastcountvalue", sz_LastCount);// put last count
          // 4. convert JSONObject to JSON to String
          json = jsonObject.toString();// convert Json object to string
  
          System.out.println("Server Request:-" + json);
          JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_DealListingURL, jsonObject, new Response.Listener&lt;JSONObject&gt;() {
              @Override
              public void onResponse(JSONObject response) {
                  System.out.println("Response:-" + response);
                  try {
                      JSONArray posts = response.optJSONArray("dealList");// GETTING DEAL LIST
                      for (int i = 0; i &lt; posts.length(); i++) {
                          JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
                          item = new CDealAppDatastorage();// object create of DealAppdatastorage
                          item.setM_szHeaderText(post.getString("dealname"));//getting deal name
                          item.setM_szsubHeaderText(post.getString("dealcode"));// getting deal code
                          item.setM_szDealValue(post.getString("dealvalue"));
  
                          if (!s_oDataset.contains(item)) {
                              s_oDataset.add(item);
                              isLoading = false;
                              m_oAdapter.notifyDataSetChanged();
                          }
                      }
                      if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
                      } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
                          mFooter.setVisibility(View.GONE);
                      } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) {
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity());
                      }
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
  
              }
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                  System.out.println("Error:-" + error);
                  if (error instanceof TimeoutError) {
                      CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "Connection lost ! Please try again", getActivity());
                  } else if (error instanceof NetworkError) {
                      CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "No internet connection", getActivity());
                  }
              }
          });
          RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
          requestQueue.add(jsonObjectRequest);
      } catch (JSONException e) {
          e.printStackTrace();
      }
  }
  
  /*This method invoke on swipe to refresh*/
  // this method invoke when swipe to refresh enable ....
  public void swipeData() {
      try {
          String json;
          // 3. build jsonObject
          JSONObject jsonObject = new JSONObject();// making object of Jsons.
          jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
          jsonObject.put("pin", m_szEncryptedPassword);// put password
          jsonObject.put("recordcount", sz_RecordCount);// put record count
          jsonObject.put("lastcountvalue", sz_LastCount);// put last count
          // 4. convert JSONObject to JSON to String
          json = jsonObject.toString();// convert Json object to string
  
          System.out.println("Server Request:-" + json);
          JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_DealListingURL, jsonObject, new Response.Listener&lt;JSONObject&gt;() {
              @Override
              public void onResponse(JSONObject response) {
                  System.out.println("Response:-" + response);
                  mSwipeRefresh.setRefreshing(false);
                  try {
                      JSONArray posts = response.optJSONArray("dealList");// get Deal list in array from response
                      for (int i = 0; i &lt; posts.length(); i++) {// loop for counting deals from server
                          JSONObject post = posts.getJSONObject(i);// counting deal based on index
                          item = new CDealAppDatastorage();// creating object of DealAppdata storage
                          item.setM_szHeaderText(post.getString("dealname"));// get deal name from response
                          item.setM_szsubHeaderText(post.getString("dealcode"));// get dealcode from response
                          item.setM_szDealValue(post.getString("dealvalue"));// get deal value from response
                          item.setM_n_Image(m_n_FormImage[i]);//set Image Index wise(Dummy)
                          s_oDataset.add(item);// add all items in ArrayList
  
                      }
                      mFooter = View.inflate(getActivity(), R.layout.footer, null);
  
  
                      if (!s_oDataset.isEmpty()) {// condition if data in arraylist is not empty
                          // Adding Load More button to lisview at bottom
                          m_ListView.addFooterView(mFooter);// add footer in listview
                          m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
                          m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
                      } else {
                          mFooter.setVisibility(View.GONE);// else Load buttonvisibility set to Gone
                      }
                      m_ListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                          @Override
                          public void onScrollStateChanged(AbsListView view, int scrollState) {
  
  
                          }
  
                          @Override
                          public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                              if (firstVisibleItem + visibleItemCount == totalItemCount &amp;&amp; totalItemCount != 0) {
                                  if (!isLoading) {
                                      isLoading = true;
                                      if (NetworkUtil.isConnected(getActivity())) {
                                          m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data
                                          m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above
                                          String itemscount = String.valueOf(m_ListView.getAdapter().getCount());
                                          System.out.println("Toatal item:-" + itemscount);
  
                                          sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
                                          sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
                                          loadmoreData();// this method load more deals from server ....
                                      } else {
                                          Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
                                      }
  
                                  }
                              }
                          }
                      });
  
                      if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
                      } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
  
                      } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) {
                          CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity());
                      }
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
  
              }
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                  System.out.println("Error:-" + error);
                  mSwipeRefresh.setRefreshing(false);
                  if (error instanceof TimeoutError) {
                      CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "Connection lost ! Please try again", getActivity());
                  } else if (error instanceof NetworkError) {
                      CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "No internet connection", getActivity());
                  }
              }
          });
          RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
          requestQueue.add(jsonObjectRequest);
  
      } catch (JSONException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>XML which contain swipe to refresh :-</p>
  
  <pre><code>&lt;android.support.v4.widget.SwipeRefreshLayout
      android:id="@+id/activity_main_swipe_refresh_layout"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context=".MainActivity"&gt;
  
      &lt;include layout="@layout/deal_list_content"/&gt;
  
  &lt;/android.support.v4.widget.SwipeRefreshLayout&gt;
  </code></pre>
  
  <p>code for :- @layout/deal_list_content</p>
  
  <pre><code>&lt;RelativeLayout
      android:id="@+id/mainLayout"
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;!----&gt;
      &lt;ListView
          android:id="@+id/dealList"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:divider="@color/transparent"
          android:dividerHeight="2dp"
          android:padding="5sp"
          android:scrollbarSize="4dp"
          android:scrollbarThumbVertical="@drawable/scrollbar"/&gt;
  
      &lt;ProgressBar
          android:id="@+id/progressBar1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerInParent="true"
          android:layout_marginBottom="10dp"&gt;
      &lt;/ProgressBar&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>