Two RecyclerViews under each other in one layout

前端 未结 7 873
不知归路
不知归路 2020-12-13 18:21

How can I get two RecyclerViews under each other in one layout? I don\'t want to have a single RecyclerView for all items. My code:



        
相关标签:
7条回答
  • 2020-12-13 18:49

    Use NestedScrollView as parent layout, it should have

    android:weightSum="2"
    

    and give

    android:layout_weight="1"
    

    to each RecyclerView of yours.It should be scrolled one after each other.

    0 讨论(0)
  • 2020-12-13 18:50

    You should create an XML layout file like this

    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/ingredients_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/steps_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    

    And in the code, you should call setNestedScrollingEnabled(false)

    RecyclerView ingredientsList = findViewById(R.id.ingredients_list);
    RecyclerView stepsList = findViewById(R.id.steps_list);
    
    ingredientsList.setNestedScrollingEnabled(false);
    stepsList.setNestedScrollingEnabled(false);
    
    0 讨论(0)
  • 2020-12-13 18:53

    Just use:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:scrollbars="vertical"
        android:layout_below="@+id/your_first_recycler"/>
    

    last line is for your problem.use it.

    0 讨论(0)
  • 2020-12-13 18:55

    I've found the answer myself.

    You need to put the LinearLayout into a ScrollView and use wrap_content as RecyclerView's layout_height.

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/list_header"
            android:background="@color/header"
            android:gravity="center"
            android:text="@string/find_friends__already_playing"
            android:visibility="visible" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/in_app_friends"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:background="@color/white"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/list_header"
            android:background="@color/find_friends__header"
            android:gravity="center"
            android:text="@string/find_friends__invite_friends" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/friends_to_invite"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"/>
    </LinearLayout>
    </ScrollView>
    

    Also there is a bug with with RecyclerView and wrap_content so you have to use a custom layout manager. Check out this post: How do I make WRAP_CONTENT work on a RecyclerView

    0 讨论(0)
  • 2020-12-13 19:01

    if you get the bottom recyclerview not scrolling with the main content, change the LinearLayout (see answer from alan_derua) to ConstraintLayout and wrap the two RecyclerViews inside. See code below:

    <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="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">
    
    <android.support.constraint.ConstraintLayout
        android:id="@+id/task_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/first_list_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@+id/textView3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:gravity="left"
            android:paddingTop="0dp"
            android:text="@string/my_tasks"
            app:layout_constraintBottom_toTopOf="@+id/second_list_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/first_list_view" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/second_list_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3" />
    
    </android.support.constraint.ConstraintLayout>
    </ScrollView>
    

    This worked for me!

    0 讨论(0)
  • 2020-12-13 19:04

    I also had the same problem and wrote a library which helps to achieve this by joining adapters and layouts.

    Gradle dependency to try it (needs jcenter repo):

    compile 'su.j2e:rv-joiner:1.0.3'//latest version by now
    

    Thea change xml to use a single RecyclerView which matches parent:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/joined_friends_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:background="@color/white"/>
    

    Then init RecyclerView in code like this:

        //init your RecyclerView as usual
        RecyclerView rv = (RecyclerView) findViewById(R.id.joined_friends_rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
    
        //construct a joiner
        RvJoiner rvJoiner = new RvJoiner();
        rvJoiner.add(new JoinableLayout(R.layout.your_title_for_in_app));
        rvJoiner.add(new JoinableAdapter(new YourInAppRvAdapter()));
        rvJoiner.add(new JoinableLayout(R.layout.your_title_for_invite));
        rvJoiner.add(new JoinableAdapter(new YourInviteRvAdapter()));
    
        //set join adapter to your RecyclerView
        rv.setAdapter(rvJoiner.getAdapter());
    

    You can check this link for more library details and explanation. Hope it helps.

    0 讨论(0)
提交回复
热议问题