1

我有一个这样的 Listfragment :

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));
        }
    }
}

这是我的适配器:

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;
    }
}

最后,在我的活动中

fragImages.SetImages(ListImage);

问题是列表视图只显示第一张图片。我查看了 ListImage 并且它包含 3 个项目,因此列表视图应该显示 3 个图像。我错过了什么吗?多谢。

4

2 回答 2

0

我怀疑你的物品在那里,只是看不见。检查您的R.layout.activity_torrent_detail_images并确认您的物品可以被看到。当您添加项目时,这些项目很可能被添加到前一个项目的右侧,离开了屏幕。

出于调试目的,我建议您列表中的每个项目的高度和宽度为 ,wrap_content以便您可以看到其他项目的去向。

于 2013-07-16T03:26:06.343 回答
-1

我猜这是因为您将 ListFragment 放在了 ScrollView 中。ScrollView 中的 ListFragment 或 ListView 会导致 ScrollView 无法正确计算 List* 的高度。

这里有一个解决方案,效果很好。

核心思想是重新实现ListView。将重新实现的 ListView 放入您的片段中(因此它不是 ListFragment)。然后片段可以正确显示。

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

重新实现的ListView如下:

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;
    }
}
于 2014-10-05T10:33:41.880 回答