2016-09-01 44 views
0

其實我沒有得到大小,當我在android中打印Toast複選框Checked Event時。檢查所有子問題

這是我在父母的代碼複選框在Android中,如果我會得到的大小問題將自動解決。

此代碼在自定義適配器中聲明。

public void onBindViewHolder(final MyViewHolder holder, int position) { 
    //Toast.makeText(context, "Key: " + position, Toast.LENGTH_LONG).show(); 
    String val = allCityList.get(position); 
    Integer cnt = val.split("-").length; 

    if (cnt > 1) { 
     val = val.split("-")[1]; 
     holder.chkbox_parent.setText(val); 
     holder.chkbox_parent.setTag("State"); 
     holder.chkbox_parent.setTypeface(null, Typeface.BOLD); 

     stateTag = val; 
    } else { 
     val = val.split("-")[0]; 
     holder.chkbox_parent.setText(val); 
     holder.chkbox_parent.setTag(stateTag); 
    } 

holder.chkbox_parent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      CheckBox chk = (CheckBox) holder.chkbox_parent; 

      if (chk.getTag().equals("State")) { 
       String state = chk.getText().toString(); 
       Toast.makeText(context, state, Toast.LENGTH_LONG).show(); 
       try { 
        RecyclerView rv1 = holder.rvParent; 
//      Toast.makeText(context, ""+rv1.getTag(), Toast.LENGTH_LONG).show(); 
        for (int i = 0; i < rv1.getChildCount(); i++) { 
         Log.e("hello......", ""+rv1.getChildCount()); 
         CheckBox cb; 
         cb = (CheckBox) rv1.getChildAt(i).findViewById(R.id.chkbox_parent); 
         Toast.makeText(context, "" + cb.getText(), Toast.LENGTH_LONG).show(); 
//    if (state.equals(cb.getTag())) 
//     cb.setChecked(chk.isChecked()); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 

問題上recyclerview未來即rv.getchildcount

回答

0

首先,調用getChildCount()將返回只是在一瞬間可見的項目。 RecyclerView是ViewGroup的子類,它提供了這樣的方法,並不旨在直接訪問子視圖。您應該嘗試將檢查/取消選中/訪問狀態邏輯移動到您的Adapter並使用notifyDatasetChanged()notifyItemChanged(int position)通知適配器發生了什麼情況。

+0

請您詳細說明我理解的幾件事或者您能否給我演示程序。 – Abhi

+0

我只提出違反您所做的RecyclerView API。我認爲,讓孩子數不是你想要的(常見的錯誤),你真正想做的是獲得/設置一些特定的項目狀態。說實話,你似乎沒有完全理解RecyclerView是如何工作的,並且在你不知道一些基本概念的情況下,我很難一步一步地解釋一切。你應該嘗試通過一個RecyclerView教程,像這樣:[link](http://www.androidhive.info/2016/01/android-working-with-recycler-view/)。它會幫助你理解回收概念。 –

+0

我想你是對的,但我在代碼中顯示的是當我們檢查複選框所有子複選框被選中(全選)你有任何soln關於? – Abhi