RecyclerView inside HorizontalScrollView Android

北城余情 提交于 2020-04-11 18:32:35

问题


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


来源:https://stackoverflow.com/questions/40803180/recyclerview-inside-horizontalscrollview-android

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