public final class MultiScrollView extends ScrollView { // private int origX, origY; // , 60 px, private final float THRESHOLD = 60; public MultiScrollView(Context context) { super(context); } public MultiScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MultiScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean canScrollHorizontally(int direction) { return true; } @Override public boolean canScrollVertically(int direction) { return true; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { origX = (int) ev.getX(); origY = (int) ev.getY(); } else if (ev.getAction() == MotionEvent.ACTION_MOVE) { float deltaX = Math.abs(ev.getX() - origX); float deltaY = Math.abs(ev.getY() - origY); return deltaX >= THRESHOLD || deltaY >= THRESHOLD; } return false; } }
public final class MyHorizontalScrollView extends HorizontalScrollView { public MyHorizontalScrollView(Context context) { super(context); } public MyHorizontalScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return false; } @Override public boolean canScrollHorizontally(int direction) { return false; } @Override public boolean onTouchEvent(MotionEvent ev) { return false; } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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"> <example.multiscroll.MultiScrollView android:id="@+id/field_y" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@color/color_black" android:fadingEdgeLength="0dp" android:focusable="true" android:isScrollContainer="true" android:overScrollMode="never" android:requiresFadingEdge="none" android:scrollbars="none" android:splitMotionEvents="true" tools:context="example.multiscroll.MainActivity"> <example.multiscroll.MyHorizontalScrollView android:id="@+id/field_x" android:layout_width="match_parent" android:layout_height="wrap_content" android:fadingEdgeLength="0dp" android:focusable="false" android:isScrollContainer="true" android:overScrollMode="never" android:requiresFadingEdge="none" android:scrollbars="none" android:splitMotionEvents="true"> <TableLayout android:id="@+id/table" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/color_white" android:orientation="vertical" android:paddingBottom="70dp" android:paddingEnd="140dp" android:paddingLeft="140dp" android:paddingRight="140dp" android:paddingStart="140dp" android:paddingTop="70dp" /> </example.multiscroll.MyHorizontalScrollView> </example.multiscroll.MultiScrollView> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="5dp"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
public class MainActivity extends Activity { private ScrollView scrollY; private GestureDetector gestureDetectorY; private RelativeLayout[][] sqContent; private int sizeI = 10; private int sizeJ = 20; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sqContent = new RelativeLayout[sizeI][sizeJ]; scrollY = (MultiScrollView) findViewById(R.id.field_y); gestureDetectorY = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { scrollX.smoothScrollBy((int) distanceX, 0); scrollY.smoothScrollBy(0, (int) (distanceY)); return true; } }); scrollY.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { gestureDetectorY.onTouchEvent(event); return true; } }); TableLayout table = (TableLayout) findViewById(R.id.table); TableRow row[] = new TableRow[sizeJ]; TableRow.LayoutParams paramsTableRow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT); paramsTableRow.setMargins(0, 50, 0, 50); paramsTableRow.weight = 1; for (int i = 0; i < sizeI; i++) { row[i] = new TableRow(this); row[i].setLayoutParams(paramsTableRow); row[i].setWeightSum(sizeJ); for (int j = 0; j < sizeJ; j++) { sqContent[i][j] = (RelativeLayout) getLayoutInflater().inflate(R.layout.sq, row[i], false); sqContent[i][j].setBackgroundResource(R.color.color_sq_action); sqContent[i][j].setOnClickListener(getOnClickListener()); row[i].addView(sqContent[i][j], j); } table.addView(row[i], i); } } private View.OnClickListener getOnClickListener() { return new View.OnClickListener() { @Override public void onClick(View v) { v.setBackgroundResource(R.color.color_black); } }; } }
Source: https://habr.com/ru/post/328450/
All Articles