[英]Manipulating views of HorizontalScrollView
正如我在上一個問題( 使用HorizontalScrollView的簡單圖像庫 )中所寫的那樣,我在使用HorizontalHorizonView的簡單圖像庫中工作。 現在,我正在解決以下任務:要動態加載和顯示圖像,目標是在加載圖像時,必須在顯示器上顯示進度條。 因此,我使用android.os.AsyncTask加載圖片。 但是首先,我制作了用於添加和滾動圖像的自定義水平滾動視圖,目標方法是從android.widget.HorizontalScrollView-android.lessons.custom_horizontal_scroll.ImageHorizontalScrollView#onFling的公共方法調用的processScroll ,它看起來像:
@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);
}
}
如您所見,在制作加載圖像之前,我在內部布局(水平滾動視圖的單個子項)中添加了進度條。 主要布局是
<?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;
}
因此,讓我們轉到以下問題:1)我在processScroll方法中調用innerLayout.scrollTo(...) ,因為android.widget.HorizontalScrollView#smoothScrollTo無法正常工作:它無法滾動到進度欄視圖,為什么呢?這樣行嗎 加載圖像后(完成ImageLoaderTask#onPostExecute方法之后),它會自行滾動。
2)因此,使用innerLayout.scrollTo(...),我得到了一個奇怪的行為:第一個滾動條正常運行,下一個滾動條未添加圖像,它在加載圖像應放置的地方留有黑色空間:)。 。 所以我認為View#addView和View#removeView無法正常工作,但是為什么呢? 我應該調用水平滾動視圖的任何刷新方法嗎?
很簡單。 我需要使用View.onLayout方法,該方法在添加視圖后會以小的待處理狀態調用。 由於我重寫了HorizontalScrollView#onLayout方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.