scrollview 子控件滑动速度不一致
先来个布局图:
向上滑动时,图片向上划出的速度较下面的蓝色view慢。
现在我们先来看布局文件:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<com.example.testscrollview.Myscrollview
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="@drawable/img" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_below="@id/layout"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="3dp"
android:background="#acfcfc" />
</LinearLayout>
</RelativeLayout>
</com.example.testscrollview.Myscrollview>
</LinearLayout>
注意要想实现imageview滑动速度比下面的慢 imageview要在外面套一层布局,因为scrollto()方法只是让布局里面的内容滑动。
再来看自定义的的scrollview:
package com.example.testscrollview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class Myscrollview extends ScrollView {
private ScrollviewListener scrollViewListener = null;
public Myscrollview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Myscrollview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Myscrollview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void computeScroll() {
// TODO Auto-generated method stub
super.computeScroll();
}
@Override
public void fling(int velocityY) {
super.fling(velocityY);
}
public void setScrollViewListener(ScrollviewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
// TODO Auto-generated method stub
//super.onScrollChanged(x,y,oldx,oldy);
if(scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
}
以及自定义监听接口:
package com.example.testscrollview;
public interface ScrollviewListener {
void onScrollChanged(Myscrollview scrollView, int x, int y, int oldx, int oldy);
}
接下来就是main了,在这里重新滑动方法:
package com.example.testscrollview;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
public class MainActivity extends Activity implements ScrollviewListener{
Myscrollview scrollView;
ImageView imageView;
RelativeLayout layout;
//Myscrollview scrollView_img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView=(Myscrollview) findViewById(R.id.scrollview);
// scrollView_img=(Myscrollview) findViewById(R.id.scrollview_img);
scrollView.setScrollViewListener(this);
// scrollView_img.setScrollViewListener(this);
layout=(RelativeLayout) findViewById(R.id.layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onScrollChanged(Myscrollview scrollView1, int x, int y,
int oldx, int oldy) {
Log.e("fc", x+" "+y);
layout.scrollTo(x, -y/3); //注意这里是负的 否则比下面的快
/* else if(scrollView1 == scrollView_img) {
scrollView.scrollTo(x, y*3);
} */
}
}
源码:下载