2015-06-25 71 views
16

我使用一個視圖GridView內的ExpandableListView試圖取消使用getChildAt()

我有一個突出被點擊時,它的項目的功能,現在我想實現一個按鈕,按下時它會取消選擇所有選定的項目,但它取消選擇只有最後單擊了看法

public class GridAdapter extends BaseAdapter { 

    private Context mContext; 
    private ArrayList<Filhos> child; 
    public ArrayList<CadastraEscolas> escola; 
    private ArrayList<ArrayList<Filhos>> filhos = new ArrayList(); 



    public GridAdapter(Context context, ArrayList<CadastraEscolas> groups, ArrayList<Filhos> childValues, int groupPosition) { 
     mContext = context; 
     child = childValues; 
     escola = groups; 
     posicaoEscola = groupPosition; 


    } 


    @Override 
    public int getCount() { 
     return child.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, final ViewGroup parent) { 


     holder = null; 



     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.child_item, null); 
      holder = new ViewHolder(); 


      final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca); 
      final TextView nomeAluno = (TextView) convertView.findViewById(R.id.name); 



      convertView.setTag(holder); 

      final View finalConvertView = convertView; 
      convertView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 





        if (list.size() > 0) { 

         isChecked = filhos.get(posicaoEscola).get(position).isChecked(); 


         if (!isChecked) { 

          selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE); 


         } else { 

          selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE); 

         } 



        ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(final View v) { 


          limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE); 



         } 
        }); 


       } 
      }); 


     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.text.setText(child.get(position).getNome()); 


     return convertView; 


    } 


    static class ViewHolder { 
     TextView text; 
    } 



    public void selecao(boolean check, int position, TextView nomeAluno, int color, View v, int visibility) { 
    filhos.get(posicaoEscola).get(position).setChecked(check); 
    nomeAluno.setTextColor(color); 
    v.findViewById(R.id.overlay).setVisibility(visibility); 
    v.findViewById(R.id.overlayText).setVisibility(visibility); 
} 

public void limpaSelecao(boolean check, int position, TextView nomeAluno, int color, ViewGroup v, int visibility) { 



    for (int x = 0; x < group.size(); x++) { 

     for (int j = 0; j < group.get(x).getalunos().size(); j++) { 


      if(filhos.get(x).get(j).isChecked()){ 
       v.getChildAt(j).findViewById(R.id.loadingPanel).findViewById(R.id.overlay).setVisibility(View.GONE); 
      } 



      nomeAluno.setTextColor(color); 
     } 
    } 


} 

} 

佈局:

<RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:background="@drawable/nomealuno_main" 
       android:layout_below="@+id/child"> 

       <View 
        android:id="@+id/overlay" 
        android:layout_width="125dp" 
        android:layout_height="50dp" 
        android:background="@color/bgOverlay" 
        android:visibility="gone"/> 
</RelativeLayout> 

這裏是什麼是happenin G:

當我選擇從A所有的項目給B這樣的:

enter image description here

和命中清楚,它只會從我點擊了最後一組中刪除選定的孩子

enter image description here

我需要將它們全部刪除,當我點擊清除時,所有的孩子,不管他們在什麼組中都必須被刪除。

ExpandableAdapter:

public class ExpandListTest extends BaseExpandableListAdapter { 


    public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE; 


    public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL; 

    /** 
    * No child could be selected 
    */ 
    public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE; 

    /** 
    * One single choice per group 
    */ 
    public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE; 

    /** 
    * One single choice for all the groups 
    */ 
    public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001; 

    private Context context; 
    public static ArrayList<CadastraEscolas> groups; 
    private ArrayList<ArrayList<Filhos>> child = new ArrayList(); 
    private HashMap<String, GPSEscolas> aMap = new HashMap<String, GPSEscolas>(); 
    private HashMap<String, GPSEscolas> HashTask = new HashMap<String, GPSEscolas>(); 
    public static ArrayList<Filhos> listchild; 
    private GridAdapter adapter; 
    private SecurePreferences Sessao; 
    public static CustomGridView gridView; 
    private SparseArray<SparseBooleanArray> checkedPositions; 
    private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName(); 


