【问题标题】:GridView content disappears during scrollingGridView 内容在滚动期间消失
【发布时间】:2012-08-27 17:59:20
【问题描述】:

我的应用程序中有一个GridView,我想在其中显示文本和复选框,就像电子邮件收件箱页面一样。我为此使用了一个适配器,但是当我显示超过 15 个元素时,顶行的文本和复选框消失了,所以当我再次向上滚动时,它们不可见。这是我的代码

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>

【问题讨论】:

  • 可以显示适配器使用的xml布局吗?
  • Adapter 动态定义对象,所以这里不需要 XML 布局。

标签: android android-gridview


【解决方案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 处理不同类型的视图时,它们会为您提供帮助。

【讨论】:

    【解决方案2】:

    我认为 GridView 已满并在您添加新项目时自动向下滚动(因此第一个项目“离开”在顶部)。是否有超过 15 件物品的空间?特别是因为您将 GridView 的高度固定为 360dp?您可以为 GridView 设置背景颜色以进行调试,以查看 GridView 获得了多少空间以及前 15 个项目占用了多少空间。

    【讨论】:

    • GridView 的高度设置为 360dp 并不重要,因为它具有滚动选项。它也可以存储 50 行,但是当我滚动它时,上面的行变得不可见。
    • 我想我没有正确理解你。如果它滚动,部分变得不可见。这正是我所期望的。
    猜你喜欢
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 2015-09-18
    • 1970-01-01
    相关资源
    最近更新 更多