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

Jes*_*run 3 android expandablelistadapter android-listview

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

  1. 在子视图中键入文本几乎是不可能的。键入每个字符后,看起来EditText失去了焦点。我已经附上了一个截图来说明这种行为。
  2. 当组展开/折叠时,几次后,TextViews中的文本似乎消失了,每个视图中仅第一个单词的第一个字母出现。似乎它们的布局参数以某种方式被错误地计算。

这是屏幕截图: EditText-令人讨厌的打字 TextView-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;
    }

}
Run Code Online (Sandbox Code Playgroud)

Kar*_*ran 5

我有一个类似的问题。

在您的清单文件中添加它,看看它是否有效。它对我有用。

android:windowSoftInputMode="adjustPan" 
Run Code Online (Sandbox Code Playgroud)

在您的活动代码中,这样就可以

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:windowSoftInputMode="adjustPan">
Run Code Online (Sandbox Code Playgroud)