Loading the correct Image in List View - android

I'm making an app that displays images from urls in a listview. I use a asynctask to get the image and place it into the imageviews. The way I currently have it is that an asynctask is called in the getview method. The do in background part gets the image from the url. The problem I'm having is when I scroll to an element in the listview, initially the wrong image is there but the correct image is quickly loaded. Why is this happening?
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitStart"
android:src="#drawable/ic_launcher" />
</LinearLayout>

Make sure that you are setting your ImageView to null as soon as you declare it. It's hard to tell without knowing what the image is, but it's possible that the ListView is reusing old images. Reference below:
wrong images are displaying first and then correct images are displaying during scrolling of listview

Related

Android Navigation Drawer Animation Lags/Slows down when an Image View is added over Recycler view

I am having a recyclerview inside a relative layout and the app is working fine, the navigation drawer is smooth. But I added an Imageview over the recycler view so that I can hide and show based on the availability of the data. But soon as I have added the Imageview the navigation drawer becomes very slow. And when I remove the Imageview it again works smooth. The image size is just 38kb. Can someone tell me how to show empty state in an efficient way
This is my Layout with the ImageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.test.sample.itemHistory"
tools:showIn="#layout/app_bar_item_history"
android:background="#ffffff">
<android.support.v7.widget.RecyclerView
android:id="#+id/materialList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageViewNoItems"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/noItems" />
</RelativeLayout>
Thanks in Advance
In my case, I try to load Image (PNG File within the Project Drawable Resource Folder) to my RecycleView's ImageView as well using Picasso and I facing the same problem where the Navigation Drawer sliding animation is being slow down.
I found that the main issue is because my Image File is quite Large so the Picasso load the Full Size image into the RecycleView's ImageView which would cause the Lag.
My Solution are: (I am using KOTLIN Language)
Re-size the image using Picasso built in resize function
Without Resize (Which Cause Lag):
Picasso.get().load(R.drawable.icon).into(holder.view.ImageView)
Resized (Solve Lag Problem):
Picasso.get().load(R.drawable.icon).resize(400,400).centerCrop().into(holder.view.ImageView)
Use setImageDrawable instead of Picasso
This Method does not required to Resize the image:
holder.view.ImageView.setImageDrawable(holder.view.ImageView.context.resources.getDrawable(R.drawable.icon))
used picasso or glide library for image loading.. and also add one line in your manifest and your project become smooth as before.
<application
android:largeHeap="true">
</application>
largeHeap is used to avoid outofmemory exception while loading images.

How to use ViewFlipper to dynamically add images in AppWidget like ad rotation

I'm trying to make AppWidget shows rotation images which can automatically change every 5 seconds,and I think ViewFlipper might be able to reach this goal.
But I get images from server and can't just put them as ImageView in .xml file under nested ViewFlipper like this:
<ViewFlipper android:id="#+id/viewflipper"
android:layout_width="match_parent"
android:layout_height="match_content"
android:autostart="true"
android:flipinterval="5000" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/drawable1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/drawable2"/>
</ViewFlipper>
I need to add images dynamically, and shows one picture at one time. If there's more than one image, they need to be shown on AppWidget by turns.
Hope somebody could help me with this, thanks!
I assume by "dynamically" you mean there could be an arbitrary number of items. What you probably want is AdapterViewFlipper. You can follow this guide for building an AppWidget whose content is backed by an adapter.

Listview Header Wrap_content causing weird layout

I have a listview with header attach using method addHeaderView(header);
same listview layout loading different data for same fragment depdens upon navigation drawer category.
header is having its own layout
{
**R.layout.header_layout**
<?xml version="1.0" encoding="utf-8"?>
<com.loopj.android.image.SmartImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/desc"
android:scaleType="centerCrop"
android:baselineAlignBottom="true"
android:src="#drawable/blankk" />
}
Note: SmartImageView is library that loads image from url .
my problem is the, images that are loading into smartimageview (listview header) are not having fixed height so the layout for different fragment are different i.e not consistent .if i assign height as say 300dp images are getting crop.
plz help me to get get header of same height with images not getting crop.
Please remove android:baselineAlignBottom="true" from your smart imageview and use fix width. I hope it will works

How to set these two different images using position in android?

I have two different images and would like to set them using different views but overlap slightly to match the below images. I also want to be able to set a different OnClickListener for each of them. I know with iOS I can set the view positions using x and y values but I'm not sure how to do this in Android.
How can I go about doing this?
Desired result:
Image One
Image Two
Try set those two images as ImageView's and put them inside a FrameLayout. That way you can set them as you want, So they can overlap one another. That way you can set an clickable and onClick properties for both of them separately:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/iFirstImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#drawable/sort_image_1"
android:clickable="true"
android:onClick="setFirstImageClick"
android:src="#drawable/sort_image_1" />
<ImageView
android:id="#+id/iSecondImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#drawable/sort_image_2"
android:clickable="true"
android:onClick="setSecondImageClick"
android:src="#drawable/sort_image_2" />
And create methods setFirstImageClick and setSecondImageClick in your activity to decide what each image click should do.
You can place the first image however you want, and then in the xml for the second image use
android:layout_toRightOf="firstImageId"
You can set each onClick separately after that.

Adding background in Activity makes my view seem faded

I have an Activity with a ListView.I want to set as a background a simple white picture with a logo in the right lower corner.My problem is that when i set as background my picture it kinda 'sits' in front of my listView.Making the textViews seem faded.How can i effectively add a background without these side effects?Thank you for your time.
EDIT:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg">
<dragNDropAdapter.DragNDropListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</dragNDropAdapter.DragNDropListView>
</RelativeLayout>
Put imageview & listview in relative layout.
Set attribute for your image where you want to place it and for list as well.

Categories

Resources