无法使用UIL设置图像的大小

时间:2014-07-14 07:59:20

标签: android listview universal-image-loader custom-lists horizontallist

enter image description here

我有两个自定义列表视图,一个是垂直的,另一个是水平的。横向是垂直列表。垂直列表的一部分在

之下
    <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"
    />

我已经使用这个库来处理水平列表视图。水平列表视图的行模板就是这个。

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

当我使用UIL库在My array adapter中的水平列表中填充数据时,它会根据textview的大小显示小图像和图像宽度的变化。当我在textview中写长文本时,它显示我的图像宽度等于textview的宽度。

这是我的水平列表视图的自定义适配器。

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) {

        }

});
}



}

我希望我的图像宽度与父级匹配,平均值等于我的屏幕宽度和高度等于水平列表视图的高度,在我的情况下是155dp。

1 个答案:

答案 0 :(得分:0)

使用以下代码

LayoutParams param=(LayoutParams) holder.IVImage.getLayoutParams();
        param.width=image_dim;
        param.height=image_dim;
holder.Places_iv.setLayoutParams(param);