添加静态编辑文本视图作为可扩展列表视图 Android 的最后一个子视图



我在动态添加静态编辑文本时遇到问题,总是在最后一个子位置。我的适配器使用类型的 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>

这是我的孩子布局.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;
        }

一切似乎都很好,但是每次编辑文本都会更改位置...有人有一些解决方案吗?谢谢

我的ExpandableList也有同样的问题......这里的主要问题是lastChild财产。你看,在创建时,组列表的最后一个子项是我们认为的那个,但在对面的滚动上,你的列表是刷新的,所以下一个最后一个子项是下一个你在对面看不到的子项,或者如果你回去,最后一个你看不到。如果你问我,这几乎是愚蠢的,但没有正确的解决方案......

由于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
      }
}

最新更新