Java 操纵水平滚动视图的视图
正如我在上一个问题()中所写的,我使用HorizontalScrollView在一个简单的图像库中工作。现在我正在解决以下任务:要动态加载和显示图像,目标是在加载图像时,进度条必须显示在显示器上。因此,我使用android.os.AsyncTask加载图像。但首先,我为添加和滚动图像创建了自定义水平滚动视图,目标方法是从android.widget.HorizontalScrollView-android.lessons.custom_HorizontalScroll.ImageHorizontalScrollView#onFling的公共方法调用的processScroll,它看起来像:Java 操纵水平滚动视图的视图,java,android,xml,view,horizontalscrollview,Java,Android,Xml,View,Horizontalscrollview,正如我在上一个问题()中所写的,我使用HorizontalScrollView在一个简单的图像库中工作。现在我正在解决以下任务:要动态加载和显示图像,目标是在加载图像时,进度条必须显示在显示器上。因此,我使用android.os.AsyncTask加载图像。但首先,我为添加和滚动图像创建了自定义水平滚动视图,目标方法是从android.widget.HorizontalScrollView-android.lessons.custom_HorizontalScroll.ImageHorizont
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("IHSV", "GestureDetectorListener onFling event. " +
"Info scrollX " + getScrollX() +
" Info velocityX " + velocityX +
" Info ev1X " + e1.getAxisValue(MotionEvent.AXIS_X) +
" Info ev2X " + e2.getAxisValue(MotionEvent.AXIS_X));
ev1X = (int) e1.getAxisValue(MotionEvent.AXIS_X);
ev2X = (int) e2.getAxisValue(MotionEvent.AXIS_X);
processScroll();
return true;
}
processScroll方法是
private void processScroll() {
boolean isForward = ev1X > ev2X;
boolean isPaging = isForward ? getImageSizeList().get(imageIndex)[0] / 2 >= ev2X :
getImageSizeList().get(imageIndex)[0] / 2 <= ev2X;
Log.d("IHSV", "processScroll imageIndex: " + imageIndex + " isForward: " + isForward + " isPaging: " + isPaging);
if (isPaging) {
boolean isSmoothScroll;
// to page forward
if (isForward) {
isSmoothScroll = imageIndex + 1 != imageItems.length;
imageIndex = ++imageIndex % imageItems.length;
} else {
// to page back
isSmoothScroll = imageIndex - 1 >= 0;
imageIndex = imageIndex - 1 < 0 ? imageItems.length - 1 : --imageIndex;
}
boolean isLoad = imageItems[imageIndex].isLoad;
if (!imageItems[imageIndex].isLoad) {
// show a progress bar
innerLayout.addView(getPbl());
}
if (isSmoothScroll) {
if (isLoad) {
smoothScrollTo(imageIndex * displayMetrics[0], 0);
} else {
innerLayout.scrollTo(imageIndex * displayMetrics[0], 0);
}
} else {
scrollTo(imageIndex * displayMetrics[0], 0);
}
// load an image in the background task
if (!imageItems[imageIndex].isLoad) {
ImageLoaderTask imgLoader = new ImageLoaderTask(this, innerLayout);
imgLoader.execute(imageItems[imageIndex].id);
}
} else {
smoothScrollTo(imageIndex * displayMetrics[0], 0);
}
}
那么让我们来回答以下问题:
1) 我在processScroll方法中调用innerLayout.scrollTo(…),因为android.widget.HorizontalScrollView#smoothScrollTo不工作:它不滚动到进度条视图,为什么会这样?图像加载后(ImageLoaderTask#onPostExecute方法完成后),它会自动滚动
2) 因此,使用innerLayout.scrollTo(…)我得到了一个奇怪的行为:第一个滚动正确,下一个没有添加图像,它在加载图像的位置留下了黑色空间:)。所以我认为View#addView和View#removeView不能正常工作,但为什么呢?我应该调用水平滚动视图的任何刷新方法吗 很简单。我需要使用View.onLayout方法,该方法在添加视图后通过一个小的挂起调用。因为我重写了HorizontalScrollView#onLayout方法
<?xml version="1.0" encoding="utf-8"?>
<android.lessons.custom_horizontal_scroll.ImageHorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalScrollView"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- **the inner layout** -->
<LinearLayout android:orientation="horizontal"
android:id="@+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</LinearLayout>
</android.lessons.custom_horizontal_scroll.ImageHorizontalScrollView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_bar_layout"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar android:id="@+id/progress_bar"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
public class ImageLoaderTask extends AsyncTask<Integer, String, String> {
@Override
protected String doInBackground(Integer... params) {
// load an image
}
@Override
protected void onPostExecute(String result) {
// remove the progess bar
innerLayout.removeView(innerLayout.findViewById(R.id.progress_bar_layout));
// add the load image
innerLayout.addView(addImageView);
...
}
}
View getPbl() {
if (pbl == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
pbl = inflater.inflate(R.layout.progress_bar, this, false);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(displayMetrics[0],
displayMetrics[1]);
lp.gravity = Gravity.FILL_HORIZONTAL | Gravity.CENTER_VERTICAL;
pbl.setLayoutParams(lp);
}
return pbl;
}