first question on SO, forgive me if I forget to include something.
In my activity, I have a function that loads a new activity that has a tabbed ViewPager with two fragments. From what I understand (or rather, from what I can tell) the activity does not load until both tabs have completed their onCreateViews() functions. Currently, this is taking between 500ms-2000ms which makes the application feel rather clunky. I believe this gives me two options:
Have the activity load and display the first tab once the first tab has finished it's onCreateView()
Preferably, speed up the loading of the second tab/fragment. What I'm doing is very slow and I'm looking for a better way to do it.
Here is an excerpt from my code for the second fragment (tab):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root_view = inflater.inflate(R.layout.stats_layout, container, false);
for (Map.Entry<String, int[]> entry : stats.entrySet()) {
String key = entry.getKey();
int[] value = entry.getValue();
if (value[2] == 1) {
LinearLayout ll_singles = (LinearLayout) root_view.findViewById(R.id.ll_singles_stats);
RelativeLayout rl_detail = (RelativeLayout) inflater.inflate(R.layout.rl_detail, ll_singles, false);
( (TextView) rl_detail.findViewById(R.id.tv_made) ).setText(Integer.toString(value[0]));
( (TextView) rl_detail.findViewById(R.id.tv_total) ).setText(Integer.toString(value[0] + value[1]));
( (TextView) rl_detail.findViewById(R.id.tv_percent) ).setText(MessageFormat.format("{0,number,0.00%}", (float) value[0] / (value[0] + value[1])));
ll_singles.addView(rl_detail);
}
}
Without getting too specific on my entire layout (unless someone asks), I inflate a single RelativeLayout (rl_detail) many (50+) times and add it to four different LinearLayouts (in this excerpt, ll_singles).
I know findViewById() is an expensive action and I have to think there is a way to avoid the findViewById() that I do every time I inflate the exact same view when I change the text for the TextView.
I have explored asynctasks (haven't been able to find an example that inflates views in the background -- it sounds like it isn't possible) and have considered using a ViewHolder, but that seems exclusive to ListViews.
Any ideas?
Related
I am designing an app which contains around 25 questions.
How to keep changing the questions when i click the next button.
So here the layout remains the same except the question (i.e. the TextView may change according to different questions but other elements like Buttons, background will not change).So creating many layout for different questions is time consuming.
So how can I use the same layout for these many questions.Should I create these many layouts and classes for asking each question??
You should store your questions in a database, or in a static final String[] if there aren't many of them.
Multiple classes are not required here, because you only change the question text, i.e. questionView.setText(newQuestion);.
Then you need 2 buttons - yes and no, which have an OnClickListener set to them, which in return sets the questionView's text with the newQuestion.
Create a class which will extend Fragment. Use your standard layout inside onCreateView.
Create a method inside this class like this:
public static FragmentName newInstance(int question)
{
FragmentName fragment = new FragmentName();
Bundle args = new Bundle();
args.putInt("QUESTION_ID", question);
fragment.setArguments(args);
return fragment;
}
Now inside your fragment's onCreateView method add some code to check the question number and edit the layout as needed
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/* ... Code to inflate your layout ... */
Bundle arguments = getArguments();
int question = arguments.getInt("QUESTION_ID", 0);
switch(question)
{
/* Add your code in here to modify the layout */
}
/*... ....*/
}
Now just use a fragment manager from your activity to handle the transitions, using FragmentName.newInstance(question_number) to instantiate the single fragments.
This is of course if just programmatically editing the Views from inside the Activity is not enough.
Create different classes but use the same layout for every class. and show/hide required textviews in each class.
Suppose you have parent layout which contain item(Parent view of question just like view container) and resolve it after that infalte your question layout by using this code. and replace the just values on the OnClickListener of yes button.
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
// Access chile view by child.getElementById(id)
item.addView(child);
When you click yes or no change the textview content accrodingly;
yes_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
tv_question.setText("This is a new question");.
}
});
I am trying to understand how the Tinder like/dislike card system works by looking through this example on GitHub: https://github.com/kikoso/Swipeable-Cards/blob/master/AndTinder/src/main/java/com/andtinder/view/CardStackAdapter.java. I understand the importance of BaseAdapters and populating the view/card with the necessary info. This part of the code that is confusing the hell out of me is this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout wrapper = (FrameLayout) convertView;
FrameLayout innerWrapper;
View cardView;
View convertedCardView;
if (wrapper == null) {
wrapper = new FrameLayout(mContext);
wrapper.setBackgroundResource(R.drawable.card_bg);
if (shouldFillCardBackground()) {
innerWrapper = new FrameLayout(mContext);
innerWrapper.setBackgroundColor(mContext.getResources().getColor(R.color.card_bg));
wrapper.addView(innerWrapper);
} else {
innerWrapper = wrapper;
}
cardView = getCardView(position, getCardModel(position), null, parent);
innerWrapper.addView(cardView);
} else {
if (shouldFillCardBackground()) {
innerWrapper = (FrameLayout) wrapper.getChildAt(0);
} else {
innerWrapper = wrapper;
}
cardView = innerWrapper.getChildAt(0);
convertedCardView = getCardView(position, getCardModel(position), cardView, parent);
if (convertedCardView != cardView) {
wrapper.removeView(cardView);
wrapper.addView(convertedCardView);
}
}
return wrapper;
}
Why are FrameLayouts being created dynamically? It seems like multiple FrameLayouts are being made with wrapper and inner wrapper? What does wrapper even mean? Why are classes like shouldFillCardBackground() used but not even defined anywhere in the repository?
First of all, it is important to notice that the CardStackAdapter you link to is abstract, so it will never be instantiated directly. Rather, the class SimpleCardStackAdapter will be used for instance.
That said, lets get the purpose of the method clear; The getView() method's purpose is to create and populate a view for the given position.
Creating layouts can be done either by inflating them from xml, or otherwise, by creating them in code. The latter is happening here. The main reason that they create the layout dynamically, is to keep things dynamic. The exact views in the layout depend on some configurations which are checked at runtime (in the method shouldFillCardBackground()). Note that this could also be achieved by creating the layout in xml, then inflate it, and dynamically hide/show (or remove/add) views and layouts. The auther of the code simply chose to do it in code.
A wrapper is an object (in this case a layout) which does not much more that holding other objects (other layouts). In the context of layouts, they are usually used to add some kind of background or padding. That is also what is happening here.
shouldFillCardBackground() is a method, not a class, and it is definitely defined: in line 71.
It is important to realize that it seems that the main puropose of this code is to be an example, a demo. It is possible it is not fully functional and that not everything is implemented the best way (for instance, shouldFillCardBackground() returns true by default).
I have a ListView in my application. The adapter for this listview contains multiple item view types (around 5 till now), via which I can inflate different types of row views inside the listview.
All row views inflated inside the adapter are custom subclassed view/view group.
public class CustomView1 extends RelativeLayout {
Bundle bundle;
public CustomView1(Bundle bundle) {
super(context);
this.bundle = bundle;
addSubViews(bundle.getBundleList("list"));
}
private void addSubViews(ArrayList<Bundle> list) {
for(Bundle element : list) {
//add sub views via reflection
View view = (View) Class.forName(packageName + type).getConstructor(Bundle.class).newInstance(element);
addView(view);
}
}
//called from getView() in adapter when convertView != null
public void onRecycle(Bundle bundle) {
if(bundle != this.bundle) {
this.bundle = bundle;
removeAllViews();
addSubViews(bundle.getBundleList("list"));
}
}
}
Bundle passed to each custom view contains layout info for that view. In this way, I can create and add any view/viewgroup inside any viewgroup. All well till now.
Now the problem comes when this code runs inside ListView. Since all the view types are created by the adapter initially, scrolling jerks a lot because the adapter keeps on creating new custom views of different itemViewType. How to reduce those jerks in listview ? Any ideas? In the listview, all viewTypes are different at the top 5 positions, so the adapter has to create these views and that makes the experience sluggish.
Even when the adapter recycles similar view type convertViews after 5th index, I clear the container using removeAllViews() and run this loop again because the subView bundle list of the incoming bundle from 6th position onwards might be different. So in the end, adapter is only recycling empty ViewGroups. Since the subView list can possibly contain anything (maybe one more bundle list inside any element bundle), I have to do removeAllViews() to accommodate new subview tree in the recycled convertView.
I thought of using vertical ScrollView but that would take too much memory upfront, and the number of custom views inflated is dynamic, can increase to 20.
The app is running but the scroll is so bad there is hardly any usability left, so its looking like till now I have achieved nothing by adding so much dynamic behavior also. Please suggest me ways to counter this problem.
I am suspecting that the use of setLayoutParams inside CustomView classes may be stopping the scroll because I set the width/height of all views after they are created.
Update #1 getView() code using ViewHolder pattern
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
holder.customView1 = new CustomView1(bundle);
convertView = holder.customView1;
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.customView1.onRecycle(bundle);
ListView has excellent support for different View types. Just make sure to use view holder pattern to avoid jerky scrolling and then override getViewTypeCount() and getItemViewType().
More detail http://android.amberfog.com/?p=296
The following piece of code is inflating the same view for 20 times. Since inflating is costly. I want to inflate it only one, and use the same view for 20 items, i just want to change the visible data in the UI.
LinearLayout ll = new LinearLayout(context);
for (int i = 0; i < 20; ++i) {
View itemView = inflater.inflate(getLayoutId(), parent, false);
itemView.setText(data.getName(i);
ll.add(itemView);
}
I want something like this.
LinearLayout ll = new LinearLayout(context);
View itemView = inflater.inflate(getLayoutId(), parent, false);
for (int i = 0; i < 20; ++i) {
itemView.setText(data.getName(i);
ll.add(itemView);
}
But am not able to use the itemView obj this way.
Can anyone tell me how to use the view many times once it inflated.
You cannot do that. If you think its costly then find another way to create your layout.
But consider gridViews for example. They create a ton of views and show them and that works great.
You cannot add the same object of a view 2 times to a layout. Every object has its own state which in your case in a way says that you will share the state between all your 20 views which doesn't make sense to do, meaning changing the text on one textView will change it on all the rest...
Just inflate 20 seperate views and fill them appropriately.
Also consider using ListView or GridView if you actually have the exact same view it can offer some nice features like view recycling.
You should use ViewHolder patern:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
it should do all things You want
I have a LinearLayout with many nested LinearLayouts and TextViewss
My main activity inflates the main LinearLayout,
Then I load data from a server and based on the data received, I add multiple Layouts in a place holder (LinearLayout)
This is simple a news page where I load Images associated with the news and place it inside an initially empty LinearLayout.
Each Image has the following info: Title(TextView), Date(TextView), Image(ImageView) so what I actually do is the following:
*Please notice that this is only the essential coded in the question I elemenated all the try -> catch ... if/else ....etc
public void addImages(JSONArray images){
ViewGroup vg = (ViewGroup) findViewById(R.id.imagesPlaceHolder);
// loop on images
for(int i =0;i<images.length;i++){
View v = getLayoutInflater().inflate(R.layout.image_preview,vg);
// then
I think that here is the problem
ImageView imv = (ImageView) v.findViewById(R.id.imagePreview);
TextView dt = (TextView) v.findViewById(R.id.dateHolder);
TextView ttl = (TextView) v.findViewById(R.id.title);
// then
dt.setText("blablabla");
ttl.setText("another blablabla");
// I think the problem is here too, since it's referring to a single image
imv.setTag( images.getJSONObject(i).getString("image_path").toString() );
// then Image Loader From Server or Cache to the Image View
}
}
The code above works good for a single image
But for multiple images the Image Loader doesn't work I guess it's because all ImageViews (Inflated multiple times) have the same ID
When you provide a ViewGroup to be used as the parent, the View returned by inflate() is this parent (vg in your case) and not the newly created View. Therefore, v points toward the ViewGroup vg and not toward the newly created View and as all of your children have the same id, the same subviews (imv, dt, ttl) are returned each time.
Two solutions. The first one is to change their id right after you are finished with them, before the next iteration. Therefore, on the next creation at the beginning of the next iteration, the newly created Views will have a different IDs from the older Views because they will still use the old constant defined in R.
The other solution would be to add the parameter false to the call to inflate() so that the newly created view will not be attached to the ViewGroup and will then be returned by the inflate() function instead of the ViewGroup. The rest of your code will then works as attended with the exception that you will have to attach them to the ViewGroup at the end of the iteration.
Notice that you still need to provide a ViewGroup because it will be used to determine the value of the LayoutParams.
I had the same problem, and based on the answer from #SylvainL, here'a a working solution:
// myContext is, e.g. the Activity.
// my_item_layout has a TextView with id='text'
// content is the parent view (e.g. your LinearLayoutView)
// false means don't add direct to the root
View inflated = LayoutInflater.from(myContext).inflate(R.layout.my_item_layout, content, false);
// Now, before we attach the view, find the TextView inside the layout.
TextView tv = (TextView) inflated.findViewById(R.id.text);
tv.setText(str);
// now add to the LinearLayoutView.
content.addView(inflated);
Is there a reason why the ImageView in the layout XML needs to have an ID? Could you erase the android:id attributes from the image_preview.xml layout and then simply iterate through the children of the inflated LinearLayout? For example:
ViewGroup v = (ViewGroup)getLayoutInflater().inflate(R.layout.image_preview,vg);
ImageView imv = (ImageView) v.getChildAt(0);
TextView dt = (TextView) v.getChildAt(1);
TextView ttl = (TextView) v.getChildAt(2);
I inflate XML-Layout with dynnamic and get text of id
private val onAddView = View.OnClickListener {
val parent = viewForm.findViewById<LinearLayout>(R.id.layout_parent)
LayoutInflater.from(activity).inflate(R.layout.layout_child, parent) // layout_child has id "tv_attribute"
}
private val onSave = View.OnClickListener {
val parent = viewForm.findViewById<LinearLayout>(R.id.layout_parent)
for (i in 0 until parent.childCount) {
val getText = parent.getChildAt(i).findViewById<TextView>(R.id.tv_attribute).text
}
}