Android(安卓)GridView之实现单元格合并,以及第一个item点击无响应或显示不正常的解决方案
16lz
2021-01-25
个人觉得 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); } } }}
了 BaseAdapter ,并且在重写 getView 方法时动态设置了 GridView 的宽度和高度。
解决方案:
首先检查 getView 方法中是否有动态设置 LayoutParams 的操作,如果有,那么 注意不能 new 一个新的LayoutParams ,而是
应该调用 getLayoutParams 获取已有的 LayoutParams 。如果 getLayoutParams 返回为空或者返回的 LayoutParams 不是
AbsListView.LayoutParams ,那么在 getView 之前使用 setLayoutParams 方法设置一下。
如果还有问题,那么检查是否采用了 ViewHolder 的写法,如果是则改成每次 getView 时都重新创建新的 View。
博主也遇到过这种问题,但是在使用了上面的方法排查后解决了问题,希望也可以解决你遇到的问题。
更多相关文章
- android样式学习(一) 使用selector改变来动态改变背景颜色
- Retrofit源码解析(一)
- 去除svn中方法
- Android(安卓)学习笔记 databinding简单使用:使用databinding在li
- android学习日记之fragment
- android 反射应用
- android关于输入法弹出时的UI变动
- android native调用
- 【PullToRefresh 系列一基本使用方法】 Android上拉加载下拉刷新