Bug 1288106 - Setting a fixed height for nested recyclerviews r?ahunt draft
authorJonathan Almeida (:jonalmeida) <jonalmeida942@gmail.com>
Thu, 04 Aug 2016 06:03:01 -0700
changeset 396754 2b4759c227e0dc730e35d540824bcf2ed72c5932
parent 396753 b27eacf97bfee429d36c4d975cbcf8d9d6a8c8be
child 399750 167b747e09d7584e7e37eac615e74494762956a5
push id25099
push userjonalmeida942@gmail.com
push dateThu, 04 Aug 2016 13:06:07 +0000
reviewersahunt
bugs1288106
milestone51.0a1
Bug 1288106 - Setting a fixed height for nested recyclerviews r?ahunt The `wrap_content` layout param for a RecyclerViews don't work with our version of the support library. For now, the quicker solution is to hardcode the height of the views as needed. MozReview-Commit-ID: HEXrUbLI8vd
mobile/android/base/java/org/mozilla/gecko/home/activitystream/MainRecyclerAdapter.java
mobile/android/base/resources/layout/activity_stream.xml
mobile/android/base/resources/layout/activity_stream_category.xml
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/MainRecyclerAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/MainRecyclerAdapter.java
@@ -1,60 +1,73 @@
 package org.mozilla.gecko.home.activitystream;
 
 
 import android.content.Context;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
-import android.util.Log;
+import android.util.TypedValue;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.FrameLayout;
+import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import org.mozilla.gecko.R;
 
 import java.util.ArrayList;
 import java.util.List;
 
 
 class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.ViewHolder> {
 
     private final List<Item> categories;
     private final Context context;
 
+    private static final int VIEW_TYPE_TOP_SITES = 0;
+    private static final int VIEW_TYPE_HIGHLIGHTS = 1;
+    private static final int VIEW_TYPE_HISTORY = 2;
+
     MainRecyclerAdapter(Context context) {
         this.context = context;
         categories = new ArrayList<>();
         categories.add(new Item("Top Sites"));
         categories.add(new Item("Highlights"));
         categories.add(new Item("History"));
     }
 
     @Override
     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, parent.getResources().getDisplayMetrics());
+        parent.setLayoutParams(new FrameLayout.LayoutParams(parent.getLayoutParams().width, (int) px));
         View itemView = LayoutInflater
                 .from(context)
                 .inflate(R.layout.activity_stream_category, parent, false);
         return new ViewHolder(itemView);
     }
 
     @Override
     public void onBindViewHolder(ViewHolder holder, int position) {
-        holder.vTitle.setText(categories.get(position).getLabel());
-
         LinearLayoutManager layoutManager = new LinearLayoutManager(context);
-        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
-        if (position == 0) {
+        if (position == VIEW_TYPE_TOP_SITES) {
+            setCorrectedLayoutOrientation(layoutManager, LinearLayoutManager.HORIZONTAL);
+            setCorrectedLayoutHeight(115, holder, context);
             holder.vCategoryRv.setAdapter(new TopSitesRecyclerAdapter(context));
-        } else if (position == 1) {
+        } else if (position == VIEW_TYPE_HIGHLIGHTS) {
+            setCorrectedLayoutOrientation(layoutManager, LinearLayoutManager.HORIZONTAL);
+            setCorrectedLayoutHeight(220, holder, context);
             holder.vCategoryRv.setAdapter(new HighlightRecyclerAdapter(context));
-        } else if (position == 2) {
+        } else if (position == VIEW_TYPE_HISTORY) {
+            setCorrectedLayoutOrientation(layoutManager, LinearLayoutManager.VERTICAL);
+            setCorrectedLayoutHeight(80, holder, context);
             holder.vCategoryRv.setAdapter(new HistoryRecyclerAdapter(context));
         }
+
+        holder.vTitle.setText(categories.get(position).getLabel());
         holder.vCategoryRv.setLayoutManager(layoutManager);
     }
 
     @Override
     public int getItemCount() {
         return categories.size();
     }
 
@@ -62,9 +75,20 @@ class MainRecyclerAdapter extends Recycl
         TextView vTitle;
         RecyclerView vCategoryRv;
         ViewHolder(View itemView) {
             super(itemView);
             vTitle = (TextView) itemView.findViewById(R.id.category_title);
             vCategoryRv = (RecyclerView) itemView.findViewById(R.id.recycler_category);
         }
     }
+
+    private void setCorrectedLayoutHeight(int height, ViewHolder holder, Context context) {
+        float px = TypedValue.applyDimension(
+                TypedValue.COMPLEX_UNIT_DIP, height, context.getResources().getDisplayMetrics());
+        holder.vCategoryRv.setLayoutParams(new LinearLayout.LayoutParams(
+                holder.vCategoryRv.getLayoutParams().width, (int) px));
+    }
+
+    private void setCorrectedLayoutOrientation(LinearLayoutManager lm, int orientation) {
+        lm.setOrientation(orientation);
+    }
 }
\ No newline at end of file
--- a/mobile/android/base/resources/layout/activity_stream.xml
+++ b/mobile/android/base/resources/layout/activity_stream.xml
@@ -2,12 +2,13 @@
 <org.mozilla.gecko.home.activitystream.ActivityStream xmlns:android="http://schemas.android.com/apk/res/android"
                                                       android:orientation="vertical"
                                                       android:layout_width="match_parent"
                                                       android:layout_height="match_parent"
                                                       android:background="@android:color/white">
 
     <android.support.v7.widget.RecyclerView
         android:id="@+id/recycler_main"
+        android:padding="12dp"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>
 
 </org.mozilla.gecko.home.activitystream.ActivityStream>
--- a/mobile/android/base/resources/layout/activity_stream_category.xml
+++ b/mobile/android/base/resources/layout/activity_stream_category.xml
@@ -28,13 +28,11 @@
                 android:layout_alignParentRight="true"
                 android:layout_alignParentEnd="true"/>
 
         </RelativeLayout>
 
         <android.support.v7.widget.RecyclerView
             android:id="@+id/recycler_category"
             android:layout_width="match_parent"
-            android:layout_height="wrap_content">
-
-        </android.support.v7.widget.RecyclerView>
+            android:layout_height="wrap_content" />
 
 </LinearLayout>