We should not put scrollable view inside scrollable view (and most developers say we should never put ListView inside ScrollView). But there are cases where we have to put the scrollable view inside Scrollable view inspite of these design guidelines.
When we put a scrollable view(Parent) inside a scrollable view(Child), it locks the touch intercept of the child view as the parent view has more priority over child. Therefore, you can't scroll the child view.
Following is the code to scroll the child view, it locks the touch intercept of parent view when a child view is touched.
Below is the example layout with scrollview inside a scrollview:
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
tools:context=".MainActivity"
android:background="@android:drawable/alert_dark_frame"
android:id="@+id/svParent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="1200dp"
android:src="@android:drawable/sym_def_app_icon"
android:scaleType="fitXY"
android:layout_centerInParent="true"/>
<ScrollView
android:id="@+id/svChild"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:drawable/alert_light_frame"
android:layout_centerInParent="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="600dp"
android:src="@android:drawable/sym_def_app_icon"
android:scaleType="fitXY"/>
<LinearLayout>
<ScrollView>
<RelativeLayout>
<ScrollView>
And following is the code to make child view scrollable when user touches it.
MainActivity.class
public class MainActivity extends Activity {
private ScrollView svParent;
private ScrollView svChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
svParent = (ScrollView) findViewById(R.id.svParent);
svChild = (ScrollView) findViewById(R.id.svChild);
//This is the code to lock the parent touch intercept when
child scrollview is touched
svChild.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
//When user touch the child view
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
svParent.requestDisallowInterceptTouchEvent(true);
// Disable touch on transparent view
return false;
//When user releases the touch event
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
svParent.requestDisallowInterceptTouchEvent(false);
return true;
//Move gesture of the user on the child view
case MotionEvent.ACTION_MOVE:
// Disallow ScrollView to intercept touch events.
svParent.requestDisallowInterceptTouchEvent(true);
return false;
default:
return true;
}
}
});
}
}
0 Comment(s)