0

I have an multiple views in my screen.

How to include my layout content in top of the screen and recyclerview in my bottom of the screen?

Thanks in Advance..

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Krishna
  • 143
  • 2
  • 10

3 Answers3

0

it can be done by overriding this method,getItemViewType(int position) this specifies which layout to inflate for a perticular position of the recycler view

@Override
    public int getItemViewType(int position) {
        if (position == 0)
            return LAYOUT_TYPE_0;
        else if (position == 1)
            return LAYOUT_TYPE_1;
        else
            return LAYOUT_TYPE_ITEM;
    }

here is the complete adapter code

  public class RecyclerViewAdapter extends RecyclerView.Adapter {

        public static final int LAYOUT_TYPE_0 = 0;
        public static final int LAYOUT_TYPE_1 = 1;
        public static final int LAYOUT_TYPE_ITEM = 2;


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == LAYOUT_TYPE_0) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_0, parent, false);
                return new LayoutPos0(view);
            } else if (viewType == LAYOUT_TYPE_1) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_1, parent, false);
                return new LayoutPos1(view);
            } else {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_common, parent, false);
                return new LayoutCommon(view);
            }

        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {            
        }

        @Override
        public int getItemViewType(int position) {
            if (position == 0)
                return LAYOUT_TYPE_0;
            else if (position == 1)
                return LAYOUT_TYPE_1;
            else
                return LAYOUT_TYPE_ITEM;
        }



        public class LayoutPos0 extends RecyclerView.ViewHolder {
                //your code goes here
        }

        private class LayoutPos1 extends RecyclerView.ViewHolder {
        }

        private class LayoutCommon extends RecyclerView.ViewHolder{          
    }

 @Override
        public int getItemCount() {
            return 100;
        }

}
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
0

It depends on what kind of top layout you want to add.

If you want to add image or view pager with sliding images, then its better to use CollapsingToolbarLayout with AppBarLayout inside CoordinatorLayout, then Add your RecyclerView.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_parentlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="260dp"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorAccent"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <LinearLayout
                    android:id="@+id/avatar_bg"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="?attr/colorAccent"
                    android:gravity="center"
                    android:minHeight="160dp"
                    android:orientation="vertical"
                    app:layout_collapseMode="parallax">
                    <!-- Here you can add your header layout only if its not too big-->
                    <ImageView
                        android:id="@+id/myprofile_avatar"
                        android:layout_width="@dimen/profile_avatar_size"
                        android:layout_height="@dimen/profile_avatar_size" />



                </LinearLayout>
                <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:local="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:minHeight="?attr/actionBarSize"
                    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:layout_collapseMode="pin"/>

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

       <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/profile_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
</android.support.v4.widget.SwipeRefreshLayout>


    </android.support.design.widget.CoordinatorLayout>
    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />

</RelativeLayout>
Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53
-1

You can do this way, to include multiple view :

@Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...);
             case 2: return new ViewHolder2(...);

         }
    }

Refer RecyclerView with multiple view type.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142