具有不同大小列的 Android GridView

标签 android gridview

这是我的第一个问题。 我正在为 Android 开发一个应用程序,我正在使用 GridView 作为 DataGrid。 我有一些具有 7 或 8 列的网格,我需要根据其内容使 gridview 的列具有不同的宽度。 我检查了这里的所有帖子,但找不到解决方案。 我现在正在做的是设置每个 View 的 LayoutParams,用我为每个 View 选择的百分比计算固定宽度, View 获得正确的宽度,但第二个 View 的文本显示在第一个 View 上,依此类推。 我所有的 View 都是 TextView 或 EditText。

我会感激你能给我的任何帮助。 非常感谢您!

马克西

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    final int row = GetRow(position);
    View field = null;
    String text = "";
//  convertView = grid.getChildAt(position);
    /*f (grid.getChildAt(position) != null) {
        if (null == convertView) {
            return grid.getChildAt(position);
        } else {
            convertView = null;
        }
    }*/
    int width = 0;
    Point size = new Point();
    ((Activity) context).getWindowManager().getDefaultDisplay().getSize(size);
    width = weights[GetCol(position)] * (size.x - 50) / 100; 

    if (row == 0){
        text = localfieldstitles[position];
        if (convertView != null){
            field = (TextView) convertView;
        } else {
            field = new TextView(context);
        }

        ((TextView)field).setText(text);
        field.setBackgroundResource(R.drawable.buttonblue);
        ((TextView)field).setTextAppearance(context, android.R.style.TextAppearance_DeviceDefault_Medium);
        LayoutParams params = new GridView.LayoutParams(width, LayoutParams.MATCH_PARENT);
        field.setLayoutParams(params);
    } else {
    // Get our text for position
        final int col = GetCol(position);
        try {
            gridData.absolute(row);
            if (gridData.getString(remotefields[col]) != null) {
                text = gridData.getString(remotefields[col]);
            } else {
                text = "";
            }

            if (convertView != null) {
                field = convertView;
                if (convertView instanceof EditText) {
                    ((EditText) field).setText(text);
                } else {
                    ((TextView) field).setText(text);
                }
            } else {
                if (editable[col].equalsIgnoreCase("T")) {
                    field = new EditText(context);
                    ((EditText) field).setText(text);
                    ((EditText) field).setInputType(fieldtypes[col]);
                } else {
                    field = new TextView(context);
                    ((TextView) field).setText(text);
                }
            }
            if (editable[col].equalsIgnoreCase("T")) {
                ((EditText)field).setSingleLine();
                ((EditText)field).setOnEditorActionListener(
                        new EditText.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_DONE || 
                                actionId == EditorInfo.IME_ACTION_NEXT) {
                                try {
                                    if (!isFindColumn(col)) { 
                                        gridData.absolute(row);
                                        switch (gridData.getMetaData().getColumnType(col)) {
                                            case Types.FLOAT: 
                                                case Types.REAL: 
                                                case Types.DECIMAL: gridData.updateFloat(localfields[col], Float.parseFloat(v.getText().toString())); break;
                                            case Types.DOUBLE: gridData.updateDouble(localfields[col], Double.parseDouble(v.getText().toString())); break;
                                            case Types.INTEGER: gridData.updateInt(localfields[col], Integer.parseInt(v.getText().toString())); break;
                                            case Types.VARCHAR: 
                                                case Types.NVARCHAR: 
                                                case Types.CHAR: gridData.updateString(localfields[col], v.getText().toString()); break;                                            
                                        }
                                        datachange.fieldChange(localfields[col], v.getText().toString());
                                    } else {
                                        findColumn.FINDTEXT = v.getText().toString();
                                        executeSearch();
                                    }
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                        }
                        return true; // pass on to other listeners. 
                    }


                    });
                    ((EditText)field).setOnFocusChangeListener(new EditText.OnFocusChangeListener() {          

                        public void onFocusChange(View v, boolean hasFocus) {
                            if(!hasFocus){
                                try {
                                    if (!isFindColumn(col)) { 
                                        gridData.absolute(row);
                                        switch (gridData.getMetaData().getColumnType(col)) {
                                            case Types.FLOAT: 
                                                case Types.REAL: 
                                                case Types.DECIMAL: gridData.updateFloat(localfields[col], Float.parseFloat(((TextView)v).getText().toString())); break;
                                            case Types.DOUBLE: gridData.updateDouble(localfields[col], Double.parseDouble(((TextView)v).getText().toString())); break;
                                            case Types.INTEGER: gridData.updateInt(localfields[col], Integer.parseInt(((TextView)v).getText().toString())); break;
                                            case Types.VARCHAR: 
                                                case Types.NVARCHAR: 
                                                case Types.CHAR: gridData.updateString(localfields[col], ((TextView)v).getText().toString()); break;                                            
                                        }
                                        datachange.fieldChange(localfields[col], ((TextView)v).getText().toString());
                                    } /*else {
                                        findColumn.FINDTEXT = ((TextView)v).getText().toString();
                                        executeSearch();
                                    }   */                              
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }   
                            }
                               //do job here owhen Edittext lose focus 
                        }
                    });
            }
            LayoutParams params;
            if (text.equalsIgnoreCase("")) {
                params = new GridView.LayoutParams(width, 40);
            } else {
                params = new GridView.LayoutParams(width, LayoutParams.WRAP_CONTENT);
            }
            field.setLayoutParams(params);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //Finally return the view }
    return field; 
}

最佳答案

首先告诉我你是如何设置你的gridview的?你有没有使用 setAdapter 方法? 如果您告诉我,我可以帮助您,我认为您必须为此目的创建 CustomAdapter。所以第一步回答我的问题。

关于具有不同大小列的 Android GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19038787/

相关文章:

Android Espresso 不等待 fragment 加载

c# - 在 RowCommand 中获取 GridView Cell 的值

android - 如何指定android :listSelector ="@null" programmatically?

c# - 从数据表中删除具有行值的列

c# - 防止 ControlParameter AutoPostBack 触发 ObjectDataSource Select 方法

c# - Asp.net C# gridview单列中的多个值

android - Admob Interstitial Ads 不会在 Android 上全屏显示

android - ActionBar 在按下时更改菜单项按钮的颜色

java - 如何在listview android中仅获取更新的复选框状态

android - 如何防止用户在实现 swipeRefreshLayout 时滚动?