Refresh layout while filling it - android

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.

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.

how to prevent creating same layout everytime while using fragments and viewpager?

I have a viewpager that holds 3 different fragments and all fragments has tablelayout which loads dynamicaly. (while saying dynamically, I never try to reach any dataset, I take dataset from bundle already, what I do is just iterate dataset and during this process creating tablerows, textviews and inserting them into tablelayout by one by, so the issue is only creating view as dynamicaly)
My problem is while swiping between fragments it takes a while. I noticed it does that filling operation I mentioned above in oncreateview everytime.
What I want to do is it should create tablelayout just once at the begining and return it everytime as view if fragment would be selected.
When I try that fragments view appears only once and after swiping nothing appears.
I couldnt sleep and I am writing these via my cellphone, so I cant share what I did at the moment but I hope youll get what I want to do and maybe direct me in a good way.
You can use:
myViewPager.setOffscreenPageLimit(3);
which will keep a fragment around once it has been loaded.
Also, you really should use a ListView instead of building out TableRows. This will scroll much smoother and load faster.

Showing animation of Views created from a custom adapter

I'm trying to show an animation with all Views that I've created from an adapter. When I scroll down, it shows the animation correctly, but when I scroll up, I see these Views recreate themselves and show the animation again. Then, when I scroll down, it happens again.
My assumption is that the mechanism of creating a View from an adapter is to load the View into memory; just the group of Views which are on screen right now (but above and below views are not loaded into memory). These will be loaded again when I scroll to these views, right?
Is there any way to fix this problem?
PS: Sorry for my English, I hope you understand my problem.
My assumption is that the mechanism of creating a View from an adapter
is to load the View into memory; just the group of Views which are on
screen right now (but above and below views are not loaded into
memory)
That's somewhat correct: a ListView will not try to visualize any data that isn't (at least partially) visible. It also 'recycles' views, meaning that any view that isn't currently used to present data to the user and is of the same 'type' as the next data item, may get reused.
Hence you shouldn't rely on persisting data with or make any assumptions about the existence of particular views. In stead, use something that's separate from the views; e.g. the dataset you're visualizing.
Quite often, you'll supply a list of POJOs to a BaseAdapter or ArrayAdapter. You could simply add a boolean to the POJO indicating whether it should animate or not, and change that whenever the animation for that particular item finishes. Alternatively, you could keep track of these values in a separate collection (which is probably the more straightforward approach if you're dealing with a Cursor as data source rather than POJOs).

Calling setcontentview multiple times?

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();

Dynamically added views in linearlayout

Dynamically I am creating the linear layout and adding the views to linear layout every 5 sec I need to update data to linear layout from database .
But When I checked the log output it is adding to linear layout but gui is not getting updated for every 5 sec. I am using scheduled fixed timer task .
What should i do ?
I agree w/ the other answer from Dimitar that you probably don't want to do this with a linear layout.
However if you must, are you calling invalidate() on the LinearLayout after adding a new view to it?
If you want to visualize data in your application, you should adapt this data to the UI component that will display it. LinearLayout doesn't sound like a good choice for what you are trying to do.
Most often, in Android this is done by using a subclass of BaseAdapter and displaying the data in a subclass of AdapterView. Changes in the adapted data should be handled by the adapter, and the UI component should be updated accordingly.
If you are using SQLite database, take a look at the classes Cursor and CursorAdapter.

Categories

Resources