android – EditText内容在ExpandableListView内部消失时单击下一个ExpandableListView头

前端之家收集整理的这篇文章主要介绍了android – EditText内容在ExpandableListView内部消失时单击下一个ExpandableListView头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个可扩展列表视图项目中的edittext子项.然后,我在这个edittext中输入了一个文本,但是当我点击其他可扩展列表视图标题时,我输入的文本消失了.

我已经在我的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,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建议我编辑.当展开头文件时,可扩展列表视图不再刷新.但是奇怪的是,当我在grouptext 0和child position 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,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类并保持你的视图,并将其设置为一个标签,并在getView()中检查convertView是否已经完成.只需从convertView添加else part和getTag,并相应地设置值.

例如:

@Override
    public View getGroupView(int groupPosition,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,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.

        }
    });
原文链接:https://www.f2er.com/android/311661.html

猜你在找的Android相关文章