まず、IMEで変換候補がポップアップされると、ListView が再描画されてしまいます。つまり、BaseAdapter だと getView() が、ExpandableListView だと getGroupView() や getChildView() がそのつど呼ばれます。そうすると、EditText にあてていた Focus が外れてしまうため、変換候補がでた瞬間に変換候補のポップアップが消えるというやっかいな現象が起こってしまうわけです。
対処方法(というか、とりあえずの解決策)は、requestFocus() することです。子要素が EditText だったり、 EditText が一つだけ含まれる LinearLayout だったりする場合は、convertView.requestFocus() でいいのですが、2個以上の EditText が含まれる場合は、どの EditText が requestFocus するかを保持しておかないとうまくいきません。
さらにさらに、EditText に入力された内容は、adapter で管理して、group を展開した場合の初期値に入れないと、1つの子要素で入力したものが、すべての子要素に伝搬します。これを制御するのはとても面倒です。なぜなら、最初にいったように毎回 ListView が再描画されてしまうので、へたに getView で adapter のデータを editText に setText すると、入力してもしても入力されない、という状況になるからです。
というわけで、サンプルです。
# 正直使えませんw

- package yanzm.example.expandablelisttest3;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.ExpandableListActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.EditText;
- import android.widget.TextView;
- public class ExpandableListTest3 extends ExpandableListActivity {
- MyExpandableListAdapter mAdapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setList();
- }
- public void setList() {
- final int LIST_SIZE = 10;
- List<String> groupList = new ArrayList<String>();
- List<List<String>> childList1 = new ArrayList<List<String>>();
- List<List<String>> childList2 = new ArrayList<List<String>>();
- List<String> childData = new ArrayList<String>();
- childData.add("");
- for (int i = 0; i < LIST_SIZE; i++) {
- groupList.add("Title: " + i);
- childList1.add(childData);
- childList2.add(childData);
- }
- mAdapter = new MyExpandableListAdapter(groupList, childList1, childList2);
- setListAdapter(mAdapter);
- }
- public class MyExpandableListAdapter extends BaseExpandableListAdapter {
- private List<String> groups;
- private List<List<String>> childs1;
- private List<List<String>> childs2;
- private boolean isFocusEditText1 = true;
- private LayoutInflater inflater;
- public MyExpandableListAdapter(List<String> groups, List<List<String>> childs1, List<List<String>> childs2) {
- this.groups = groups;
- this.childs1 = childs1;
- this.childs2 = childs2;
- this.inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- public int getRowId(int groupPosition) {
- return groupPosition;
- }
- public Object getChild(int groupPosition, int childPosition) {
- return childs1.get(groupPosition).get(childPosition);
- }
- public Object getChild2(int groupPosition, int childPosition) {
- return childs2.get(groupPosition).get(childPosition);
- }
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
- public int getChildrenCount(int groupPosition) {
- return childs1.get(groupPosition).size();
- }
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- Log.d("getChildView", "groupPosition = " + groupPosition + ", childPosition = " + childPosition + ", isLastChild = " + isLastChild + ", convertView = " + convertView + ", ViewGroup = " + parent);
- if(convertView == null) {
- convertView = inflater.inflate(R.layout.child_item, parent, false);
- }
- EditText editText1 = (EditText)convertView.findViewById(R.id.edittext1);
- editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- Log.d("onFocusChanged1", "View : " + v + ", hasFocus : " + hasFocus);
- if(hasFocus)
- isFocusEditText1 = true;
- }
- });
- EditText editText2 = (EditText)convertView.findViewById(R.id.edittext2);
- editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- Log.d("onFocusChanged2", "View : " + v + ", hasFocus : " + hasFocus);
- if(hasFocus)
- isFocusEditText1 = false;
- }
- });
- if(isFocusEditText1)
- editText1.requestFocus();
- else
- editText2.requestFocus();
- return convertView;
- }
- public Object getGroup(int groupPosition) {
- return groups.get(groupPosition);
- }
- public int getGroupCount() {
- return groups.size();
- }
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
- public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
- Log.d("getGroupView", "groupPosition = " + groupPosition + ", isExpanded = " + isExpanded + ", convertView = " + convertView + ", ViewGroup = " + parent);
- if(convertView == null) {
- convertView = inflater.inflate(R.layout.group_item, parent, false);
- }
- if(convertView instanceof TextView) {
- ((TextView)convertView).setText(getGroup(groupPosition).toString());
- }
- return convertView;
- }
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- return true;
- }
- public boolean hasStableIds() {
- return true;
- }
- }
- }
group_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/textview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:paddingLeft="48dip"
- android:minHeight="?android:attr/listPreferredItemHeight"
- />
child_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:paddingLeft="64dip"
- android:paddingRight="12dip"
- android:minHeight="?android:attr/listPreferredItemHeight"
- >
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:layout_width="30dip"
- android:layout_height="wrap_content"
- android:text="住所"
- android:layout_marginLeft="5dip"
- />
- <EditText
- android:id="@+id/edittext1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:inputType="text"
- />
- </LinearLayout>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:layout_width="30dip"
- android:layout_height="wrap_content"
- android:text="氏名"
- android:layout_marginLeft="5dip"
- />
- <EditText
- android:id="@+id/edittext2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:inputType="text"
- />
- </LinearLayout>
- <RadioGroup
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <RadioButton
- android:id="@+id/radio1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="男性"
- />
- <RadioButton
- android:id="@+id/radio1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="女性"
- />
- </RadioGroup>
- </LinearLayout>
個人的には、可変長でない場合は素直に LinearLayout でレイアウトして、Visibility で管理したほうがいいのでは、と思いました。 → 次のエントリ??
# それにしても、 RadioGroup の挙動も微妙だった。あんまり RadioGroup 使わないから、もしかして使い方まちがってるのかもw
0 件のコメント:
コメントを投稿