GridViewをGridView内に作成すると思いましたか? 必要な列数と行数を持つGridViewがあり、Grid内の各ビューは複数のEditTextで構成される別のGridViewになります。そのためには、すべてのGridView用に2つのアダプタが必要です。
コードで編集: これは試用版であることに注意し、必要に応じて調整します。
MainActivityレイアウト:
<GridView
android:id="@+id/grid_view_parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</GridView>
item_grid_parent:
<GridView
android:id="@+id/grid_view_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</GridView>
item_grid_child:
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:textColor="#FFFFFF" />
MainActivity:
GridParentAdapter:
public class GridParentAdapter extends BaseAdapter{
private Context context;
private GridView gridViewChild;
private List<String> items = new ArrayList<>();
public GridParentAdapter(Activity context, List<String> items){
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return 3;
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup
parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_grid_parent, parent
gridViewChild = (GridView) convertView.findViewById
(R.id.grid_view_child);
final GridChildAdapter gridChildAdapter = new
GridChildAdapter(context, items, this);
gridViewChild.setNumColumns(4);
gridViewChild.setAdapter(gridChildAdapter);
return convertView;
}
public void setItemChar(int position, String character){
items.set(position, character);
}
}
GridChildAdapter:
public class GridChildAdapter extends BaseAdapter {
private EditText editText;
private GridView gridViewParent;
private Context context;
private List<String> character;
private GridParentAdapter gridParentAdapter;
public GridChildAdapter(Context context, List<String> character
,GridParentAdapter gridParentAdapter){
this.context = context;
this.character = character;
this.gridParentAdapter = gridParentAdapter;
}
@Override
public int getCount() {
return character.size();
}
@Override
public Object getItem(int i) {
return character;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup
parent) {
LayoutInflater inflater =(LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_grid_child, parent,
false);
editText = (EditText) convertView.findViewById(R.id.edit_text);
editText.setText(character.get(position));
editText.setClickable(true);
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction()) {
InputMethodManager im = (InputMethodManager)
context.getSystemService
(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(editText, 0);
editText.requestFocus();
}
return false;
}
});
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i,int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int
i1, int i2) {
if (editText.getText().toString() != "null") {
gridParentAdapter.setItemChar
(position,charSequence.toString());
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
return convertView;
}
public String getEditTextValue(EditText editText){
String string = "";
if(editText != null){
string = editText.getText().toString();
}
return string;
}
}
2Dアレイを表示するためのTextViewを拡張してカスタムビューを作成? – Sourabh
私に例を教えてもらえますか? – Razon