我LinearLayout
内有ScrollView
。 LinearLayout
有100 FrameLayouts
。单击按钮时,我会在FrameLayout
的中间位置插入新的BackgroundWorker
s。为了保持当前查看的FrameLayout
的位置,我将ScrollView
滚动到等于当前偏移的偏移量加上新插入的FrameLayout
的高度。
虽然此过程在BackgroundWorker
中运行,但如果用户以正的速度翻转ScrollView的内容,则同样FrameLayout
会一次又一次地出现。例如,你从FrameLayout
编号为7的小速度开始投掷,8从底部出现,但在再次出现之前7出现。用户滚动也可以正常工作。问题只在于甩尾。
我发现,在OnScrollChanged
事件中,oldt
事件t
参数大于FrameLayouts
,这是意料之外的,因为投掷速度为正。
下面我按顺序从第5,第6,第7,第8 ......位置插入新的public class MainActivity : Activity {
ScrollView1 sV;
Button btn;
LinearLayout linearLayout;
BackgroundWorker worker;
Handler handler;
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
sV = FindViewById<ScrollView1>(Resource.Id.scrollView1);
btn = FindViewById<Button>(Resource.Id.pagedownbutton);
btn.Click += Btn_Click;
sV.SetBackgroundColor(Android.Graphics.Color.White);
linearLayout = new LinearLayout(ApplicationContext);
linearLayout.Orientation = Orientation.Vertical;
worker = new BackgroundWorker();
worker.DoWork += Worker_DoWork;
handler = new Handler();
for (int i = 0; i < 100; i++) {
TextView txt = new TextView(ApplicationContext);
txt.Text = i.ToString();
txt.TextSize = 50;
txt.SetTextColor(Color.White);
txt.Gravity = Android.Views.GravityFlags.Center;
FrameLayout frameLayout = new FrameLayout(ApplicationContext);
frameLayout.SetBackgroundColor(Android.Graphics.Color.Blue);
frameLayout.AddView(txt);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(700, 1200);
layoutParams.SetMargins(0, 5, 0, 0);
layoutParams.Gravity = Android.Views.GravityFlags.Center;
frameLayout.LayoutParameters = layoutParams;
linearLayout.AddView(frameLayout, i);
}
sV.AddView(linearLayout);
}
private void Worker_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 0; i < 1000; i++) {
TextView txt = new TextView(ApplicationContext);
txt.Text = "i" + i.ToString();
txt.TextSize = 50;
txt.SetTextColor(Color.White);
txt.Gravity = Android.Views.GravityFlags.Center;
FrameLayout frameLayout = new FrameLayout(ApplicationContext);
frameLayout.SetBackgroundColor(Android.Graphics.Color.Blue);
frameLayout.AddView(txt);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(700, 1200);
layoutParams.SetMargins(0, 5, 0, 0);
layoutParams.Gravity = Android.Views.GravityFlags.Center;
frameLayout.LayoutParameters = layoutParams;
//Inducing some delay so that the background process will run long
for (int j = 0; j < 10000000; j++) {
}
//Adding FrameLayout and scrolling
handler.Post(() => {
linearLayout.AddView(frameLayout, 5 + i);
if (sV.ScrollY >= (5 + i) * 1205)
sV.ScrollTo(0, 1205 + sV.ScrollY);
});
}
}
private void Btn_Click(object sender, System.EventArgs e) {
worker.RunWorkerAsync();
}
}
public class ScrollView1 : ScrollView{
public ScrollView1(Context context):base(context) {
}
public ScrollView1(Android.Content.Context context, Android.Util.IAttributeSet attr) : base(context, attr) {
}
protected override void OnScrollChanged(int l, int t, int oldl, int oldt) {
}
public override void Fling(int velocityY) {
base.Fling(velocityY);
}
}
。
FrameLayout
如何在没有滚动故障的情况下按顺序显示{{1}},同时在后台线程中进行程序化滚动?