ExpandableListView中的EditText - 打开软键盘时保持焦点

时间:2012-06-28 01:45:22

标签: android android-listview expandablelistadapter

我有一个包含多个组的ExpandableList,每个组包含一个不同类型的子(不同的布局)。我有几个具有类似结构但有不同子视图的活动,所以我决定为它编写一个自定义适配器。适配器在大多数情况下运行良好,除了几个问题:

  1. 在子视图中键入文本几乎是不可能的。看起来EditText在键入每个字符后失去焦点。我附上了截图来说明行为。
  2. 当组展开/折叠时,几次后,TextViews中的文本似乎消失了,每个视图中只显示第一个单词的第一个字母。它看起来好像他们的布局参数在某种程度上被误算了。
  3. 以下是截图: EditText - Jittery typing TextView - Layout of text in TextView

    这是我的适配器的代码:

    public class GenericExpandableListAdapter extends BaseExpandableListAdapter {
    
        private Context context;
        private String[] groups;
        private View[] children;
    
        public static interface ExpandableListItemClickListener {
            public void onClick(View view, int groupId, int itemId);
        }
    
        public GenericExpandableListAdapter(Context context, String[] groups, int[] children) {
            if(groups.length != children.length) {
                throw new IllegalArgumentException("Items must have the same length as groups.");
            }
            this.context = context;
            this.groups = groups;
    
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.children = new View[children.length];
            for(int i = 0; i < children.length; i++) {
                this.children[i] = inflater.inflate(children[i], null);
            }
        }
    
        public GenericExpandableListAdapter(Context context, String[] groups, View[] children) {
            if(groups.length != children.length) {
                throw new IllegalArgumentException("Items must have the same length as groups.");
            }
            this.context = context;
            this.groups = groups;
            this.children = children;
        }
    
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition];
        }
    
        @Override
        public int getChildTypeCount() {
            return children.length + 1;
        }
    
        @Override
        public int getChildType(int groupPosition, int childPosition) {
            return groupPosition;
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
    
        @Override
        public View getChildView(final int groupPosition, final int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            if(convertView != null) {
                return convertView;
            }
            return children[groupPosition];
        }
    
        @Override
        public int getChildrenCount(int groupPosition) {
            return 1;
        }
    
        @Override
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }
    
        @Override
        public int getGroupTypeCount() {
            return 1;
        }
    
        @Override
        public int getGroupCount() {
            return groups.length;
        }
    
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
    
        @Override
        public int getGroupType(int groupPosition) {
            return 0;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
    
            if(convertView != null) {
                ((TextView) convertView).setText(getGroup(groupPosition).toString());
                return convertView;
            }
            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);
    
            final TextView textView = new TextView(context);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(50, 0, 0, 0);
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }
    
        @Override
        public boolean hasStableIds() {
            return true;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    
    }
    

1 个答案:

答案 0 :(得分:4)

我遇到了类似的问题。

在您的清单文件中添加它,看看它是否有效。它确实适合我。

android:windowSoftInputMode="adjustPan" 

在您的活动代码中,所以它会是这样的

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:windowSoftInputMode="adjustPan">