单击下一个 ExpandableListView 时,ExpandableListView 中的 EditText 内容消失 header

EditText content inside ExpandableListView disappear when clicking next ExpandableListView header

我在 expandablelistview 项 child 中有一个 edittext。然后,我在这个 edittext 里面输入了一个文本,但是每当我点击其他 expandablelistview header,我输入的文本就消失了。

我已经在我的 Manifest.xml 中使用了 android:windowSoftInputMode="adjustPan" 和许多其他可能的修复方法,但没有奏效。

这是我的适配器:

public class ExpendableAdapter extends BaseExpandableListAdapter {
    private Context _context;
    private List<String> _listDataHeader;
    private HashMap<String, List<ExpandItemModel>> _listDataChild;

public ExpendableAdapter(Context context, List<String> listDataHeader,
                             HashMap<String, List<ExpandItemModel>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@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) {

    final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    final EditText etTotal = (EditText)convertView
            .findViewById(R.id.et_total);

    etTotal.setText(childText.getTotal);

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }



    TextView tvHeader = (TextView) convertView
            .findViewById(R.id.tv_header);
    tvHeader.setTypeface(null, Typeface.BOLD);
    tvHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return true;
}


@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

这是布局:

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rl_expen_but"
        android:layout_below="@+id/rl_ats_draft2"
        android:layout_above="@+id/selanjutnya"
        >



        <ExpandableListView
            android:background="@color/cardview_light_background"
            android:id="@+id/lvExp"
            android:descendantFocusability="beforeDescendants"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:animateLayoutChanges="true"/>

        <View
            android:layout_below="@+id/lvExp"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#bebebe"/>

    </RelativeLayout>

我上面的代码有什么问题吗?谢谢。

编辑:

这是我按照 chandil03 的建议编辑代码后的样子。当我展开 headers 时,expandablelistview 不再刷新。但奇怪的是,当我在 groupposition 0 和 child 位置 0 的 edittext 中键入值时,然后在不同 goupposition 和 childposition 的其他一些 edittext 中出现与我在之前的 edittext 中键入的完全相同的值。或者更奇怪的是,以前的编辑文本中的值有时会消失。

static class ViewHolder {
    protected View et;

    public void addView(View et){
        this.et = et;
    }
}

       @Override
       public View getChildView(final int groupPosition, final int childPosition,
                     boolean isLastChild, View convertView, ViewGroup parent) {

       final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);

       ViewHolder viewHolder;

       if (convertView == null) {
           LayoutInflater infalInflater = (LayoutInflater) this._context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = infalInflater.inflate(R.layout.list_item, null);
           viewHolder = new ViewHolder();
           viewHolder.addView(convertView
                .findViewById(R.id.et_total));
           convertView.setTag(viewHolder);
       }else{
           viewHolder = (ViewHolder)convertView.getTag();
       }

       final EditText etTotal = (EditText)convertView
        .findViewById(R.id.et_total);

       etTotal.setText(childText.getTotal);

       return convertView;
  }

每当您展开另一个组时,列表都会刷新,并且您会在其中获得另一个没有文本的 EditText 实例。您输入文本的 EditText 将成为另一个 groupView.

的子项

所以我建议您创建一个 ViewHolder class 并将您的视图保留在其中并将其设置为标签,并在 getView() 中检查您拥有的 convertView 是否为 null已经完成了。只需从 convertView 添加 else 部分和 getTag 并相应地设置值。

例如:

@Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        View v = convertView;
        if (v == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.my_layout, parent, false);
            ViewHolder holder = new ViewHolder(); // View holder to save views.
            holder.addView(v.findViewById(R.id.myView));
            v.setTag(holder);
        }
        else{
        ViewHolder holder = (ViewHolder) v.getTag();  // get tag and set values
        // Do whatever you need to with the group view
        }
        return v;
    }

编辑 1

我已经修改了你的代码。也查看评论。

@Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.editText.setText(childText.getTotal); // Here whatever you will type in edittext will be overwritten by the value of 'childText.getTotal'. So after you are done writing in edit text make sore you change that in "_listDataChild" list. 

        return convertView;
    }

    /**This is a class that holds view, Here i m not talking about support.v7.widget.RecyclerView.ViewHolder class. Though concept is more or less same.*/
    class ViewHolder{
        EditText editText;

        public ViewHolder(View v)
        {
            editText = (EditText) v.findViewById(R.id.et_total);
        }
    }

编辑 2

getChildView() 方法中添加以下代码。

holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View v, boolean hasFocus)
        {
            if(!hasFocus)
                childText.getTotal = holder.editText.getText().toString(); 
// In java variable contains reference of Object so when you change childText object, it will be reflected in same HashMap you are getting data from.

        }
    });

每次向下滚动或单击 ExpandableListView 中的另一个 header 时,EditText 都会被回收。

最好的办法是保存用户在模型中输入的文本 例如:

etTotal.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                childText.setTotal(etTotal.getText().toString().trim());
            }
        }
    });

首先创建一个虚拟模型 class 来存储来自 edittext 的数据,如下所示:

public class DummyModel {
    String edt_field;
    //
    //And any other data you might want to store
    //
    public DummyModel(String field)
    {
        this.edt_field=field;
    }
}

然后创建一个静态数组列表来存储您的数据:

public class DummyStatic  {
public static ArrayList<DummyModel> arl_static=new ArrayList();
}

然后在您的适配器中 class 像这样更改您的 getChildView() 方法:

    @Override
public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    final EditText etTotal = (EditText)convertView
            .findViewById(R.id.et_total);
    etTotal.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) {
             DummyStatic.arl_static.add(childPosition,new DummyModel(charSequence.toString()));
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    etTotal.setText(childText.getTotal);

    return convertView;
}

这段代码要做的是,当您在编辑文本中键入字符时,它们将存储在数组列表中的 DummyModel 对象中。

您可以通过以下方式检索此数据:

String value = DummyStatic.arl_static.get(position).edt_field;

我终于可以使用这个库来处理这个问题了:

http://bignerdranch.github.io/expandable-recycler-view/

然后通过使用 AddTextChangedListener,我插入了我键入的值。之后,检查 ViewHolderChild 中插入的值。例如:

public void bind(final MyModel model) {
            tvName.setText(model.getName());
            tvPrice.setText(model.getPrice());
            tvNote.setText(model.getNote());

            for(int i=0;i<listProduct.size();i++){
                if(listProduct.get(i).getIdProduct().equals(model.getIdProduct())){
                    et.setText(listProduct.get(i).getQuantity());
                    break;
                }
            }

            et.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if(m!=null){
                        listProduct.remove(m);
                    }

                    if (!s.toString().equals("")) {

                        m = new MyModel();
                        m.setName(model.getName());
                        m.setNote(model.getNote());
                        m.setIdProduct(model.getIdProduct());
                        m.setPrice(model.getPrice());
                        m.setQuantity(s.toString());
                        m.setSubtotal(Long.parseLong(s.toString()) * Long.parseLong(model.getPrice()) + "");
                        m.setStock(model.getStock());
                        listProduct.add(m);
                    }
                }
            });
        }