2012-11-19 22 views
2

我有一個動態添加靜態EditText的問題,總是在最後一個子元素位置。我的適配器使用類型的JSON對象。第一次展開ExpandableListView組的子項時,可以找到我的EditText的正確位置,但是當我沿相反方向滾動子項時,我的EditText會不斷更改位置。這是我的Editext佈局:添加一個靜態editText視圖作爲expandableListView的最後一個子Android

<?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的每一次變化的位置......有人有一些解決方案?謝謝

回答

0

我有同樣的問題與我的ExpandableList ...這裏的主要問題是與lastChild財產。你看,創建時,組列表中的最後一個孩子是我們認爲的那個孩子,但是在滾動的對面,你的列表很清爽,所以下一個孩子是下一個你不能在對面看到的孩子,或者如果你回去最後一個你看不到。這是非常愚蠢的,如果你問我,但對此沒有合適的解決方案...

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 
     } 
} 
+0

我都嘗試過,但它是也沒有工作。 你有其他解決方案嗎? –

相關問題