
I have two custom listviews, one is vertical and other is horizontal. And horizontal is inside vertical list. Part of vertical list is below
<com.devsmart.android.ui.HorizontalListView
android:id="@+id/hlistview"
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_margin="6dp"
android:background="#fff"
/>
I've used this library to handle horizontal list view. Row template for horizontal list view is this.
<?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="150dp"
android:padding="6dip"
android:orientation="vertical" >
<TextView
android:id="@+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="12sp"
android:gravity="center_horizontal"
android:text="likes"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dip"
android:orientation="vertical" >
<ImageView
android:id="@+id/IVImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/stalker"
/>
</LinearLayout>
</LinearLayout>
When i populate data in horizontal list inside My array adapter using UIL library it shows me small images and image width changes according to the size of textview. Mean when i write long text in textview it shows me image width equal to the width of textview.
This is my custom adapter for my horizontal listview.
public class HomeHoriLVAdapter extends ArrayAdapter<HomeSliderClassForAdapter> {
Context context;
public ImageLoader imageLoader;
DisplayImageOptions options;
public HomeHoriLVAdapter(Context context, int resource,
List<HomeSliderClassForAdapter> objects) {
super(context, resource, objects);
File cacheDir = StorageUtils.getOwnCacheDirectory(context, "MyFolderCache");
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
// You can pass your own memory cache implementation
.diskCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
//imageLoader.init(ImageLoaderConfiguration.createDefault(a));
// imageLoader=new ImageLoader(activity.getApplicationContext());
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.stalker_logo_imageload)
.cacheInMemory(true)
.cacheOnDisk(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
// https://github.com/nostra13/Android-Universal-Image- Loader/blob/master/library/src/com/nostra13/universalimageloader/core/DisplayImageOptions.java
// link for more functions of DisplayImageOptions();
this.context = context;
}
private class ViewHolder {
TextView txtDescription;
ImageView IVImage;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
HomeSliderClassForAdapter rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.horizontal_listitem, null);
holder = new ViewHolder();
holder.txtDescription = (TextView) convertView.findViewById(R.id.txtDescription);
holder.IVImage = (ImageView) convertView.findViewById(R.id.IVImage);
holder.IVImage.getLayoutParams().width = LayoutParams.MATCH_PARENT;
holder.IVImage.getLayoutParams().height = LayoutParams.MATCH_PARENT;
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
Session asd = new Session(getContext());
asd.getAccessToken();
holder.txtDescription.setText(rowItem.Description);
display(holder.IVImage, rowItem.PhotoURL.concat("?access_token=" + asd.getAccessToken()));
return convertView;
}
public void display(ImageView img, String url, final ProgressBar spinner)
{
img.getLayoutParams().width = LayoutParams.MATCH_PARENT;
img.getLayoutParams().height = LayoutParams.MATCH_PARENT;
imageLoader.displayImage(url, img, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
public void display(ImageView img, String url)
{
imageLoader.displayImage(url, img, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
}
I want my image width to match parent, mean equals to my screen width and height equals to the height of horizontal list view which in my case is 155dp.
android:scaleType="centerCrop".- nostra13