个人觉得 GridView 中实现单元格合并的需求并不常见,但是这里还是介绍一种方法,先看一下效果图:


接下来是代码实现:

public class MainActivity extends Activity {    private static GridView mGridView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mGridView = new GridView(getApplicationContext());        MAdapter adapter = new MAdapter(mGridView.getContext(), new String[]{"item1", "item2", "item3",                "item4", "item5", "item6", "item7", "item8", "item9", "item10", "item11", "item12",                "item13", "item14", "item15", "item16", "item17", "item18", "item19", "item20"});        mGridView.setAdapter(adapter);        mGridView.setNumColumns(2);        setContentView(mGridView);    }    class MAdapter extends BaseAdapter {        private String[] items;        private Context context;        public MAdapter(Context context, String[] items) {            this.items = items;            this.context = context;        }        @Override        public int getCount() {            return items.length;        }        @Override        public Object getItem(int position) {            return position;        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public int getItemViewType(int position) {            return super.getItemViewType(position);        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            TextView textView;            if (position % 4 == 0) {                textView = new MyText(context);                AbsListView.LayoutParams params = new AbsListView.LayoutParams(                        AbsListView.LayoutParams.MATCH_PARENT, 100);                textView.setLayoutParams(params);                textView.setTextColor(Color.BLACK);                textView.setTextSize(20);                textView.setText(items[position]);                textView.setBackgroundColor(Color.GRAY);                textView.setGravity(Gravity.CENTER);            } else {                textView = new TextView(context);                AbsListView.LayoutParams params = new AbsListView.LayoutParams(                        AbsListView.LayoutParams.MATCH_PARENT, 200);                textView.setLayoutParams(params);                textView.setTextColor(Color.BLACK);                textView.setTextSize(20);                textView.setText(items[position]);                textView.setVisibility(View.VISIBLE);                if (position % 4 == 1) {                    AbsListView.LayoutParams params2 = new AbsListView.LayoutParams(                            AbsListView.LayoutParams.MATCH_PARENT, 100);                    textView.setLayoutParams(params2);                    textView.setVisibility(View.INVISIBLE);                }                textView.setGravity(Gravity.CENTER);            }            return textView;        }        class MyText extends TextView {            public MyText(Context context) {                super(context);            }            @Override            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {                super.onMeasure(widthMeasureSpec, heightMeasureSpec);                int targetWidth = mGridView.getMeasuredWidth()                        - mGridView.getPaddingLeft()                        - mGridView.getPaddingRight();                widthMeasureSpec = MeasureSpec.makeMeasureSpec(targetWidth,                        MeasureSpec.getMode(widthMeasureSpec));                super.onMeasure(widthMeasureSpec, heightMeasureSpec);            }        }    }}


接下来再说一个不太常见的问题,在使用 GridView 时第一个 item 点击无响应或显示不正常,这种情况一般都是因为使用

了 BaseAdapter ,并且在重写 getView 方法时动态设置了 GridView 的宽度和高度。

解决方案:

        首先检查 getView 方法中是否有动态设置 LayoutParams 的操作,如果有,那么 注意不能 new  一个新的LayoutParams ,而是

应该调用 getLayoutParams 获取已有的 LayoutParams 。如果 getLayoutParams 返回为空或者返回的 LayoutParams 不是 

AbsListView.LayoutParams ,那么在 getView 之前使用 setLayoutParams 方法设置一下。

        如果还有问题,那么检查是否采用了 ViewHolder 的写法,如果是则改成每次 getView 时都重新创建新的 View。

博主也遇到过这种问题,但是在使用了上面的方法排查后解决了问题,希望也可以解决你遇到的问题。

更多相关文章

  1. android样式学习(一) 使用selector改变来动态改变背景颜色
  2. Retrofit源码解析(一)
  3. 去除svn中方法
  4. Android(安卓)学习笔记 databinding简单使用:使用databinding在li
  5. android学习日记之fragment
  6. android 反射应用
  7. android关于输入法弹出时的UI变动
  8. android native调用
  9. 【PullToRefresh 系列一基本使用方法】 Android上拉加载下拉刷新

随机推荐

  1. [zz] Android Service 示例
  2. Mainfest
  3. Android(安卓)分享功能的实现
  4. android 五种Log的意思
  5. Android电话拨号器
  6. arcgis for android 离线切片加载与geoda
  7. Android:如何实现例如iOS的listview 的弹
  8. [Android] 启动无线与网络设置的Action__
  9. 2011.12.06——— android 带边框的Image
  10. Android 默认把触摸屏show touches打开