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
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'm populating a GridView wth an AsyncTask:
#Override
public void onLoad() {
super.onLoad();
//... set Loading view and actually get the data
adapter = new DepartmentGridAdapter(getActivity(), R.layout.gird_department_item_layout,
mSalesPresenter.getDepartments());
}
#Override
public void onDoneLoading() {
super.onDoneLoading();
gridView.setAdapter(adapter); // Populate the GridView
onShowTutorial(); // <--- This is where I need to get the firstChild.
}
After the AsyncTask is done, I just need to access the firstChild of the GridView:
#Override
public void onShowTutorial() {
if (gridView.getChildAt( gridView.getFirstVisiblePosition())!= null )
// ... doStuff
}
But the statement is Always null. I even call getCount for the GridView and it's not 0. The problem seems that the GridView childs are not accesible right away. If i Use a button to force the execution of the method onShowTutorial() after the UI is ready then I can access the first child.
I'm running out of ideas to trigger the method after the execution of the thread.
Any solution?
Try to post a runnable to be run when the gridView is done doing what it is currently doing (more info regarding View.post() here) :
#Override
public void onDoneLoading() {
super.onDoneLoading();
gridView.setAdapter(adapter); // Populate the GridView
// Tells the gridView to "queue" the following runnable
gridView.post(new Runnable() {
#Override
public void run() {
onShowTutorial(); // <--- This is where I need to get the firstChild.
}
});
}
Found it.
I used a ViewTreeObserver and it works:
gridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
onShowTutorial();
}
});
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();
}
});
I want to refresh my tile after clicking on a Download all button. I m using GridAdapter to display a grids for multiple videos. So If i click on Download all Button tile grid should show a progress bar, But currently it is not showing without refreshing the entire page.
We have tried below mentioned code on button click event but application getting crashed.....
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
File tempfile = new File(mContext.getExternalFilesDir(course_name),productCode);
if(tempfile.exists()){
viewHolder.mProgressBar.setVisibility(View.VISIBLE);
iewHolder.mImageView.setVisibility(View.INVISIBLE);
}else{
File tempthumbfile = new File(mContext.getExternalFilesDir(course_name),fname);
Log.d("tempthumbfile-->",""+tempthumbfile);
if(tempthumbfile.exists()){
viewHolder.mImageView.setBackgroundDrawable(Drawable.createFromPath(new File(mContext.getExternalFilesDir(course_name),fname).getAbsolutePath()));
}
viewHolder.mProgressBar.setVisibility(View.INVISIBLE);
viewHolder.mImageView.setVisibility(View.VISIBLE);
}
handler.postDelayed(this, 2000);
}
}, 1500);
Your question is not very specific, but try the following two approaches:
1) First and foremost, if you want to refresh a specific tile in your grid. Just call TileAdapter.notifyDataSetChanged();
2) If you are trying to e.g. blur the entire Grid and show a loading bar on top of it. Find a reference to the View hosting the grid and call view.invalidate();
try this :
Runnable task = new Runnable() {
public void run() {
this.runOnUIThread(new Runnable() {
public void run() {
// TODO: add your job/functionality here
}
});
}
}
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