1

I have a Listfragment like this :

private ArrayList<String>images;
private View view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.activity_torrent_detail_images, container, false);
    SetAdapter();
    return view;
}

public void SetImages(ArrayList<String> images) {
    this.images = images;
}

public void SetAdapter() {
    if(images!= null && view != null) {
        if(this.getActivity() != null) {
            this.setListAdapter(new ImageAdapter(this.getActivity(),this.images));
        }
    }
}

here's my adapter:

public class ImageAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final ArrayList<String> images;
    App app;

    public ImageAdapter(Context context,
            ArrayList<String> images) {
        super(context, R.layout.search_row, images);
        this.context = context;
        this.images = images;
        app =((App)context.getApplicationContext());
    }

    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.imagesrow, parent, false);
        ImageView img = (ImageView)rowView.findViewById(R.id.imgImage);
        app.imageLoader.displayImage(images.get(position), img);//this load the image from the http

        return rowView;
    }
}

Finally, in my activity

fragImages.SetImages(ListImage);

The problem is that the listview only shows the first image. I've look in the ListImage and it contains 3 items, so the listview should show 3 images. Did I miss anything? thanks a lot.

sandrstar
  • 12,503
  • 8
  • 58
  • 65

2 Answers2

0

I suspect that your items are there, just out of sight. Check out your R.layout.activity_torrent_detail_images and confirm that your items are able to be seen. It is quite likely that as you add items, the items are being added to the right of the previous item, going off the screen.

For debugging purposes, I would recommend that each item in your list have a height and width of wrap_content so that you can see where the other items are going.

Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68
-1

I guess it's because you put the ListFragment in a ScrollView. ListFragment or ListView in a ScrollView will cause the ScrollView cannot calculate correctly the height of the List*.

There is a solution here, which works perfectly.

The essential thought is to rerealize the ListView. Put the rerealized ListView in your fragment (so it's not a ListFragment). Then the fragment can be shown correctly.

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <fragment android:name="com.foo.BarFragment"
                android:id="@+id/bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </fragment>
        </LinearLayout>

</ScrollView>

The rerealized ListView is as followed:

public class NestedListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener {

    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
    private int listViewTouchAction;

    public NestedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        listViewTouchAction = -1;
        setOnScrollListener(this);
        setOnTouchListener(this);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, -1);
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int newHeight = 0;
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            ListAdapter listAdapter = getAdapter();
            if (listAdapter != null && !listAdapter.isEmpty()) {
                int listPosition = 0;
                for (listPosition = 0; listPosition < listAdapter.getCount()
                        && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
                    View listItem = listAdapter.getView(listPosition, null, this);
                    //now it will not throw a NPE if listItem is a ViewGroup instance
                    if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(
                                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    }
                    listItem.measure(widthMeasureSpec, heightMeasureSpec);
                    newHeight += listItem.getMeasuredHeight();
                }
                newHeight += getDividerHeight() * listPosition;
            }
            if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
                if (newHeight > heightSize) {
                    newHeight = heightSize;
                }
            }
        } else {
            newHeight = getMeasuredHeight();
        }
        setMeasuredDimension(getMeasuredWidth(), newHeight);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
            if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
                scrollBy(0, 1);
            }
        }
        return false;
    }
}
Community
  • 1
  • 1
Remi Liu
  • 91
  • 3