Reputation: 588
SwipeRefresh not working in fragment. My xml code:
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pagerefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" /></android.support.v4.widget.SwipeRefreshLayout>
My fragmnet code:
View drawer = inflater.inflate(R.layout.fragment_progress, container, false);
swipeRefreshLayout = (SwipeRefreshLayout) drawer.findViewById(R.id.pagerefresh);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
getProgressData();
}
});
swipeRefreshLayout.setColorSchemeColors(android.R.color.holo_green_dark,
android.R.color.holo_red_dark,
android.R.color.holo_blue_dark,
android.R.color.holo_orange_dark); adapter = new ProgressOrderListAdapter(orderLists, getActivity());
recyclerView.setAdapter(adapter);
return recyclerView;
getProgressData() function & it's more helpfull for you
private void getProgressData(){
String token = SharedPreferencesManager.readPreferenceString("token", "D/N");
JSONObject progressData = new JSONObject();
try{
progressData.put("token", token);
JsonObjectRequest progressObject = new JsonObjectRequest(1, Common.OrderDetails + "progress", progressData, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject progressResponse) {
Log.d("Responseprogress", progressResponse.toString());
try {
int status = progressResponse.getInt("status");
if(status == 1) {
progressOrderProgress(progressResponse);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("Response", "PROGRESS ERROR");
}
});
progressObject.setShouldCache(false);
ServiceBellApp.getInstance().addToRequestQueue(progressObject);
}
catch (JSONException localJSONException){
localJSONException.printStackTrace();
return;
}
}
private void progressOrderProgress(JSONObject progressResponse) throws JSONException {
JSONArray result = progressResponse.getJSONArray("orderdata");
for(int i=0; i<result.length(); i++){
OrderList orderListModule = new OrderList();
JSONObject orderData = null;
try {
orderData = result.getJSONObject(i);
orderListModule.setPackage_name(orderData.getString("name"));
orderListModule.setOrderdate(orderData.getString("date"));
orderListModule.setServicedate(orderData.getString("service"));
orderListModule.setServicetime(orderData.getString("time"));
orderListModule.setOrderid(orderData.getString("id"));
orderListModule.setOrdstatus(orderData.getString("status"));
orderListModule.setOrderamount(orderData.getInt("ramount"));
}catch (JSONException e) {
e.printStackTrace();
}
orderLists.add(orderListModule);
}
swipeRefreshLayout.setRefreshing(false);
adapter.notifyDataSetChanged();
}
This is my code. I am not get any error in logcat, but it's not working. Please help me friends
Upvotes: 4
Views: 4911
Reputation: 574
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefresh);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mArrayList = new ArrayList<CabsETA>();
mSwipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
mSwipeLayout = true;
mSwipeRefreshLayout.setRefreshing(true);
getNear();
}
}
);
}
});
public void getNear() {
latLongSevenKms();
mSwipeRefreshLayout.setRefreshing(true);
}
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topLayout">
<android.support.v7.widget.RecyclerView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_gravity="start"
android:layout_marginTop="8dp"
android:background="#f5f5f5"
android:choiceMode="singleChoice"
android:divider="@null"
android:dividerHeight="0dp" />
</android.support.v4.widget.SwipeRefreshLayout>
Use:
LinearLayoutManager mm = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mm);
Upvotes: 3
Reputation: 8149
Understand this basic flow what is see in your code you have done all thig just one thing is missed.
you made request get new data and then add it to orderLists.add(orderListModule);
and then call adapter.notifyDataSetChanged();
here is issue you should call here set adatpter with your new dataset instead of notifyDataSetChanged()
.
@Override
public void onCreate(Bundle savedInstanceState) {
...
listView.setAdapter();
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshContent();
...
}
// fake a network operation's delayed response
// this is just for demonstration, not real code!
private void refreshContent(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getNewTweets());
mListView.setAdapter(mAdapter);
mSwipeRefreshLayout.setRefreshing(false);
});
}
// get new cat names.
// Normally this would be a call to a webservice using async task,
// or a database operation
private List<String> getNewCatNames() {
List<String> newCatNames = new ArrayList<String>();
for (int i = 0; i < mCatNames.size(); i++) {
int randomCatNameIndex = new Random().nextInt(mCatNames.size() - 1);
newCatNames.add(mCatNames.get(randomCatNameIndex));
}
return newCatNames;
}
Update: in your code
private void progressOrderProgress(JSONObject progressResponse) throws JSONException {
JSONArray result = progressResponse.getJSONArray("orderdata");
for(int i=0; i<result.length(); i++){
OrderList orderListModule = new OrderList();
JSONObject orderData = null;
try {
orderData = result.getJSONObject(i);
orderListModule.setPackage_name(orderData.getString("name"));
orderListModule.setOrderdate(orderData.getString("date"));
orderListModule.setServicedate(orderData.getString("service"));
orderListModule.setServicetime(orderData.getString("time"));
orderListModule.setOrderid(orderData.getString("id"));
orderListModule.setOrdstatus(orderData.getString("status"));
orderListModule.setOrderamount(orderData.getInt("ramount"));
}catch (JSONException e) {
e.printStackTrace();
}
orderLists.add(orderListModule);
}
swipeRefreshLayout.setRefreshing(false);
adapter = new ProgressOrderListAdapter(orderLists, getActivity());
recyclerView.setAdapter(adapter);
}
Upvotes: 2