2014-10-20 125 views
0

我有一個包含一組自定義視圖的列表視圖。每個自定義視圖內都是一個gridview。從自定義列表適配器更新自定義gridview android

我希望能夠在用戶點擊自定義視圖中的複選框時更新gridview中每個單元格的背景。

這裏是我的自定義視圖

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" > 

    <TextView 
     android:id="@+id/lblListHeader" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="32sp" /> 

    <CheckBox 
     android:id="@+id/lblHeaderCheckbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:focusable="false" 
     android:layout_centerVertical="true" /> 
</RelativeLayout> 

<ca.package.CustomGridView 
    android:id="@+id/gridView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:clickable="false" 
    android:gravity="center" 
    android:numColumns="7" 
    android:padding="10dp" 
    android:stretchMode="columnWidth" 
    clickable="true" /> 

這裏是我的每個網格單元

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridItem" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="center_horizontal" 
android:orientation="vertical" 
android:clickable="true" > 

<TextView 
    android:id="@+id/grid_itemTxt" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center"> 
</TextView> 

</LinearLayout> 

這裏佈局我如何填充列表

expListView = (ListView) this.findViewById(R.id.event_list); 

    prepareListData(); 

    listAdapter = new CustomAdapter(this, listDataHeader, 
      listDataChild, listChildBoolean); 

    expListView.setAdapter(listAdapter); 

這裏是getView方法我對於ListView

public View getView(int groupPosition, View convertView, ViewGroup parent) { 

    final int gp = groupPosition; 

    String headerTitle = (String) getGroup(groupPosition); 

    // Boolean bool = _listDataBoolean.get(groupPosition); 
    Boolean bool = currentCalendarEvents.get(groupPosition).getAlert(); 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 
    } 

    String[] data = new String[14]; 

    List<String> times = _currentListDataChild.get(_currentListDataHeader 
      .get(groupPosition)); 

    data[0] = "Su"; 
    data[1] = "M"; 
    data[2] = "Tu"; 
    data[3] = "W"; 
    data[4] = "Th"; 
    data[5] = "F"; 
    data[6] = "Sa"; 

    data[7] = times.get(6); // sun 
    data[8] = times.get(0); // mon 
    data[9] = times.get(1); // tues 
    data[10] = times.get(2); // wed 
    data[11] = times.get(3); // thurs 
    data[12] = times.get(4); // fri 
    data[13] = times.get(5); // sat 

    ArrayList<Boolean> boolArray = _listChildBoolean.get(_currentListDataHeader 
      .get(groupPosition)); 

    final GridView gridView = (GridView) convertView.findViewById(R.id.gridView); 

    final GridAdapter adapter = new GridAdapter(this._context, data, boolArray); 

    gridView.setAdapter(adapter); 

    TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.lblListHeader); 
    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle); 

    final CheckBox chk2 = (CheckBox) convertView 
      .findViewById(R.id.lblHeaderCheckbox); 
    chk2.setChecked(bool); 

    chk2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      if (chk2.isChecked()) { 
       currentCalendarEvents.get(gp).setAlert(true); 
       currentCalendarEvents.get(gp).setDaysTrue(); 

       // I WANT TO UPDATE THE LIST HERE 

       Log.i(LOG_TAG, "box checked"); 

      } else { 
       currentCalendarEvents.get(gp).setAlert(false); 
       currentCalendarEvents.get(gp).setDaysFalse(); 

       // AND HERE 

       Log.i(LOG_TAG, "box not checked"); 
      } 
     } 
    }); 


    convertView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Log.i(LOG_TAG, "gridview clicked"); 
      currentCalendarEvents.get(gp).setBoolDays(adapter.getHashMap()); 
     } 
    }); 

    return convertView; 
} 

自定義適配器這裏是我的GridAdapter

public class GridAdapter extends BaseAdapter { 

private Context context; 
private String[] items; 
private ArrayList<Boolean> boolArray; 
private ArrayList<View> views; 
LayoutInflater inflater; 

public GridAdapter(Context context, String[] items, 
     ArrayList<Boolean> boolArray) { 
    this.context = context; 
    this.items = items; 
    this.boolArray = boolArray; 
    this.views = new ArrayList<View>(); 
    inflater = (LayoutInflater) this.context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

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

    if (convertView == null) { 

     convertView = inflater.inflate(R.layout.grid_cell, null); 

     final TextView tv = (TextView) convertView 
       .findViewById(R.id.grid_itemTxt); 
     tv.setText(items[position]); 

     if (position < 7) { 
      convertView.setBackgroundColor(Color.DKGRAY); 
      tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24); 
      tv.setTextColor(Color.WHITE); 
      tv.setTypeface(null, Typeface.BOLD); 
      tv.setLines(2); 
     } else { 

      Log.e("blerp", "test : " + items[position]); 

      if (!items[position].equals("")) { 
       if (boolArray.get(position % 7)) { 
        convertView.setBackgroundColor(context.getResources() 
          .getColor(R.color.LightGreen)); 
       } else { 
        convertView.setBackgroundColor(Color.LTGRAY); 
       } 

       tv.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (boolArray.get(position % 7)) { 
          ((View) tv.getParent()) 
            .setBackgroundColor(Color.LTGRAY); 
          boolArray.set(position % 7, false); 
         } else { 
          ((View) tv.getParent()) 
            .setBackgroundColor(context 
              .getResources().getColor(
                R.color.LightGreen)); 
          boolArray.set(position % 7, true); 
         } 
        } 
       }); 

      } else 
       convertView.setBackgroundColor(Color.LTGRAY); 
      tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); 
      tv.setLines(2); 
     } 

    } 

    return convertView; 
} 

@Override 
public int getCount() { 
    return items.length; 
} 

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

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

public HashMap<String,Boolean> getHashMap() { 
    HashMap<String, Boolean> hm = new HashMap<String,Boolean>(); 
    hm.put("sun", boolArray.get(0)); 
    hm.put("mon", boolArray.get(1)); 
    hm.put("tues", boolArray.get(2)); 
    hm.put("wed", boolArray.get(3)); 
    hm.put("thurs", boolArray.get(4)); 
    hm.put("fri", boolArray.get(5)); 
    hm.put("sat", boolArray.get(6)); 
    return hm; 
} 


} 

任何幫助將不勝感激,我可以張貼如果需要的話我更多的代碼。

回答

0

啓動適配器類中的gridview和複選框(使用按ID查找視圖)。
然後通過 如果(checkBox.isChecked){
gridview.setBackgroundColor(Color.rgb(X,Y,Z)))}

+0

謝謝,這是有益的,我能夠改變的背景整個gridview,但我需要能夠改變每個單元格的背景。任何想法我可以怎麼做呢? – edbert 2014-10-20 15:13:11

+0

使用convertView.setBac ....而不是gridview.setBac ...在if條件中。 convertView代表視圖的1個單元格。 – 2014-10-20 15:19:28

+0

PLZ標記答案有用,如果它解決了你的問題 – 2014-10-20 15:20:01