Calling setcontentview multiple times? - android

Can i call setcontentview multiple times if my layout is same but the resource changes.for instance if images get exchanged in 2 imageview widgets??(this is infact all that is happening in my app)

You can switch the setContentView several times. However, I have learned in practice, that UI elements don't cross over. In other words each time you set the view, you have to re-findViewById for your UI elements.

You should go for ViewFlipper. Follow this link,it may help you.
Calling setContentView() multiple times

You can "refresh" the ImageViews by calling
myImageView.invalidate();
This will make the view be redrawn. If you're using an AdapterView (such as a ListView) call
myListView.notifyDataSetChanged();

Related

UI freezes while adding many views programmatically

I have views with hundreds layouts (Linears with TextViews), basically it's like list with data.
My presenter after querying the database (sqlbrite, rxjava, its asynchronous) calling method on view which simply create linear with textviews and adds it to some other Linear (its parent for every added view)
With 100+ rows of data on my older phone there is a freeze. How can I reduce it? I can't add views on other thread than UI of course.
First thing you need to use a recycler view for list of views. and ofcoarse you can use 100 different types of views with ViewTypes of recycler view
Second if you insist not to use recycler view then you may better use AsyncTask for adding views at least. Because in some methods of AsyncTask you can access the UI thread and can add elements.
Third possibility is with event bus. you will add a method to the fragment/activity and register it with event bus. make it run in background from the tag(java tags). and call it from background thread for adding items(views)
Hope some of it solve your problem :) if not get back to me with more explanation of the question.
P.S EventBus is a library work almost the same as BroadcastReceivers but it has more features and its more efficient as well.

ViewPager Circular with less than 4 views

I am trying to implement an infinite Carousel using the ViewPager component. I based on the one created by Antonyt but there is a problem using less than 4 views. As the view is already in place. Must be a way to trick the Viewpager to draw the same view/page in different places.
I ran into the same problem, couldn't find the solution myself. But i guess it is impossible to do by using only 1-4 views. What the problem is that all the views (for 1-4 images in repetition) will be instantiated at once(or atleast in a very short interval) this interferes with the image loading process because the prev load hasn't finished, so the prev one returns a damaged view. You may handle this something like this though.
1) right after instantiation, use handler with delay of 100-200ms to flip through views setCurrentItem() again and again(around 10 times) in either direction, this way the original 4 will be out of range(if you have off screen page limit, which i hope you have beacause of infinte nature of your code) and will be created one by one later on.
2) Use multiple buffer objects,
1 image-8 buffers
2 images-4 buffers for each
3 images-2 buffers for each... something like this.
neither is an ideal solution but both worked for me but. I would like to know what you tried too.
The solution is to use the same child view in the ViewPager. And to do that without having the same child already assign to a parent is to use a ProxyView with the real view inside. Extends that Fake View from ViewGroup and override the Draw method will do the trick!
You may need to do some works in OnDestoryItem method to re-attach item which have been detached.
I have already post my solution here,https://github.com/antonyt/InfiniteViewPager/issues/2
Try the following trick which I've used successfully to make a (faux)infinite ListView.
In your adapter's getCount() method, return Integer.MAX_VALUE.
Then in the adapter's instantiateItem() or destroyItem(), Use position % datasource.size()
This trick was taken from HERE.

ListView getView() called too often

I have a ListView with custom Adapter. To be honest, I have many of them at the same time on screen, and my Tegra 3 device started to lag, what made me really confused... I found than in each ListView's Adapter the getView() method is called for all visible rows every time any animations runs on screen. That gives me like few hundreds of calls per second! Digging more, most of these calls are due to measure() and onMeasure() calls of ListViews' parents, and - this is tke key - they are useless, because all the layouts of my ListViews
have const size.
So my question is: how to eliminate these calls? Of course I want to leave proper calls alone (caused by adding items to Adapter and notifyDataSetChanged() ).
I've tried almost anything, but either the whole list doesn't draw itself (when I overriden it's onMeasure() and forced to returned const size without calling super.onMeasure()) or stops updating at some time.
How you implemented the getView() method? If you implement it in the correct way there should be nearly no lagging.
Check out this really really good video:
http://www.youtube.com/watch?v=wDBM6wVEO70
Slides: http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf
As Romain said, work with it not against it. Best is to leave measure() alone and focus on your adapter.
Thats how ListView is implemented.. I don't think that will cause a performance Overhead.. Provided you do things properly there..
For example..
Don't instanciate LayoutInflator inside GetView Method, Do it at class level..
And Inflate View Only if the convertView==null or else just return convertView.. Inflating view is a costly process....
Well like you said these calls are due to measure() and onMeasure() calls of ListViews parents and I'm sure you are using height=wrap_content also with wrap_content on height your ListView will check without stop if your height has changed.
So the solution is to put the height=fill_parent.
I hope this helped you.
The underlying reason for this is that ListView.onMeasure() calls AbsListView.obtainView(), which will request a view from your list adapter. So if your view is being remeasured through animations, your performance will be very poor.

Refresh view display when underlying data changes

I have seen this question answered a few times in which it is suggested to use the method notifyDataSetChanged() from BaseAdapter.
Is there a way to refresh when your application does not use any adapters? I have a simple application where I use a few activities with preferences, and relative layouts with text views and buttons. At the moment I do not use any of the adapters like SimpleAdapter or ArrayAdapter or CursorAdapter. It seems like in my case I have to create one of them just to get to use notifyDataSetChanged()? There is no easier way for me?
Looks like (I may be mistaken, but it really looks like) you don't catch the purpose of those classes - SimpleAdapter, ArrayAdapter or CursorAdapter. They are expected to work with ListView inside of an Activity (or even better - inside of a ListActivity). If you don't use ListView then those adapers are most likely useless for you.
I assume you have your data persisted in some way (SharedPreferences or file). So if you start any of your Activities, then it just reads the data to populate the views. In this case nothing extra is needed. In case if you need to reload data for a currently visible Activity, then just reread the data from persistent storage and repopulate the views.
If your data is changing and you need to refresh your view, how is the data actually changing? Is it in a separate thread or, do you just want something to happen periodically (polling an RSS feed, or something)?
I had a similar problem. I had a radio group, and each radio button had a label. When the screen rotated, I used a different layout with a radio group & radio buttons with the same IDs. I was calling setContentView() in onCreate(): and after rotation, the “old” labels would show up on the new layout (bizarre). When I moved setContentView() to onResume, everything seems to update ok. Thanks to Arhimed for his answer above.

Refresh layout while filling it

I have an activity with a HorizontalScrollView. When it opens, I start filling this view (or rather, a container layout inside it) with another views. This is done from another thread by using handler.post.
The views are added in bunches of 15, and when there are no more views to add, I start updating them with new data (this is a kind of streaming data from a server).
The problem is that the scrollview is empty until all of the views are added. As soon as they are all added and start updating, the scrollview gets drawn.
How do I refresh it in the process of adding views? I don't want the screen to be empty for 3 seconds while all of the views are added.
Thanks a lot.
UPDATE: turned out this problem is not specific for HorizontalScrollView, this is the case for any generic layout.
Im not sure I understand exactly how your program is constructed, so that the views are not shown until you start updating... but perhaps myLayout.forceLayout() helps?
The thing is ofcourse that layout only gets shown after onCreate().
I could imagine you use a AsyncTask to perform data lookup and use the callbacks provided in that class to update the UI, since they run on the main thread. This way you can just finish the onCreate(), and then start updating your view.

Categories

Resources