2015-06-25 4 views
16

मैं उपयोग कर रहा हूँ का उपयोग कर एक दृश्य का चयन रद्द करने की कोशिश कर रहा एक GridView के अंदर एक ExpandableListViewgetChildAt()

मैं एक समारोह है कि एक आइटम जब यह क्लिक किया जाता है पर प्रकाश डाला गया, अब मैं एक बटन को लागू करने की कोशिश कर रहा हूँ जब दबाया कि यह सभी चयनित आइटम का चयन रद्द होगा, लेकिन यह अचयनित है केवल पिछले दृश्य क्लिक किया

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 है छ:

जब मैं एक से सभी आइटम का चयन इस तरह बी को:

:

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

एंड्रॉयड 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

यह मूल तत्व के माध्यम से चला जाता है, पहला बच्चा लेता है और कलन विधि के लिए एक पुनरावर्ती कॉल करता है, तो यह दूसरा है, तीसरे आदि ... जब कोई बच्चों के माध्यम से जाने के लिए छोड़ दिया हैं, यह कहता है नोड के लिए अपने अचयनित कार्य।

ए, सी, ई, डी, बी, एच, मैं, जी, एफ

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

पोस्ट करूंगा, मैंने सवाल संपादित कर लिया है, क्योंकि मेरे पास 2 समूहों के साथ एक विस्तारणीय लिस्ट व्यू है, इसलिए मेरे पास 2 व्यू ग्रुप हैं, मैं सभी बच्चों को कैसे प्राप्त कर सकता हूं तुरंत? – 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

का चयन रद्द कर रहा है क्या आप वाकई उस दृश्य के संदर्भ में गुजर रहे हैं जिसमें दोनों अनुभाग हैं? क्या यह हमेशा वही है जो अचयनित करता है, या क्या वह वह व्यक्ति है जिसमें आप क्लिक कर रहे छात्र हैं? –

संबंधित मुद्दे