2016-06-01 11 views
1

Ich habe ein ExpandableListView wie folgt erklärt:Checkbox-Check springt zufällig um die Listenelemente herum?

<ExpandableListView 
     android:id="@+id/ListBExpandable" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

und ich habe das Layout Child Artikel wie folgt erklärt:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:minWidth="25px" 
    android:minHeight="25px" > 
    <CheckBox 
     android:text="CheckBox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/checkBoxBListItem" /> 
</LinearLayout> 

Als ich das Kontrollkästchen des Kindes bin Überprüfung und erweitern oder ein Gruppenelement der geprüft schließen checkbox start spring in scheint zufällige Position.

Dies ist die Quelle des Adapters:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 

namespace LEX.Droid 
{ 
    public class BActivityGroupedAdapter : BaseExpandableListAdapter 
    { 
     public static List<BNodeProxy> bNodes = bew List<BNodeProxy>(); 
     public static Dictionary<int, BGroupProxy> bGroups = new Dictionary<int, BGroupProxy>(); 

     Activity context; 
     List<int> groupKeys; 
     List<int> filteredGroupKeys; 
     Dictionary<int,List<int>> filteredItems; 

     public BActivityGroupedAdapter(Activity context, Dictionary<int,List<int>> filteredItems) 
     { 
      this.context = context;      
      groupKeys = bGroups.Keys.ToList(); 

      this.filteredGroupKeys = filteredGroupKeys; 
     } 

     public override Java.Lang.Object GetChild(int groupPosition, int childPosition) 
     { 
      int nodeId = filteredItems[ filteredGroupKeys[ groupPosition ] ][ childPosition ]; 

      return bNodes.Where(o => o.id == nodeId).First().label; 
     } 

     public override long GetChildId(int groupPosition, int childPosition) 
     { 
      return childPosition; 
     } 

     public override int GetChildrenCount(int groupPosition) 
     { 
      return filteredItems[ filteredGroupKeys[ groupPosition ] ].Count(); 
     } 

     public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent) 
     { 
      View view; 

      int nodeId = filteredItems[ filteredGroupKeys[ groupPosition ] ][ childPosition ]; 

      var item = bNodes.Where(o => o.id == nodeId).First(); 

      if(convertView == null) 
      { 
       view = context.LayoutInflater.Inflate(Resource.Layout.ListChildItem, null); 
      } 
      else 
      { 
       view = convertView; 
      } 

      try 
      { 

       var checkBoxSelect   = view.FindViewById<CheckBox>(Resource.Id.checkBoxListItem); 
        checkBoxSelect.Text  = item.label; 
        checkBoxSelect.Selected = item.selected; 
        checkBoxSelect.Tag  = item.id; 
        checkBoxSelect.Click -= theClickEvent; 
        checkBoxSelect.Click += theClickEvent; 
      } 
      catch(Exception ex) 
      { 

      } 
      view.Clickable   = false; 
      view.Focusable   = false; 
      view.FocusableInTouchMode = false; 
      view.LongClickable  = false; 
      return view; 
     } 

     public override Java.Lang.Object GetGroup(int groupPosition) 
     { 
      return bGroups[ filteredGroupKeys[ groupPosition ] ].label; 
     } 

     public override long GetGroupId(int groupPosition) 
     { 
      return groupPosition; 
     } 

     public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent) 
     { 
      View view; 

      var item = bGroups[ filteredGroupKeys[ groupPosition ] ].label; 

      if(convertView == null) 
      { 
       var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater; 
       view = inflater.Inflate(Resource.Layout.ListGroupItem, null);    
      } 
      else 
      { 
       view = convertView; 
      } 

      try 
      { 
       var textBox = view.FindViewById<TextView>(Resource.Id.groupItemListText); 
        textBox.SetText(item, TextView.BufferType.Normal); 
      } 
      catch(Exception ex) 
      { 
      } 

      return view; 
     } 
     public override bool IsChildSelectable(int groupPosition, int childPosition) 
     { 
      return true; 
     } 

     public override int GroupCount 
     { 
      get 
      { 
       return filteredGroupKeys.Count; 
      } 
     } 

     public override bool HasStableIds 
     { 
      get 
      { 
       return true; 
      } 
     } 

     private void theClickEvent(object sender, EventArgs e) 
     { 
      var selectionState = ((CheckBox)sender).Checked; 
      int tagId   = (int)((CheckBox)sender).Tag; 

      bNodes.Where(o => o.id == tagId).First().selected = selectionState; 

     } 
    } 
} 
  • Wie Sie das Kontrollkästchen aus diesem seltsamen Verhalten zu verhindern?

Antwort

1

der Fehler gefunden ..

checkBoxSelect.Selected = item.selected; 

eigentlich sein sollte:

checkBoxSelect.Checked = item.selected; 

scheint, wie mein Stack-Überlauf :) ging

Verwandte Themen