NestedScrollView, Scroll(parent) の中でScroll(child)させるには
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--
NestedScrollViewはScrollViewと同じ様に使えます。
入れ子にしたスクロールの親と子をサポートしている
-->
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#fdc"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:textSize="30sp"
android:textColor="#640"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nyan"/>
<ImageView
android:src="@drawable/nyan"
android:layout_gravity="center_horizontal"
android:layout_width="300dp"
android:layout_height="300dp"/>
<ScrollView
android:id="@+id/scrollView_child"
android:background="#dfe"
android:layout_width="match_parent"
android:layout_height="100sp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_marginLeft="50dp"
android:textSize="20sp"
android:textColor="#484"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: Nyan" />
</LinearLayout>
</ScrollView>
<TextView
android:layout_margin="50dp"
android:layout_width="300dp"
android:layout_height="300dp"
android:text="Give me a fish !"/>
<ImageView
android:src="@drawable/nyan2"
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:text="res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String resume =
"Name: Nyan\n" +
"Address: Kotatsu\n" +
"Favorite: Beer\n" +
"Job: Sleep\n" +
"Height: 30cm\n" +
"Weight: Secret\n" +
"Age: Unknown\n" +
"Pattern: Tiger\n";
private NestedScrollView parentScrollView;
private ScrollView childScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// スクロール(子)に文字をセット
TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(resume);
// スクロールビュー(子)のタッチ処理
childScrollView = (ScrollView)findViewById(R.id.scrollView_child);
childScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
parentScrollView = (NestedScrollView)findViewById(R.id.nested_scrollview);
parentScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// Allow the touch request for parent scroll
childScrollView.getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
}
}