Domanda

I'm using HorizontalListView by dev-smart (from here) to implement a horizontal list view of images with subtext like that:

What I want.

The problem is that each itemview of the list is as wide as screens' width. AdjustViewBounds is set true but doesn't work... I get this:

What I get

I tried:

  • Fix width and height to the image and convertView in the getView() method
  • Fix width to image and RelativeLayout in XML
  • Fix width to child in an HorizontalListView's method

May be the HorizontalListView is bugged? Is there other way to do that? Previously I use horizontal ListView inside horizontal ListView but this has a problem. It loads all pictures even they are never sawn.

Can anybody help me? Thank you!!

This is my custom adapter's getView() method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.item_actor, null);

        holder.img = (ImageView) convertView.findViewById(R.id.actor_img);
        holder.tittle = (TextView) convertView.findViewById(R.id.actor_txt);
        convertView.setTag(holder);
    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.img.setImageResource(R.drawable.actor_thumb);
    holder.tittle.setText(getItem(position).getName());
    convertView.setLayoutParams(new LayoutParams(150,190));
    ImageDownloader.getInstance(mContext).download(getItem(position).getPhotoUrl(), holder.img,
            R.drawable.actor_thumb);
    return convertView;

}

This, the item view to be inflated:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/actor_img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_launcher" />

<net.asturdroid.datamovie.widgets.TextViewRoboto
    android:id="@+id/actor_txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@color/text_over_image_background"
    android:gravity="center"
    android:maxLines="3"
    android:textColor="@color/text_over_image" />

</RelativeLayout>

And this, the HorizontalList item:

        <com.devsmart.android.ui.HorizontalListView
            android:id="@+id/casting_view_list"
            android:layout_width="wrap_content"
            android:layout_height="190dp" /* wrap_content doesn't works. */>
        </com.devsmart.android.ui.HorizontalListView>
È stato utile?

Soluzione

I think it's a bug on HorizontalListView class.

I change

child.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
    MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));

to

child.measure(MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.AT_MOST),
    MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.AT_MOST)); 

on HorizontalListView.java and it works for me.

I hope helps someone else.

Altri suggerimenti

I had the same issue what I did was change the width of each element on the item view to be inflated:

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" >

   <ImageView
       android:id="@+id/actor_img"
       android:layout_width="150dp"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:adjustViewBounds="true"
       android:scaleType="centerCrop"
       android:src="@drawable/ic_launcher" />

   <net.asturdroid.datamovie.widgets.TextViewRoboto
       android:id="@+id/actor_txt"
       android:layout_width="150dp"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true"
       android:background="@color/text_over_image_background"
       android:gravity="center"
       android:maxLines="3"
       android:textColor="@color/text_over_image" />

    </RelativeLayout>

I hope this helps

I have used the same HorizontalListView. I have solved the issue with the below code in getView() method of MyAdapter class.

convertView = getLayoutInflator().inflate(R.layout.row_item, parent, false);

Maybe you can try this one,it works for me. Here is the link of github:https://github.com/TomIsYourName/Android-HorizontalListView

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top