2

我在动态添加静态 EditText 时遇到问题,总是在最后一个子位置。我的适配器使用 JSON 类型的对象。我第一次展开 ExpandableListView 组的子项时,可以为我的 EditText 找到正确的位置,但是当我向相反方向滚动子项时,我的 EditText 会不断改变位置。这是我的 Editext 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content" 
 android:layout_width="fill_parent" 
 android:orientation="vertical" 
 android:paddingLeft="10dip"
 android:paddingRight="10dip"
 android:background="@color/lgray">
<RelativeLayout android:id="@+id/feedcell_addcomment" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="5dip" android:paddingBottom="5dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="1dip" android:background="@color/mlgray" >


    </RelativeLayout>
</LinearLayout>

这是我的 childLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layoutchildren" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical" android:paddingLeft="10dip" android:paddingRight="10dip" android:background="@color/lgray">
    <RelativeLayout   android:paddingLeft="10dip" android:paddingRight="10dip" android:background="@color/white" android:layout_height="45dip" android:layout_width="fill_parent" android:layout_marginBottom="2dip">
        <ImageView android:id="@+id/feedcommentcell_profileimg" android:layout_width="32dip" android:layout_height="32dip" android:layout_centerVertical="true" android:scaleType="fitXY"/>
        <TextView android:id="@+id/feedcommentcell_username" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:textColor="@color/black" android:layout_toRightOf="@+id/feedcommentcell_profileimg" android:layout_marginLeft="10dip" android:layout_alignTop="@+id/feedcommentcell_profileimg"/>
        <TextView android:id="@+id/feedcommentcell_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="11sp" android:textColor="@color/black" android:layout_toRightOf="@+id/feedcommentcell_profileimg" android:layout_marginLeft="10dip" android:layout_below="@+id/feedcommentcell_username"/>
        <TextView android:id="@+id/feedcommentcell_scanhour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10sp" android:textColor="@color/dgray" android:layout_alignParentRight="true" android:layout_alignBottom="@+id/feedcommentcell_username"/>
        </RelativeLayout>
</LinearLayout>

这是我的适配器中的 getChildView

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

            int type=getItemViewType(groupPosition,childPosition);
            View v = convertView;
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (v == null)
            {

                switch(type)
                {

                    case TYPE_CHILD:

                        v = vi.inflate(R.layout.feedcommentcell, null);

                        break;

                    case TYPE_EDIT:

                        v = vi.inflate(R.layout.edittextcommentlist, null);
                        break;
                }

            }

            try
            { 
                if(type == TYPE_CHILD)
                {
                    JSONObject o = getChild(groupPosition,childPosition);
                        //JSONObject o = itemsChildren.get(childPosition);
                    if(o!=null)
                    {
                        TextView commentUser=(TextView)v.findViewById(R.id.feedcommentcell_username);
                        commentUser.setText(o.getString("username"));

                        String shareMessageId= o.getString("ShareMessageId");
                        o.put("ShareMessageId", shareMessageId);

                        TextView commentText=(TextView)v.findViewById(R.id.feedcommentcell_comment);
                        commentText.setText(o.getString("Message"));

                        TextView commentScanHour=(TextView)v.findViewById(R.id.feedcommentcell_scanhour);
                        commentScanHour.setText(o.getString("timestamp"));

                        ImageView commentImg=(ImageView)v.findViewById(R.id.feedcommentcell_profileimg);
                        String imgurl=o.getString("userimage");
                        if(imgurl!=null && imgurl!="")
                        imageLoader.DisplayImage(imgurl, context, commentImg);

                    }

                }
                else if(type==TYPE_EDIT)
                {
                    //JSONObject o = getChild(groupPosition,childPosition);
                    JSONObject o = itemsChildren.get(childPosition);


                        o.put("edit", "false");
                        v = vi.inflate(R.layout.edittextcommentlist, null);

                    //EditText edit = (EditText) v.findViewById(R.id.feedcell_edittxt);


                }




            }
            catch(Exception e)
            {}


            return v;
        }

这些是其他覆盖方法。我没有向您展示 getGroupView 和他的布局,因为可以正常工作..

    @Override
         public JSONObject getChild(int groupPosition, int childPosition) {
                return itemsChildren.get(childPosition);
            }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

        @Override
        public boolean areAllItemsEnabled()
        {
            return true;
        }
        @Override
        public int getChildrenCount(int groupPosition) {

            return itemsChildren.size();
        }

        @Override
        public JSONObject getGroup(int groupPosition) {
            return itemsGroup.get(groupPosition);
        }

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

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

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }

        public boolean isTextView(int groupPosition,int childPosition)
        {
            JSONObject jo=new JSONObject();
            jo = getChild(groupPosition,childPosition);
            boolean result =false;
            try
            {
                String isEdit=jo.getString("edit");
                if(isEdit.equalsIgnoreCase("true"))
                    result=true;

            }
            catch(Exception e){}
            return result;

        }

        public int getItemViewType(int groupPosition,int childPosition) 
        {
            //JSONObject jo=getChild(groupPosition,childPosition);


            if(isTextView(groupPosition,childPosition))
                return TYPE_EDIT;
            else
                return TYPE_CHILD;
        }

一切似乎都很好,但editText每次都会改变位置......有人有一些解决方案吗?谢谢

4

2 回答 2

0

由于isLastChild无法正常工作,您可以执行以下操作:

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

      if (childPosition == itemsGroup.get(groupPosition).size() - 1) {
           // This gives you confirmation that is the last child in the group
      }
}
于 2013-03-17T23:36:34.050 回答
0

I have the same problem with my ExpandableList... The main problem here is with lastChild property. You see, on creation, the last child of the group list is the one we think it is, but on scroll opposite, your list is refreshing so next last child is next which you can't see on opposite, or if you go back down, last one you can't see. This is pretty much stupid if you ask me but there is no right solution for this...

于 2013-01-17T01:16:16.767 回答