RecyclerView inside HorizontalScrollView Android

2019-02-20 21:01发布

问题:

I am using a Recycler view inside a Horizontal Scroll View to display threaded comments like below:

Comment1 Comment1Child1 Comment1Child2 Comment1Child2Child1 Comment1Child3 Comment2

etc.

I have done this with the following XML:

<HorizontalScrollView
                android:id="@+id/horizontalScrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:scrollbarSize="2dp">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/commentRV"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="47dp"
                    android:minWidth="200dp" />
            </HorizontalScrollView>

You'll see I set a min width of 200dp so that the comments never get too small and the deeper down the thread they go the further off screen it will be pushed.

It is displaying perfectly but I am unable to scroll horizontally. I can see the scroll bars but I cannot scroll horizontally.

I have fiddled with disabling Touch and nestedScrollingEnabled in the RV but not sure what the real solution is. Nothing seems to work?

Any help would be much appreciated! Thank you

回答1:

Remove HorizontalScrollView you can give LinearLayoutManager with horizontal orientation like this

LinearLayoutManager layoutManager= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
reclyclerView.setLayoutManager(layoutManager);
//recyclerView.setMinimumWidth(200); optional for width if u need