我正在创建一个Sudoku应用程序,但遇到问题 - 我试图只隐藏网格上的某些元素,我可以使用getView(),但是任何时候我都使用EditText来更改值和向下移动键盘,再次调用getView()并擦除输入,所以我试图找到解决方法。我相信这样做的方法是简单地遍历元素并隐藏onCreate()中的某些元素,尽管我在获取视图的任何子节点时遇到了问题。我可以使用getChildAt(i),虽然这只返回数据,而不是视图本身,所以我无法更改可见性。我也尝试过使用之前的Stackoverflow答案中的getChildAt()解决方案,但似乎无效。
继承我的代码:
MainActivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
public class MainActivity extends AppCompatActivity {
public GridView gridView;
public String[] items = new String[]{
"5","3","4","6","7","8","9","1","2",
"6","7","2","1","9","5","3","4","8",
"1","9","8","3","4","2","5", "6","7",
"8","5","9","7","6","1","4","2","3",
"4","2","6","8","5","3","7","9","1",
"7","1","3","9","2", "4","8","5","6",
"9","6","1","5","3","7","2","8","4",
"2","8","7","4","1","9","6","3","5",
"3","4","5", "2","8","6","1","7","9"
};
public Boolean[] masker = new Boolean[]{
TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE,
FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE,
TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE,
TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE,
TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE,
FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE,
FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) this.findViewById(R.id.myGridView);
CustomGridAdapter gridAdapter = new CustomGridAdapter(MainActivity.this, items, masker);
gridView.setAdapter(gridAdapter);
final int size = gridAdapter.getCount();
for(int i = 0; i < size; i++) {
ViewGroup gridChild = (ViewGroup) gridView.getChildAt(i);
int childSize = gridChild.getChildCount();
for(int k = 0; k < childSize; k++) {
gridChild.getChildAt(k);
}
}
}
}
CustomGridAdapter:
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import static java.lang.Boolean.FALSE;
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private String[] items;
private Boolean[] masker;
private String input;
private EditText t;
LayoutInflater inflater;
public CustomGridAdapter(Context context, String[] items, Boolean[] masker ){
this.context = context;
this.items = items;
this.masker = masker;
inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = inflater.inflate(R.layout.cell, null);
}
t = (EditText) convertView.findViewById(R.id.editCell);
if(masker[position]){
t.setText(items[position]);
t.setEnabled(FALSE);
}else{
t.setText("");
}
if( (position % 9 != 0) && (position % 3 == 0) ){
t.setBackgroundResource(R.drawable.columnborder);
}
if( (position >= 18 && position <= 26) || (position >= 45 && position <= 53) ){
t.setBackgroundResource(R.drawable.rowborder);
}
if( (position == 21 || position == 24 || position == 48 || position == 51 ) ){
t.setBackgroundResource(R.drawable.cornerborder);
}
return convertView;
}
@Override
public int getCount() { return items.length; }
@Override
public Object getItem(int position) { return items[position]; }
@Override
public long getItemId(int position) { return position; }
}
基本上,我只想隐藏数独的网格的某些元素,但我认为我不能在getView()中执行它,因为它会因t.setText(“”);而继续重置值。
有什么建议吗?
答案 0 :(得分:1)
你的方法完全错了。除了在getView期间,你不应该改变GridView中的视图。您通过其适配器及其保存的数据操作GridView。例如,假设您有一个由字符串列表支持的适配器,并且您希望隐藏其他所有字符串。你有两个选择 - 改变列表只保留其他所有字符串,或者更改它保存的数据以保存字符串和可见性,并在getView中同时遵守这两个字符串。
GridView(或ListView或RecyclerView)的重点是它的数据支持视图,它将循环以匹配数据。如果你开始尝试自己操纵视图,那就会严重破坏。不要这样做 - 操纵数据,然后告诉视图自我更新。