RecyclerView that does not scroll and shows all items

安稳与你 提交于 2019-12-29 16:07:25

问题


I have a RecyclerView (and some other views) in a ScrollView. Currently the RecyclerView is laid out as very small (it shows 2 items out of 5 that it contains) and it scrolls independently of the ScrollView, which is obviously not great UX. I would like to get the RecyclerView to not scroll and to extend so that all its items are visible.

(I know it's stupid to use a RecyclerView in this case. I'm only doing this because somewhere else in the app I need a normal RecyclerView with scrolling etc. but the same kind of content, and I don't want to duplicate code).


回答1:


It’s pretty simple, simply set the RecyclerView’s height to wrap_content.

You might also benefit from disabling nested scrolling on the recycler view, like so:

RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setNestedScrollingEnabled(false);



回答2:


The solution of setNestedScrollingEnabled(false) isn't as full as it should: you need to use NestedScrollView instead of ScrollViewfocusableInTouchMode="true" to the child of the NestedScrollView .

If you insist on using ScrollView, you should also set minHeight to the RecyclerView, and also set overScrollMode="never" . In this case, it still isn't a good solution because the minHeight might not be enough in some cases

Other alternative solutions that you should consider:

  1. Replace the ScrollView&RecyclerView with a single RecyclerView, which has views with additional view type for what you had in the ScrollView

  2. Use GridLayout or another layout instead.




回答3:


Maybe it is not completely clear at first sight what to do with all these answers. I just tried them and the working one is:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

No need to change anything programmatically.




回答4:


Also try to play with:

android:overScrollMode



回答5:


An easy way is to use in your Custom Adapter, inside the onBindViewHolder method this line: holder.setIsRecyclable(false);




回答6:


You should replace your scrollView for androidx.core.widget.NestedScrollView with matchparent, its simple and work fine.




回答7:


In your activity.xml file

<androidx.core.widget.NestedScrollView 
    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"
    tools:context=".ActivityName">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="false">

     </androidx.recyclerview.widget.RecyclerView>

</androidx.core.widget.NestedScrollView>

In RecyclerView use android:nestedSrollingEnabled="false" and use NestedScrollView as a parent Scroll View.




回答8:


Following is the code for disabling scroll in the recycler-view, and showing all the items in your layout. It might work:

public class NoScrollRecycler extends RecyclerView {

    public NoScrollRecycler(Context context){
        super(context);
    }

    public NoScrollRecycler(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    public NoScrollRecycler(Context context, AttributeSet attrs, int style){
        super(context, attrs, style);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){

        //Ignore scroll events.
        if(ev.getAction() == MotionEvent.ACTION_MOVE)
            return true;

        //Dispatch event for non-scroll actions, namely clicks!
        return super.dispatchTouchEvent(ev);
    }
}

use like this way:

<com.example.custom.NoScrollRecycler
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_white"/>



回答9:


I realised that I use BottomNavigationView which blocked my recycler view from displaying the last item. I fixed it by adding paddingBottom to it:

    <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recipient_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="70dp"
                app:layout_constraintTop_toBottomOf="@id/view"
                />


来源:https://stackoverflow.com/questions/38192841/recyclerview-that-does-not-scroll-and-shows-all-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!