2012-11-17 42 views
4

我有一個ExpandableListView複選框旁邊的組名稱和擴展時,與複選框以及子項目。可擴展列表視圖與selectall複選框:組itemclick和滾動錯誤

假設我有4組50個孩子。當一個組被擴展時,我點擊選擇所有複選框,一切正常,所有複選框被選中,並保持他們的狀態,如果我滾動。

但是,如果我滾動直到列表的最後一個孩子,滾動後得到越野車(滾動到頂部,然後觸摸屏幕停止滾動不再工作了),點擊組不再工作了直到我再次點擊selectall複選框。 點擊一個孩子複選框什麼都不做,我必須點擊selectall複選框才能工作。

我改變了兒童複選框,組複選框的可調焦狀態,並嘗試了很多方法,但我找不到解決方案。你知道它來自哪裏嗎?

public class ExpandableListAdapter extends BaseExpandableListAdapter { 


    LayoutInflater cinflater; 
    LayoutInflater ginflater; 
    ArrayList<Group> array; 
    Context context; 


    public ExpandableListAdapter(Context context, ArrayList<Group> array) { 
     super(); 

     cinflater = LayoutInflater.from(context); 
     ginflater = LayoutInflater.from(context); 

     this.array = array; 
     this.context = context; 

    } 


    public class ChildViewHolder { 

     CheckBox contactcheckbox; 
     TextView name; 

    } 


    @Override 
    public int getChildrenCount(int groupPosition) { 
     // TODO Auto-generated method stub 
     return array.get(groupPosition).getContacts().size(); 
    } 


    private OnCheckedChangeListener contactchecklistener = new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) { 

        Contact contact = (Contact) checkboxView.getTag(); 
        contact.setCheck(isChecked); 

       } 

      }; 

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

     ChildViewHolder cholder; 

     if(convertView == null){ 

      cholder = new ChildViewHolder(); 

      convertView = cinflater.inflate(R.layout.childsitems, null); 

      cholder.name = (TextView) convertView.findViewById(R.id.childnametextview); 

      cholder.contactcheckbox = (CheckBox) convertView.findViewById(R.id.childcheckbox); 
      cholder.contactcheckbox.setOnCheckedChangeListener(contactchecklistener); 


      convertView.setTag(cholder); 

     }else{ 

      cholder = (ChildViewHolder) convertView.getTag(); 

     } 

     cholder.contactcheckbox.setTag(array.get(groupPosition).getContacts().get(childPosition)); 
     cholder.contactcheckbox.setChecked(array.get(groupPosition).getContacts().get(childPosition).isCheck()); 

     cholder.name.setText(array.get(groupPosition).getContacts().get(childPosition).getName()); 

     return convertView; 
    } 



    public class GroupViewHolder { 
     TextView groupname; 
     CheckBox groupcheck; 

    } 


    @Override 
    public int getGroupCount() { 
     // TODO Auto-generated method stub 
     return array.size(); 
    } 

    private OnCheckedChangeListener groupchecklistener = new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

      int index = (Integer) buttonView.getTag(); 

      for(int i = 0;i<array.get(index).getContacts().size();i++){ 
       array.get(index).getContacts().get(i).setCheck(isChecked); 
      } 

      array.get(index).setCheck(isChecked); 

      notifyDataSetChanged(); 

     } 
    }; 


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


     GroupViewHolder gholder; 

     if(convertView==null){ 
      gholder = new GroupViewHolder(); 
      convertView = ginflater.inflate(R.layout.groupsitems,null); 

      gholder.groupname = (TextView)convertView.findViewById(R.id.groupnametextview); 
      gholder.groupcheckbox = (CheckBox)convertView.findViewById(R.id.groupcheckbox); 
      gholder.groupcheckbox.setOnCheckedChangeListener(groupchecklistener); 

      convertView.setTag(gholder); 

     }else{ 
      gholder = (GroupViewHolder)convertView.getTag(); 
     } 

     gholder.groupname.setText(array.get(groupPosition).getGroupname()); 
     gholder.groupname.setTextSize(24); 
     gholder.groupname.setTextColor(Color.parseColor("#FF858585")); 
     gholder.groupname.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 

     int paddingleft = 48; // 38 dps 
     final float scale = getResources().getDisplayMetrics().density; 
     int dpvalue = (int) (paddingleft * scale + 0.5f); 
     gholder.groupname.setPadding(dpvalue, 16, 0, 16); 


     gholder.groupcheckbox.setTag(groupPosition); 
     gholder.groupcheckbox.setChecked(array.get(groupPosition).isCheck()); 

     if(isExpanded == true){ 
        convertView.setBackgroundDrawable(getResources().getDrawable(R.drawable.expandedbackground)); 
     }else{ 
      convertView.setBackgroundColor(Color.TRANSPARENT); 
     } 

     return convertView; 
    } 





    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public boolean hasStableIds() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

} 

groupsitems.xml:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 


    <CheckBox 
     android:id="@+id/groupcheckbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="12dp" 
     android:focusable="false" 
     android:gravity="right|center" 
     android:paddingRight="10dp" /> 

<TextView 
android:id="@+id/groupnametextview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:layout_alignParentTop="true" 
android:layout_toLeftOf="@+id/groupcheckbox" 
android:focusableInTouchMode="false" 
android:text="Large Text" 
android:textAppearance="?android:attr/textAppearanceLarge" 
android:textColor="#FF858585" 
android:textStyle="bold" /> 

</RelativeLayout> 

childitems.xml:

​​

+0

@ADR你在哪裏讀到,「點擊最後一項拋出錯誤」?我看不到有關拋出異常的信息......這種編輯是不正確的。 – Sam

+0

發佈您的相關代碼,否則我們可能無法爲您提供幫助。 – Sam

+0

是的,沒有錯誤或什麼,我從來沒有談到點擊最後一項...我沒有發佈的代碼,以保持簡單,但它在那裏。 – Samet

回答

4

我找到了解決方案。而不是使用OnCheckedChangeListener在groupcheckbox的,我用OnClickListener並解決一切是這樣的:

gholder.groupcheckbox.setChecked(array.get(groupPosition).isCheck()); 
gholder.groupcheckbox.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       for(int i = 0;i<array.get(groupPosition).getContacts().size();i++){ 
        array.get(groupPosition).getContacts().get(i).setCheck(gholder.groupcheckbox.isChecked()); 
       } 

       array.get(groupPosition).setCheck(gholder.groupcheck.isChecked()); 

       notifyDataSetChanged(); 
      } 
     }); 
+1

哈哈,就像我在寫一個答案一樣,你只是打敗了我。這是一個令人討厭的問題,我之前遇到過。基本上發生的事情是你的listview被鎖定,因爲它獲取'notifyDataSetChanged();'*很多*,太多了。當您滾動並且新的複選框進入視圖時,您的方法正在更正複選框以顯示它們是否已被選中。然後onCheckedChanged被調用。這是一個必要的困境,因爲你正確使用'viewHolder'模式並檢查null'convertview'。正如你所做的那樣,我認爲將它設置爲onClick是正確的。 – mango

+0

無論如何感謝您的幫助。這麼簡單但卻很難找到。 – Samet

相關問題