GridView某个单元格的选中状态受到键盘影响
发布于 2022-06-06 11:59:07阅读 1190
GridView如何设置某个单元格为选中状态?
首先,该组件自带的gridview.setSelector(R.color.orange);
,可以设置选中;但如果页面上同时有输入控件,比如EditText
,这时GridView的选中状态就会受到键盘影响,比如当前GridView的某个单元格为选中状态,拉起/收回 键盘,这个选中状态会自动取消,下面介绍一种方法:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
GridView gridView=(GridView) arg1.getParent();
for (int i=0; i<menuList.size(); ++i){
RelativeLayout relativeLayout=(RelativeLayout) gridView.getChildAt(i);
if (i==arg2){
relativeLayout.setBackgroundColor(Color.GREEN);
}else {
relativeLayout.setBackgroundColor(Color.WHITE);
}
}
// gridview.setSelector(R.color.orange);
}
});