Manipulating views of HorizontalScrollView



As I wrote in the previous question (Simple image gallery with HorizontalScrollView) I work at a simple image gallery with use HorizontalScrollView. Now I'm solving the following task: to load and show images dynamically, the goal is while an image is load the progress bar have to be shown on the display. So I use android.os.AsyncTask for loading an image. But at first I made custom horizontal scroll view for adding and scrolling images, the goal method is processScroll that is called from public method of android.widget.HorizontalScrollView - android.lessons.custom_horizontal_scroll.ImageHorizontalScrollView#onFling, it looks like:



@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;
}


and the processScroll method is



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);
}
}


Where as you can see before I make a loading image I add a progress bar to my inner layout (the single child of the horizontal scroll view). The main layout is



<?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>


The progress bar layout is



<?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>


The task of loading image looks like that



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);
...
}

}


I inflate the progress bar in the following method:



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;
}


So let's go to the questions: 1) I call innerLayout.scrollTo(...) in the processScroll method because android.widget.HorizontalScrollView#smoothScrollTo doesn't work: it doesn't scroll to the progress bar view, why does it behave that way? It scrolls itself after the image is load (after the ImageLoaderTask#onPostExecute method is accomplished).


2) So with innerLayout.scrollTo(...) I'm getting a strange behavior: the first scroll works right, the next one doesn't add an image it leave black space :) in the place where the load image should be. So I think View#addView and View#removeView don't work correctly, but Why? Should I call any refresh methods of the horizontal scroll view?


No comments:

Post a Comment