    public ExpandListTest(Context context, ArrayList<CadastraEscolas> groups, HashMap<String, GPSEscolas> data, SecurePreferences mSessao, HashMap<String, GPSEscolas> hashTask) { 
     this.context = context; 
     this.groups = groups; 
     this.aMap = data; 
     this.Sessao = mSessao; 
     checkedPositions = new SparseArray<SparseBooleanArray>(); 
     child = new ArrayList(); 
     if (groups != null) { 
      for (int i = 0; i < groups.size(); i++) { 
       child.add(i, groups.get(i).getalunos()); 
      } 
     } 
    } 


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

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

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


     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.gridview, null); 
     } 


     listchild = new ArrayList<Filhos>(); 

     for (int j = 0; j < groups.get(groupPosition).getalunos().size(); j++) { 

      listchild.add(child.get(groupPosition).get(j)); 

     } 


     gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar); 

     gridView.setExpanded(true); 
     adapter = new GridAdapter(context, groups, listchild, child, groupPosition, aMap, HashTask, Sessao); 
     gridView.setAdapter(adapter);// Adapter 
     gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE); 



     return convertView; 

    } 


    @Override 
    public int getChildrenCount(int nGroup) { 
     return 1; 

    } 


    @Override 
    public Object getGroup(int groupPosition) { 
     return groups.get(groupPosition); 
    } 

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     CadastraEscolas group = (CadastraEscolas) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater inf = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = inf.inflate(R.layout.group_item, null); 
     } 
     ExpandableListView mExpandableListView = (ExpandableListView) parent; 
     mExpandableListView.expandGroup(groupPosition); 
     TextView tv = (TextView) convertView.findViewById(R.id.group_name); 
     tv.setText(group.getNome_fantasia()); 

     return convertView; 
    } 

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

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

} 
+0

閱讀ViewGroup文檔 – pskink

+0

@pskink所有權利我讀過它,現在可以幫助我嗎? – AND4011002849

+0

是你想從它的父母或什麼? – Karoly

回答

6

的Android ViewGroups可以包含任意數量的意見,但意見只能有一個父ViewGroup中,如果您嘗試添加已經有一個父視圖,你會得到這個異常(從ViewGroup.addViewInner()):

throw new IllegalStateException("The specified child already has a parent. " + 
       "You must call removeView() on the child's parent first."); 

這意味着你的佈局結構是一個樹(不是圖)的,所以你可以使用任何樹遍歷算法通過查看每個迭代的佈局。

以下是可能的算法,這是一種後序遍歷一個。

enter image description here

它經過的根元素,採取的第一個孩子,使一個遞歸調用的算法,那麼它的第二,第三等..時,有沒有孩子離開穿過去,它會調用您取消選擇節點的功能。

A,C,E,d,B,H,I,G,F

public void onClickTheButton(View view) { 
    unselectall(R.layout.your_layout); 
} 

public void unselectAll(View view) { 
    if(view instanceof ViewGroup) { 
     for(int ii = 0 ; ii<(ViewGroup)view.getChildrenCount(); ii++) { 
      unselectAll((ViewGroup)view.getChildAt(ii)); 
     } 
    } 
    unselect(view); 
} 

,可以看到:

對於圖中的樹中,節點將在該順序來取消選擇許多其他的方式來寫:https://en.wikipedia.org/wiki/Tree_traversal

它是學習,如果你想減輕你的編程經驗,這些算法是如何工作的一絕。

+0

我已經編輯了這個問題,因爲我有一個ExpandableListView裏面有2個組,所以我有2個ViewGroups,我怎麼能得到所有的孩子立刻? – AND4011002849

+0

向我解釋這是怎麼工作的,所以我可以接受你的答案作爲合適的人 – AND4011002849

+0

這樣可以嗎? –

1

如下更改getView()方法。

@Override 
public View getView(final int position, View convertView, final ViewGroup parent) { 
    holder = null; 

    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.child_item, null); 
     holder = new ViewHolder(); 

     final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.text.setText(child.get(position).getNome()); 

    final View finalConvertView = convertView; 
    convertView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if (list.size() > 0) { 
       isChecked = filhos.get(posicaoEscola).get(position).isChecked(); 

       if (!isChecked) { 
        selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE); 
       } else { 
        selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE); 
       } 

       ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(final View v) { 

         limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE); 
        } 
       }); 
      } 
     }); 

     return convertView; 
    } 
+0

檢查此並讓我知道它是否工作? –

+0

不,得到相同的結果,只有最後一個組單擊是取消選擇 – AND4011002849

+0

你確定你傳遞的視圖包含兩個部分的引用嗎?它總是與未經選擇的一樣,還是包含你點擊的學生的那一個? –