2012-08-26 43 views
6

我的應用程序中有一個GridView,我想在其中顯示文本和複選框,就像電子郵件收件箱頁面一樣。我使用適配器,但是當我顯示超過15個元素時,頂部行的文本和複選框消失,因此當我再次向上滾動時,它們不可見。這裏是我的代碼GridView內容在滾動期間消失

public class EmployeeAdaptor extends BaseAdapter { 
Context context; 
String [] table; 
int pos; 
int formatOfTable; 
public EmployeeAdaptor(Context c,String []tab,int numberOfItems,int format, boolean[] sel) { 
    context = c; 
    table = tab; 
    items = numberOfItems; 
    formatOfTable = format; 
    //ifformat is 0 then show text view in first column 
    //if it is 1 then show radio button in first column 
    //if it is 2 then show check box in first column. 
    pos = 0; 
} 
public int getCount() { 
    // 
    return items; 
} 

public Object getItem(int position) { 
    // 
    return position; 
} 

public long getItemId(int position) { 
    // 
    return position; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 

    View v; 
    TextView text = new TextView(context); 
    RadioButton radio = new RadioButton(context); 
    CheckBox check = new CheckBox(context); 

    if(convertView == null){ 
     v = new View(context); 
    } 
    else{ 
     v = convertView; 
    } 

    if(formatOfTable==2 && position%5==0 && position/5!=0){ 
     check.setId(position/5); 
     v = check; 

    } 
    else if(formatOfTable==0 && position%5==0 && position/5!=0){ 
     text.setText(""); 
     v = text; 
     //To set blank text at first position when no need of check 
    } 
    else{ 
     if(position%5!=0){ 
      try{ 
       v = text; 
       text.setText(table[pos]); 
       text.setTextColor(Color.BLACK); 
       text.setTextSize(20); 
       pos++; 
      } 
      catch(Exception e){ 

      } 
     } 
    } 
    if(position/5==0){ 
     text.setTypeface(Typeface.DEFAULT_BOLD); 
    } 
    return v; 
} 
    } 

電話適配器類如:

table.setAdapter(new EmployeeAdaptor(this, empTable, numberofboxes, 0, selected)); 
//OR 
table.setAdapter(new EmployeeAdaptor(this, empTable, numberofboxes, 1, selected)); 

佈局XML文件是

<GridView 
    android:id="@+id/gridView1" 
    android:layout_width="wrap_content" 
    android:layout_height="360dp" 
    android:layout_marginTop="40dp" 
    android:numColumns="5" android:verticalSpacing="35dp"> 
</GridView> 
+0

可以顯示適配器使用的xml佈局嗎? – Vicent

+0

適配器動態定義對象,因此不需要XML佈局。 – ADCDER

回答

-1

我想在GridView只是充滿並自動向下滾動(所以當您添加新項目時,在頂部的第一項「離開」)。是否有超過15個項目的空間?尤其是因爲您將GridView的高度固定爲360dp?您可以爲GridView設置一個背景顏色進行調試,以查看GridView獲取的空間量以及前15個項目佔用的空間量。

+0

GridView的高度設置爲360dp並不重要,因爲它具有滾動選項。它也可以存儲50行,但當我滾動它時,上面的行變得不可見。 – ADCDER

+0

我想我沒有正確理解你。如果它滾動,則部分變得不可見。這是我所期望的。 – Jochem

1

問題似乎是您沒有正確回收視圖。以下最簡單的例子是基於你的代碼的簡化(我已經擺脫了一些成員和一些條件塊)。只考慮兩種單元,TextViewCheckBox

public class EmployeeAdaptor extends BaseAdapter { 
    private Context context; 
    public String [] table; 
    private int items; 

    public EmployeeAdaptor(Context c,String []tab,int numberOfItems) { 
     context = c; 
     items = numberOfItems; 
    } 

    @Override 
    public int getCount() {return items;} 

    @Override 
    public Object getItem(int position) {return position;} 

    @Override 
    public long getItemId(int position) {return position;} 

    @Override 
    public int getItemViewType(int position) { 
     int viewType; 
     if (position%5 == 0 && position/5 != 0) { 
      // Positions 5, 10, 15... 
      // Return a CheckBox 
      viewType = 0; 
     } else { 
      // Return a TextView 
      viewType = 1; 
     } 
     return viewType; 
    } 

    @Override 
    public int getViewTypeCount() { 
     return 2; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v; 
     if (convertView == null) { 
      if (position%5 == 0 && position/5 != 0) { 
       // Positions 5, 10, 15... 
       // Return a CheckBox 
       v = new CheckBox(context); 
      } else { 
       // Return a TextView 
       v = new TextView(context); 
      } 
     } else { 
      v = convertView; 
     } 
     if (position < 5) { 
      ((TextView)v).setTypeface(Typeface.DEFAULT_BOLD); 
      ((TextView)v).setText(table[position]); 
     } else if (position%5 == 0 && position/5 != 0) { 
      // Positions 5, 10, 15... 
      ((CheckBox)v).setId(position/5); 
     } else { 
      ((TextView)v).setTypeface(Typeface.DEFAULT); 
      ((TextView)v).setText(table[position]); 
     } 
     return v; 
    } 
} 

請注意使用getViewTypeCountgetItemViewType。當你的getView處理不同類型的視圖時,它們可以幫助你。