RecyclerView and ListView are not recommended inside ScrollView, because when rendering ScrollView elements values ββare not calculated. This means that your adapter cannot be full when ScrollView is displayed, and later, when the RecyclerView is notified of data changes (for example, when you initialize your adapter), there is no way to recalculate the heights of the elements.
This is really a pain in the neck, because you should try to calculate the heights of the elements, and it is never accurate, so you will have discrepancies when displaying a ListView or RecyclerView inside a ScrollView. Below are examples of how to calculate element heights here and here .
My advice is to turn the RecyclerView into a LinearLayout and add items programmatically so that you emulate the behavior of a ListView or RecyclerView:
LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.files); layout.removeAllViews(); for (int i = 0; i < fileAdapter.getCount(); i++) { final View item = fileAdapter.getView(i, null, null); item.setClickable(true); item.setId(i); item.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fileContentRowPosition = v.getId(); # Your click action here } }); layout.addView(item); }
Here is its XML with file definition:
<ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="match_parent"> ... <LinearLayout android:id="@+id/files" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:orientation="vertical"> </LinearLayout> </ScrollView>
All java code can be checked here and the whole layout.
On the other hand, if you still want to continue your implementation and about your problem, you can check out this article on Handling Scrollable Controls in Scrollview
Yours faithfully,
source share