android GridView内容在滚动过程中消失

w80xi6nr  于 2022-12-31  发布在  Android
关注(0)|答案(3)|浏览(220)

我有一个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>
ikfrs5lh

ikfrs5lh1#

问题似乎是你没有正确地循环视图。下面的例子是基于你代码的简化(我去掉了一些成员和一些条件块)。只考虑了两种单元格,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处理不同类型的视图时,它们会有所帮助。

flvlnr44

flvlnr442#

你需要给予要显示的文本的合理长度,比如说为Textview添加此属性android:maxLength=“5”。继续调整长度,直到它不再消失

mbskvtky

mbskvtky3#

我认为GridView是满的,当你添加新项时,它会自动向下滚动(所以第一个项会“留在”顶部)。有空间容纳15个以上的项吗?特别是在你将GridView的高度固定为360dp之后?你可以为GridView设置一个背景色,以便调试,看看GridView获得了多少空间,以及前15个项占用了多少空间。

相关问题