我有一个像这样的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);
问题是listview只显示第一张图片。我查看了ListImage,它包含3个项目,因此listview应该显示3个图像。我错过了什么吗?非常感谢。
答案 0 :(得分:-1)
我怀疑你的物品在那里,就在视线之外。查看您的R.layout.activity_torrent_detail_images
并确认您的商品可以看到。很有可能当您添加项目时,项目将被添加到上一项的右侧,离开屏幕。
出于调试目的,我建议列表中的每个项目的高度和宽度均为wrap_content
,以便您可以看到其他项目的去向。
答案 1 :(得分:-1)
我想这是因为你将ListFragment放在ScrollView中。 ScrollView中的ListFragment或ListView将导致ScrollView无法正确计算List *的高度。
有一个解决方案here,效果很好。
基本思想是重新实现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;
}
}