gpt4 book ai didi

android - 如何处理配置更改时的** GridView **?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:48:24 26 4
gpt4 key购买 nike

我在应用程序的 Activity 中使用 GridView 。每个 GridView 行包含三个复选框,可以根据用户想要从数据库中查询的内容设置为选中/未选中。该 Activity 还包括一个 editText,它会在每次 Activity 开始时导致 onScreenKeyboard 出现,我在这里遇到了问题。 OnScreenKeyboard,当出现干扰 gridview 时,它的一些 checkboxes 就会消失。我的想法是每次配置更改时刷新 GridView 。我尝试通过 onRetainNonConfigurationInstance() 返回一个对象来处理这个问题。对象包含一个数组列表来填充我的 gridview 行,但是当我使用 getLastNonConfigurationInstance() 检索返回的对象时,onCreate() 显示 null。任何人都可以建议我如何处理这个问题,或者是否有任何其他可用的方法可以使我的 gridView 在配置更改时表现正常。以下是代码,我想说清楚我已经在 list 文件中添加了 keyboardHidden 配置更改但是当键盘出现时它有时不会触发 onConfigurationChanged()

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.failureselection);
findLocalWidgets(); //Initializes Layout Objects

if(configchanged){
//Re populate grid view
customDataGridRows = (ArrayList<CustomGridViewRow>) getLastNonConfigurationInstance();
dgvwFailures.setAdapter(new CustomGridViewAdapter(this,
customDataGridRows));

configchanged = false;
}else{
fillFailuresList(customDataGridRows);
dgvwFailures.setAdapter(new CustomGridViewAdapter(this,
customDataGridRows));
}

}


@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.failureselection);

configchanged = true;
}


@Override
public Object onRetainNonConfigurationInstance() {

return customDataGridRows;
}

private boolean fillFailuresList(
ArrayList<CustomGridViewRow> customDataGridRows) {
boolean isFilled = false;
try {
// Adding Failures data
customDataGridRows.add(new CustomGridViewRow(false, "Hood", false,
"Assembly Defect", false, "Masking Poor"));
customDataGridRows.add(new CustomGridViewRow(false, "Floor", false,
"Forget Work", false, "Assembly Defect"));
customDataGridRows.add(new CustomGridViewRow(false, "Grill", false,
"Incorrect Assembly", false, "Bad Company"));
customDataGridRows.add(new CustomGridViewRow(false,
"R Right Frame", false, "Interference", false,
"Fix Large Effort"));
customDataGridRows.add(new CustomGridViewRow(false, "R Left Frame",
false, "Leakage", false, "High Incidence"));
customDataGridRows.add(new CustomGridViewRow(false, "R Frame",
false, "Dirt", false, "Recurrence"));
customDataGridRows.add(new CustomGridViewRow(false,
"Outside R Frame", false, "Decal", false, "Checking"));
customDataGridRows.add(new CustomGridViewRow(false,
"F Right Frame", false, "Other", false, "Foreign Body"));
customDataGridRows.add(new CustomGridViewRow(false, "F Left Frame",
false, "", false, "Not Caulking"));
customDataGridRows.add(new CustomGridViewRow(false, "F Frame",
false, "", false, "Painting Defect"));
customDataGridRows.add(new CustomGridViewRow(false,
"Outsie F Frame", false, "", false, "Other"));
customDataGridRows.add(new CustomGridViewRow(false, "", false, "",
false, ""));

// Populating Failures grid view
// dgvwFailures.setAdapter(new CustomGridViewAdapter(this,
// customDataGridRows));

isFilled = true;

} catch (Exception e) {
e.getMessage();
}
return isFilled;

}

最佳答案

几个选项:

  1. How to prevent Custom Views from losing state across screen orientation changes为你的 CustomGridViewRow .
  2. 在您的适配器中创建一个方法并将值放入一个包中,并在创建时使用 savedInstanceState 恢复它

提示:删除任何 <requestFocus />从你的 EditText 布局。尽管 Android 会将焦点设置到第一个可聚焦 View 。

另一个黑客:How to remove focus without setting focus to another control?

关于android - 如何处理配置更改时的** GridView **?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8558637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com