0

I have 3 different viewgroups that need to be added in a LinearLayout. I'm using addView() to add it.

However, the adding is based on the response that my web service is returning. If there is no data, it will make a callback to the UI that the view will be empty.

Essentially, there are 3 views which are Featured, Latest and Categories. I want Featured to be at the top, followed by Latest and Categories.

I'm calling the web service like so,

public void loadFromApis() {
    dealsService.getFeaturedDeals(this);
    dealsService.getLatestDeals(this);
    dealsService.getDealsCategories(this);
}

Example of successful callback (with data) and view adding:

@Override
public void onFeaturedSuccess(List<FeaturedModel> model) {
    View view1 = DealsPanel.build(this, model);
    linearLayout.addView(view1, 0);
}

@Override
public void onLatestSuccess(List<LatestModel> model) {
    View view2 = DealsPanel.build(this, model);
    linearLayout.addView(view2, 1);
}

@Override
public void onCategoriesSuccess(List<CategoriesModel> model) {
    View view3 = DealsPanel.build(this, model);
    linearLayout.addView(view3, 2);
}

I've tried using the index parameter to set the position, but since I'm loading the view based on API response, the layout wouldn't know which view to be draw first, so initializing the index would result in IndexOutOfBoundsException error.

My question is, based on this requirements, how can I statically define the position of each view to be added first and so forth? Any suggestions on improving the structure of this code?

Thanks in advance

emen
  • 6,050
  • 11
  • 57
  • 94

1 Answers1

1

One approach would be to statically define 3 child layouts in code or in XML inside of your parent LinearLayout, and then add your new views to the child layouts. That will preserve their order. For example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

   <LinearLayout android:id="@+id/featuredDealsLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

   <LinearLayout android:id="@+id/latestDealsLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

   <LinearLayout android:id="@+id/dealCategoriesLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
</LinearLayout>

Then, assuming you initialize variables containing the new layouts (ie featuredDealsLayout) you could change your code to something like:

@Override
public void onFeaturedSuccess(List<FeaturedModel> model) {
    View view = DealsPanel.build(this, model);
    featuredDealsLayout.addView(view);
}

@Override
public void onLatestSuccess(List<LatestModel> model) {
    View view = DealsPanel.build(this, model);
    latestDealsLayout.addView(view);
}

@Override
public void onCategoriesSuccess(List<CategoriesModel> model) {
    View view = DealsPanel.build(this, model);
    dealCategoriesLayout.addView(view);
}
Egg
  • 1,986
  • 16
  • 20
  • Thanks for the approach. It seems working now. But I'm just curious, what do you think of the impact on the UI performance due to the additional view hierarchy in the layout? – emen Nov 18 '16 at 07:47
  • 1
    The performance impact should be minimal since it's just 2 layers of nesting. You could, however, use a method like this to replace the linearlayouts with your views to keep things as fast as possible: http://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another-view-on-run-time – Egg Nov 18 '16 at 14:27