单击下一个ExpandableListView标头时,ExpandableListView内的EditText内容消失

zam*_*mim 11 android expandablelistview android-edittext

我有一个edittext内部expandablelistview项目的孩子.然后,我在其中输入了一个文本edittext,但每当我点击其他expandablelistview标题时,我输入的文本就会消失.

我已经android:windowSoftInputMode="adjustPan"在我的Manifest.xml中使用了许多其他可能的修复程序,但是没有用.

这是我的适配器:

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;
}
}
Run Code Online (Sandbox Code Playgroud)

这是布局:

<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>
Run Code Online (Sandbox Code Playgroud)

我用上面的代码做错了吗?谢谢.

编辑:

按照chandil03的建议编辑后,我的代码就是这样的.展开标题时,expandablelistview不再刷新.但奇怪的是,当我在集合0和子位置0的edittext中键入值时,在不同的goupposition和childposition中的其他一些edittext中出现与我在前一个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;
  }
Run Code Online (Sandbox Code Playgroud)

zam*_*mim 1

我终于能够使用这个库来处理这个问题:

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);
                    }
                }
            });
        }
Run Code Online (Sandbox Code Playgroud)