问题
I want to make a LinearLayout
(to which I'm adding items dynamically) scrollable inside a scrollable RecyclerView
. On startup, I can see a little scrollbar initially (indicating it's a scrollable view, fades in seconds). Tried appCompat NestedScrollView as well as enabling nestedScrollingEnabled
, with no success. How can I make the LinearLayout
scrollable inside the RecyclerView
?
<FrameLayout 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">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:fillViewport="true"
android:nestedScrollingEnabled="true">
<LinearLayout
android:id="@+id/playerlist_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="7dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</FrameLayout>
RecyclerView code
<android.support.v7.widget.RecyclerView
android:id="@+id/messagesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:nestedScrollingEnabled="false"/>
UPDATE: I found out that scrolling does indeed work, but only if I spam the mouse or tap really fast inside the scrollview. It kind of gets focused and allows scrolling as long as mouse is held down. Really strange.
回答1:
Try disabling the touch of recycler view when scrollview
is being touched.
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView1);
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Disallow the touch request for parent scroll on touch of child view
view.getParent().requestDisallowInterceptTouchEvent(false);
return true;
}
});
回答2:
I solved it by adding a line to rafsanahmad007's solution:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView1);
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Disallow the touch request for parent scroll on touch of child view
view.getParent().requestDisallowInterceptTouchEvent(false);
view.onTouch(motionEvent);
return true;
}
});
I'm not sure that's how View.onTouch
is called on Java, as I'm developing with Xamarin.Android (C#). It should be pretty similar.
来源:https://stackoverflow.com/questions/48477033/scrollview-inside-recyclerview-wont-scroll