I have researched how to start the Android SwipeRefreshLayout programmatically.
I have found that website where it is explained how to do that.
Here is the code how I start the animation:
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
}
});
BNow I would like to stop the animation of the SwipeRefreshLayout when the data is loaded.
The code
mSwipeRefreshLayout.setRefreshing(false);
does not work for me.
Has anyone an idea ?
mSwipeRefreshLayout.setRefreshing(true); does not appear to trigger the onRefresh() listener. It only shows the animation. I'm assuming you are calling mSwipeRefreshLayout.setRefreshing(false); as a result of something that happens in your refresh handler.
The solution is to call your refresh method manually from within the Runnable.
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
myRefreshMethod();
}
});
Related
When I swipe up to refresh my ListView, I perform a click using performClick(), but the wheel freezes until everything that's under the onClickListener is executed.
btini.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Code here
refreshlistmain.setRefreshing(false);
refreshlistmain.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
btini.performClick();
}
}
);
Since the code under onClick() is quite long, the wheel stays frozen for a couple of seconds before disappearing.
How do I fix that?
This code arrangement is wrong. It a recursive call. On each click a new SwipeRefreshLayout.OnRefreshListener will be registered. Just use it like below . Perform long running task and then at last call refreshlistmain.setRefreshing(false);. If long running task is on worker thread then you need to wait for it to complete.
btini.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do stuff
refreshlistmain.setRefreshing(false);
}
});
refreshlistmain.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
btini.performClick();
}
}
);
This is because you have some long task on UI thread. It will not freeze not only SwipeToRefreshLayout but also any animation or ProgressBar you have (got that).
Solution
Put your task (which you are doing after button click) in AsyncTask or different thread. Which will run Asynchronously.
I am trying to remove item from list and notifyItemRemoved() method it is not working it gives array index out of bound exception by removing last item and if I remove item from middle its also remove very next item to it but if put the code outside the thread its working fine
there is my code
Runnable runnable = new Runnable() {
#Override
public void run() {
int progress = 0;
ArrayList<MediaFileObject> selection = localDatabase.getAllMediaObjectsOfID(list.get(selectedAlbum).getId());
for (MediaFileObject obj : selection) {
if (searializeableTasks.isCancelled())
break;
localDatabase.moveMediaObjectToTrash(obj);
progress++;
if (searializeableTasks.isCancelled())
sendProgressToHandle(-101);
else
sendProgressToHandle(progress);
}
if (!searializeableTasks.isCancelled())
localDatabase.deleteAlbum(list.get(selectedAlbum).getId());
Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
list.remove(selectedAlbum);
savedAlbumAdapter.notifyItemRemoved(selectedAlbum);
}
});
}
};
searializeableTasks.startTasks(localDatabase.getAllMediaObjectsOfID(list.get(selectedAlbum).getId()).size(), runnable);
but if put all this code inside main thread(Class) its working fine as expected I am putting my code here I want to show my custom progress dialog there for after completion I want to update my RecyclerView can someone please help me. What is wrong in my code? or Why it is not working in separate thread?
Call notify on main thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
savedAlbumAdapter.notifyItemRemoved(selectedAlbum);
}
});
Or
yourrecyclerView.post(new Runnable() {
#Override
public void run() {
savedAlbumAdapter.notifyItemRemoved(selectedAlbum);
}
});
use only
notifyDataSetChanged();
It will work.If you use notifyItemRemoved() after remove() then it would again try to remove next position , so Exception arises.
Is it possible to use only SwipeRefreshLayout loading animation without really refreshing view i.e on initial loading lot's of data?
This animation:
I want to show swipe animation before any view is show, as preloader for view.
Tried different combinations on wiew create, when I use
swipeRefreshLayout.setRefreshing(true);
and
swipeRefreshLayout.setRefreshing(false);
Thing works fine, but my swipe listener then doesn't work, it doesn't even catches event.
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
SomeCustomFragment.refreshFragment();
swipeRefreshLayout.setRefreshing(false);
}
});
Is that I'm trying to do even possible?
You can try this:
refreshLayout.post(new Runnable() {
#Override
public void run() {
refreshLayout.setRefreshing(true);
// load data
.....
}
});
Try with below code to trigger the swipe refresh layout programmatically.
swipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
}
});
By calling directly swipeRefreshLayout.setRefreshing(true) the OnRefreshListener will NOT get executed.
In order to stop the circular loading animation call swipeRefreshLayout.setRefreshing(false)
Views will be rendered on the screen after the onCreate() method. So you need to have a callback function that executes after the onCreate() method.
Something like below will do the trick.
swipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
//Do your stuff here
}
});
Remember to set the animation off by setting
swipeRefreshLayout.setRefreshing(false)
As soon as you're done with the task
I have an activity class where when a button is clicked i calls a function which updates some data. Below is the function
"startDIImport"
This function internally uses Thread.sleep(10000) to wait for another task to be completed.
Hence while this function is in progress i am planning to show a progress bar.
"showProgress" is the function which shows the progress bar.
I have written below code to show progress bar while my task runs in oncreate function
new Thread(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
showProgress();
}
});
}
}).start();
new Thread(new Runnable() {
#Override
public void run() {
startDIImport();
}
}).start();
But my progressbar hangs when i click the button. Can you please help in resolving this issue.
I have checked AsyncTask but came to know that this has to be used only if the background task runs for few seconds. My background task may run to few hours.
Can you please let me know the best solution to handle this.
Use this code to run code on the ui thread. Also, you should never Thread.sleep(), use a proper callback mechanism instead. Show more code, it's hard to see what you're trying to do.
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
showProgress();
}
});
I am having a problem trying to refresh a View in an Android application. I have a button that have a image and what I need to do is to change the image when someone clicked the button.
Where is the problem? The image don't refresh until the activity finished proccessing the code. Any idea how I can refresh the image as soon as It execute the instruction
buttton1.setBackgroundDrawable(getResources().getDrawable(R.drawable.f1));
Have you considered using the xml side and have the drawables as selectors as then the selectors will get chosen by the particular key/touch event to display the correct graphic..
Try running your method that does your processing from a thread.
ficha1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button bot = (Button) findViewById(R.id.boton1);
bot.setBackgroundDrawable(getResources().getDrawable(R.drawable.f2));
//ficha.setText(fichas.get("boton1").toString());
new Thread(
new Runnable() {
public void run() {
controlJugada(fichas.get("boton1").toString(), bot);
}
}
).start();
}
});
I solved a similar problem by putting the offending code in a post delayed handler with a zero delay.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// code slowing things down here
}
}, 0);
The first thing you do in the onclick listner is change the backgrount of the button