Swipecards
Swipecards copied to clipboard
Redraw
Hi.
I've tried using this with a custom BaseAdapter and custom views. The views have an ImageView in them, and I am using AsyncTask to populate them with downloaded images. As soon as I load an image into the ImageView, all the views are inflated again, and it's an infinite loop. As soon as I take out the image loading, everything works ok.
I've attached the sources below,
Main layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<com.lorentzos.flingswipe.SwipeFlingAdapterView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/adapterView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rotation_degrees="16"
app:max_visible="10"
app:min_adapter_stack="4" />
</LinearLayout>
Main activity:
public class TestActivity extends Activity {
public final static String TAG = "TEST_ACTIVITY";
private TestAdapter testAdapter;
private SwipeFlingAdapterView adapterView;
private ArrayList<String> itemsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Log.i(TAG, "onCreate");
itemsList = new ArrayList<String>();
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
itemsList.add("http://logo.png");
adapterView = (SwipeFlingAdapterView) findViewById(R.id.adapterView);
testAdapter = new TestAdapter(TestActivity.this, itemsList);
adapterView.setAdapter(testAdapter);
adapterView.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
//al.remove(0);
itemsList.remove(0);
testAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
Toast.makeText(TestActivity.this, "Left", Toast.LENGTH_SHORT).show();
}
@Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(TestActivity.this, "Right", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
// Ask for more data here
Log.d(TAG, "loading more items");
//al.add("XML ".concat(String.valueOf(i)));
testAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
//i++;
}
});
}
}
Custom view for the adapter:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="60dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/testView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center" />
</FrameLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
The adapter:
public class TestAdapter extends BaseAdapter {
public final static String TAG = "TEST_ADAPTER";
private Context context;
private ArrayList<String> items;
protected ViewHolder holder;
public TestAdapter (Context context, ArrayList<String> items) {
this.context = context;
this.items = items;
}
/**
* add one item to list
*/
public void add(String item) {
this.items.add(item);
}
/**
* add one item to list
*/
public void add(int position, String item) {
this.items.add(position, item);
}
/**
* remove one item from list
*/
public void remove(String item) {
this.items.remove(item);
}
public void clear() {
this.items.clear();
}
@Override
public int getCount() {
return this.items.size();
}
@Override
public String getItem(int position) {
return this.items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final String item = getItem(position);
Log.d(TAG, "item: " + item + ", position: " + position);
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.test_view, parent, false);
holder = new ViewHolder();
holder.testView = (ImageView) view.findViewById(R.id.testView);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// load the image
if (item != null && !item.equals("")) {
new LoadImage(holder).execute(item);
}
return view;
}
public class ViewHolder {
ImageView testView;
}
private class LoadImage extends AsyncTask<String, Void, Bitmap> {
private ViewHolder holder;
public LoadImage(ViewHolder holder) {
this.holder = holder;
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bmp = null;
try {
URL urlValue = new URL(params[0]);
bmp = BitmapFactory.decodeStream(urlValue.openConnection().getInputStream());
if (bmp != null) {
return bmp;
}
} catch (java.io.IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bmp) {
if (bmp != null) {
holder.testView.setImageBitmap(bmp);
}
}
}
}
Sorry for the long text, it's as detailed as possible :)
Any suggestions?
How about using a ready async image loader? e.g. Ion looks to work great with the library :)
https://github.com/koush/ion
I do not think that the way the images are loaded is important. If I try using google maps fragments the same thing happens.
I think it's the same source with #9 . Something is happening and it causes redrawing. Although I checked the sources I hadn't figured out what's wrong to fix this