grandma
grandma

Reputation: 37

RecyclerView won't Scroll

I made a fragment which contains 3 RecyclerView, 1 which is a horizontal RV works, but the other 2 which is vertical doesn't work. I couldn't scroll through it.

(image link: [http://s15.postimg.org/lc6bxz9ej/so1.png][2])

HomeFragment.java @AfterViews

LinearLayoutManager catLM = new LinearLayoutManager(mContext);
    mRVcatList.setLayoutManager(catLM);
    mRVcatList.setAdapter(new CategoryAdapter(initCategories()));

    LinearLayoutManager hotLM = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
    mRVhotList.setLayoutManager(hotLM);
    mRVhotList.setAdapter(new HotItemsAdapter(initHotItems()));

    LinearLayoutManager brandLM = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
    mRVbrandList.setLayoutManager(brandLM);
    mRVbrandList.setAdapter(new BrandAdapter(initBrands()));

    mRVbrandList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.mFLframe, new ProductListFragment_());
            transaction.addToBackStack(null);
            transaction.commit();
        }
    }));
    mRVcatList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.mFLframe, new CategoryFragment_());
            transaction.addToBackStack(null);
            ((MainActivity) mContext).hideLogo();
            transaction.commit();
        }
    }));
    mRVhotList.addOnItemTouchListener(new HotItemClickListener(mContext, new HotItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.mFLframe, new DetailFragment_());
            transaction.addToBackStack(null);
            ((MainActivity) mContext).hideLogo();
            transaction.commit();
        }
    }));

And here is my XML file,

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <!--//HOT-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/mRLtopHot">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hot Items"
            android:textColor="@color/ColorPrimaryDark"
            android:paddingLeft="5dp"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/mTVhot"
            android:textSize="@dimen/bebas_medium" />

        <View
            android:layout_width="wrap_content"
            android:layout_height="2dip"
            android:background="#00B7F6"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRVhotList"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:padding="5dp"
        android:background="#FFFFFF"
        android:layout_below="@+id/mRLtopHot"/>

    <!--CAT-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#FFFFFF"
        android:layout_marginTop="10dp"
        android:id="@+id/mRLtopCat"
        android:layout_below="@+id/mRVhotList"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Categories"
            android:paddingLeft="5dp"
            android:textColor="@color/ColorPrimaryDark"
            android:id="@+id/mTVcat"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:clickable="false"
            android:textSize="@dimen/bebas_medium" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/mIVcatCollapse"
            android:paddingRight="5dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <View
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="2dip"
            android:background="#00B7F6"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRVcatList"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:background="#FFFFFF"
        android:layout_below="@+id/mRLtopCat"/>

    <!--BRAND-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:id="@+id/mRLtopBrand"
        android:background="#FFFFFF"
        android:layout_below="@+id/mRVcatList"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Brands"
            android:paddingLeft="5dp"
            android:textSize="@dimen/bebas_medium"
            android:textColor="@color/ColorPrimaryDark"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/mTVbrand" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="5dp"
            android:id="@+id/mIVbrandCollapse"
            android:background="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <View
            android:layout_width="wrap_content"
            android:layout_height="2dip"
            android:background="#00B7F6"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRVbrandList"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:background="#FFFFFF"
        android:layout_below="@+id/mRLtopBrand"
        android:layout_centerHorizontal="true"/>


</RelativeLayout>

Previously, I made the "Categories" and "Brand" tab closed and could be expanded when clicked (it's just a simple usage of setVisibility), and the RecyclerView(s) did scroll! But I don't use that UI anymore. Unfortunately, this problem arises.

How could I make the vertical RecyclerView works? Thanks!

Upvotes: 0

Views: 1605

Answers (1)

Mariano Zorrilla
Mariano Zorrilla

Reputation: 7686

You can't have a list style layout inside a ScrollView (The ScrollView will destroy the listview/gridview/recyclerview scroll function)

You must get the height of the RecyclerView to make it fixed. Here is an example: Android list view inside a scroll view

Or, you can use this library but it use a ListView: https://github.com/PaoloRotolo/ExpandableHeightListView (maybe will work with RecyclerView)

Upvotes: 3

Related Questions