没有为ScrollView上滚动的每个像素调用onScrollChange()

时间:2017-11-06 12:31:58

标签: android android-scrollview onscrollchanged

我想在每滚动50个像素后添加新的子视图,但语句scrollY = 149缺少某些值,即scrollY = 151Log scrollY = 150scrollY = 150丢失了。因此,代码不会为scrollY运行,也不会添加新的子视图。

虽然它适用于scrollY的大多数值,但对于public class MainActivity extends AppCompatActivity implements ScrollViewListener { RelativeLayout parent; TextView height; ObservableScrollView scrollView; int position=1; View view1,view2; @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); parent = (RelativeLayout) findViewById(R.id.parent); scrollView = (ObservableScrollView) findViewById(R.id.scroll_view); LayoutInflater inflater = LayoutInflater.from(this); view1 = inflater.inflate(R.layout.education,null, false); view1.setId(R.id.parent+position); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.LEFT_OF,R.id.divider); params.topMargin = 100; params.rightMargin = 3; parent.addView(view1, params); position++; final Animation leftAnimation = new TranslateAnimation(view1.getX()-50,view1.getX(),view1.getY()+100,view1.getY()); leftAnimation.setDuration(1000); view1.setAnimation(leftAnimation); scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() { @Override public void onScrollChange(View view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { Log.d("scroll",scrollY+":"+oldScrollY); if((scrollY-oldScrollY)>0 && scrollY%50 == 0) { Log.d("scroll","abcghfg"); if(position%2==0) { RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.RIGHT_OF,R.id.divider); // params.topMargin = (position-1)*(view1.getLayoutParams().height)+position*100; params.topMargin = 30; params.leftMargin = 3; params.addRule(RelativeLayout.BELOW,parent.getChildAt(position-1).getId()); Log.d("scroll","abc"); view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.education,null,false); view2.setId(R.id.parent+position); Animation rightAimation = new TranslateAnimation(view2.getX()+50,view2.getX(),view2.getY()+100,view2.getY()); rightAimation.setDuration(1000); view2.startAnimation(rightAimation); parent.addView(view2, params); } else { RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.LEFT_OF,R.id.divider); //params.topMargin = (position-1)*(view1.getLayoutParams().height)+position*100; params.topMargin = 30; params.rightMargin = 3; params.addRule(RelativeLayout.BELOW,parent.getChildAt(position-1).getId()); Log.d("scroll","def"); view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.education,null,false); view2.setId(R.id.parent+position); Animation animation = new TranslateAnimation(view2.getX()-50,view2.getX(),view2.getY()+100,view2.getY()); animation.setDuration(1000); view2.startAnimation(animation); parent.addView(view2, params); } position++; } } }); } } 的值很少,但事件未被触发。

我该如何解决这个问题?或者还有其他方法可以实现这一目标。

{{1}}

提前致谢。

1 个答案:

答案 0 :(得分:0)

Android不会渲染滚动中的每个像素,因为它会对性能造成不利影响。如果滚动速度很慢,您将看到将为每个像素调用回调,但如果您快速滚动,则会在渲染触发时调用它。

在您的情况下,您需要检查自上次渲染上一个视图以来它是否渲染了50个或更多像素。