2013-07-16 106 views
0

我有這樣的Listfragment: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

是否在ImageAdapter圖像包含3個項目? – sandrstar

+0

是的,它有3個項目 –

+0

每個代碼看起來像setAdapter()在SetImages()之前被調用(onCreateView()從片段SetImages()之前調用)。是這樣嗎? – sandrstar

回答

-1

我懷疑你的物品在那裏,只是在視線之外。看看你的R.layout.activity_torrent_detail_images並確認你的物品能夠被看到。很有可能當您添加項目時,這些項目將被添加到前一項目的右側,從屏幕上移開。

爲了便於調試,我建議您的列表中的每個項目的高度和寬度爲wrap_content,以便您可以看到其他項目的去向。

+0

感謝您的回答,我沒有想到這一點。我會盡快嘗試,但爲什麼這些項目都在同一行? –

+0

@ user2578055如果方向是水平方向而不是垂直方向,則列表項可能位於同一行。 –

+0

方向設置爲垂直 –

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

的rerealized 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; 
    } 
} 
+0

雖然此鏈接可能回答此問題,但最好在此處包含答案的基本部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –

+0

@DmitryGinzburg謝謝你的建議。 –

+0

您隨時歡迎您! –