2014-10-30 69 views
0

我擺弄我的自定義BaseExpandableListAdapter,並在我的getChildView方法中,我似乎得到了隨機位置的子視圖。無論是否回收,我都會得到隨機的職位訂單。例如,在convertView = NULL條件分支我得到:在BaseExpandableListAdapter - getChildView隨機位置

logcat的照片(ConvertView == NULL):

childPosition:childPosition - 0 childPosition:childPosition - 1 childPosition:childPosition - 0 childPosition:childPosition - 3 childPosition:childPosition - 4 childPosition:childPosition - 在(ConvertView!= NULL)0

logcat的打印:

childPosition:childPosition - 1 childPosition:childPosition - 2 childPosition:childPosition - 3 childPosition:childPosition - 4 childPosition:childPosition - 5 childPosition:childPosition - 6 childPosition:childPosition - 7 childPosition:childPosition - 8 childPosition :childPosition - 0 childPosition:childPosition - 1個 childPosition:childPosition - 0 childPosition:childPosition - 1

這裏是我的適配器代碼:

public class MyExapndableListAdapter extends BaseExpandableListAdapter { 

    private Context context; 
    private List<String> headerList; 
    private HashMap<String, List<String>> _listDataChild; 

    public MyExapndableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) { 
     this.context = context; 
     this._listDataChild = listChildData; 
     this.headerList = listDataHeader; 

     Log.d("LIST CHILD", "" + _listDataChild.size()); 
     Log.d("LIST HEADER", "" + this.headerList.size()); 
    } 


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

    @Override 
    public int getChildrenCount(int i) { 
     return this._listDataChild.get(this.headerList.get(i)).size(); 
    } 

    @Override 
    public Object getGroup(int i) { 
     return this.headerList.get(i); 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this.headerList.get(groupPosition)).get(childPosititon); 
    } 

    @Override 
    public long getGroupId(int i) { 
     return i; 
    } 

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

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

     View v = convertView; 

     String headerTitle = (String) getGroup(groupPosition); 

     if (v == null) { 
      LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.list_group, null); 
      ViewHolder holder = new ViewHolder(); 
      holder.addView(v.findViewById(R.id.blaGroup)); 
      v.setTag(holder); 
     } 

     ViewHolder holder = (ViewHolder) v.getTag(); 

     MyText title = (MyText) holder.getView(R.id.blaGroup); 
     title.setText(headerTitle); 

     return v; 
    } 

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


     final String childText = (String) getChild(groupPosition, childPosition); 



     View row = convertView; 

     if (row == null) { 

//   Log.d("NULL VIEW", "childText - " +childText); 
      Log.d("childPosition", "childPosition - " +childPosition); 

      LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.list_child, null); 
      ViewGroup.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

      final float GESTURE_THRESHOLD_DIP = 30.0f; 

      // Convert the dips to pixels 
      final float scale = context.getResources().getDisplayMetrics().density; 
      int pixel = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f); 

      Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/pacifico.ttf"); 
      int fontColor = context.getResources().getColor(R.color.testColor); 
      int strokeWidth = 5; 
      int strokeColor = context.getResources().getColor(R.color.card_white); 

      // Add TextView on the fly 
      MyText text = new MyText(context, pixel, childText, font, fontColor, strokeWidth, strokeColor); 
      text.setLayoutParams(lp); 
      text.setText(childText); 
      text.setTextSize(GESTURE_THRESHOLD_DIP); 
      text.setId(3005); 

      ((ViewGroup) row).addView(text); 


      ViewHolder holder = new ViewHolder(); 
      holder.addView(text); 
      row.setTag(holder); 



     } 

     else { 
//   Log.d("NOT NULL VIEW", "childText - " +childText); 
      Log.d("NOT NULL VIEW childPosition", "childPosition - " +childPosition); 
     } 

     ViewHolder holder = (ViewHolder) row.getTag(); 
     MyText text = (MyText) holder.getView(3005); 
     text.setText(childText); 

     return row; 
    } 

    public static class ViewHolder { 

     private HashMap<Integer, View> storedViews = new HashMap<Integer, View>(); 

     public ViewHolder() { 
     } 

     public ViewHolder addView(View view) { 
      int id = view.getId(); 
      storedViews.put(id, view); 
      return this; 
     } 

     public View getView(int id) { 
      return storedViews.get(id); 
     } 
    } 

    @Override 
    public boolean isChildSelectable(int i, int i2) { 
     return true; 
    } 

} 
+0

您是否使用getchild方法獲取錯誤的項目。 – Piyush 2014-11-10 06:31:52

回答

0

由於您使用的是散列圖,您將得到一個隨機位置。散列圖在其內部排序數據方面有其自己的方法。所以我建議使用自定義對象而不是Hashmap。

+0

你介意舉個例子嗎? – Yosi199 2014-10-31 09:46:46

+0

@ Yosi199我想他是在討論擴展一個'List',並且自己做了「關鍵發現」,但是在一個'List '上工作。 – Korcholis 2014-11-06 11:45:21

+0

對不起,延遲迴復。如果你的'_listDataChild'只是一個字符串列表,你可以使用字符串數組來代替。看看這個[教程](http://www.vogella.com/tutorials/AndroidListView/article.html) – Harry 2015-01-15 02:24:58

0

那麼childView的位置不是隨機的,只是它們是來自不同組的子位置,將它們與groupId一起打印,您將會了解發生了什麼。此外,childPosition不依賴於convertview是否爲null,通常爲列表項調用getChildView,這將在滾動時進入屏幕。 希望這會有所幫助,如果你想要一個例子,請回復。