Fixed the issue with images jumping randomly around the list.

This commit is contained in:
Robert Kolner committed 2013-02-09 01:38:54 +01:00
1 parent 7932de4e76
commit fae5cdad67
4 files changed
+60 -23

No files matched your search

+9 -2
View File
@@ -3,8 +3,14 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3px"
android:paddingBottom="3px">
android:paddingTop="6dp"
android:paddingBottom="6dp">
<TextView android:id="@+id/event_list_row_hidden_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
<FrameLayout android:id="@+id/event_list_row_image_fl"
android:layout_height="40dp"
@@ -19,6 +25,7 @@
/>
<ImageView android:id="@+id/event_list_row_image"
android:contentDescription="@string/event_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
+1
View File
@@ -19,4 +19,5 @@
<string name="BC">BokCaféen</string>
<string name="GB">Glassbaren</string>
<string name="billettluka">Billettluka</string>
<string name="event_image">Event-bilde</string>
</resources>
@@ -15,20 +15,48 @@ import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageLoader extends AsyncTask<String, Void, Drawable> {
private final static String TAG = ImageLoader.class.getName();
private final static ConcurrentMap<String, Drawable> cache = new ConcurrentHashMap<String, Drawable>();
private final ViewGroup parent;
private final int position;
private final ViewGroup row;
private final int imageId;
private int progressbarId = -1;
public ImageLoader(ViewGroup parent, int position, int imageViewId) {
this.parent = parent;
this.position = position;
private int hiddenTextViewId = -1;
private String correctText = "";
public ImageLoader(ViewGroup row, int imageViewId) {
this.row = row;
this.imageId = imageViewId;
}
public void setHiddenId(int hiddenTextViewId, String correctText) {
if (hiddenTextViewId < 0 || correctText == null) {
Log.w(TAG, "Invalid parameters to Imageloader.setHiddenId()");
return;
}
this.hiddenTextViewId = hiddenTextViewId;
this.correctText = correctText;
}
private boolean isHiddenIdCorrect(ViewGroup row) {
if (hiddenTextViewId < 0) {
return true;
}
TextView hiddenIdView = (TextView) row.findViewById(hiddenTextViewId);
CharSequence hiddenId = hiddenIdView.getText();
if (hiddenId != null) {
return this.correctText.equals(hiddenId.toString());
}
return false;
}
@Override
protected Drawable doInBackground(String... params) {
if (params.length > 1)
@@ -63,18 +91,15 @@ public class ImageLoader extends AsyncTask<String, Void, Drawable> {
protected void onPostExecute(Drawable result) {
super.onPostExecute(result);
if (result != null && parent != null) {
ViewGroup row = (ViewGroup)parent.getChildAt(position);
if (row != null) {
if (progressbarId > 0) {
View pb = row.findViewById(progressbarId);
pb.setVisibility(View.GONE);
}
ImageView image = (ImageView)row.findViewById(imageId);
image.setImageDrawable(result);
image.setVisibility(View.VISIBLE);
if (result != null && row != null && isHiddenIdCorrect(row)) {
if (progressbarId > 0) {
View pb = row.findViewById(progressbarId);
pb.setVisibility(View.GONE);
}
ImageView image = (ImageView)row.findViewById(imageId);
image.setImageDrawable(result);
image.setVisibility(View.VISIBLE);
}
}
@@ -84,24 +84,28 @@ public class EventListActivity extends BaseListActivity {
ListAdapter adapter = new ArrayAdapter<Event>(this, R.layout.event_list_row, R.id.event_list_row_text, events) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
ViewGroup row;
if (null == convertView) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.event_list_row, null);
row = (ViewGroup)inflater.inflate(R.layout.event_list_row, null);
} else {
row = convertView;
row = (ViewGroup)convertView;
}
final Event e = getItem(position);
/* Set hidden text: */
TextView hiddenView = (TextView) row.findViewById(R.id.event_list_row_hidden_text);
hiddenView.setText(Integer.toString(e.id));
/* Set images: */
String thumbnailUri = e.getThumbnail().toString();
ImageLoader loader = new ImageLoader(parent, position, R.id.event_list_row_image);
ImageLoader loader = new ImageLoader(row, R.id.event_list_row_image);
loader.setProgressbar(R.id.event_list_row_image_pb);
loader.setHiddenId(R.id.event_list_row_hidden_text, Integer.toString(e.id));
loader.execute(thumbnailUri);
/* Set text: */
TextView titleView = (TextView) row.findViewById(R.id.event_list_row_text);
titleView.setText(Html.fromHtml(e.title));