Search code examples
androidandroid-recyclerviewscrollviewmaterial-designsmooth-scrolling

How to design complex UI in android


How can I have a ViewPager above a RecyclerView, or have several RecyclerViews in a ScrollView? I designed the following layout, but it has some problems when scrolling. What is the problem and how can I solve it?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dcdcdc"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ActivityMain"
    tools:showIn="@layout/app_bar_main">

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


       <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="200dip"
           />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </LinearLayout>
</ScrollView>

I have tried compile 'com.bartoszlipinski:recyclerviewheader2:2.0.1' with

 <com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader
           android:id="@+id/header"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal|top">

1- firstly it supports 11 sdk or greater,
2- secondly ViewPager wont work in header RecyclerView. and i have tried RecyclerView layout_width="wrap_content" but it is just supported in sdk21 .

How can I solve the RecyclerView scrolling ?


Solution

  • Try losing the top ScrollView and using a ListView instead of RecyclerView.

    ScrollView is unnecessary here and you need a hack for ViewPager to work with RecyclerView as it consumes all touch events. It would be simpler if you could get away with a ListView.