2

Я искал похожие темы, но они мне не помогли, и они были легкими, или они все еще остались без ответа, поэтому я опишу свою проблему.GetChildView не называется

У меня есть ExpandableListView с customExpandableListAdapter и я могу показать свою Groups, но не мой Children. GetChildView все равно не вызываются.

Я благодарю вас за помощь.

EDIT:


Если я вхожу этот раздел

... 
client = (ExpandableListView)row.findViewById(R.id.field_expandable_list); 
client.setFocusable(false);     
client_adapter = new ClientAdapter(context, titles, allFields); 
client.setAdapter(client_adapter); 

Log.i("TAG", "WmsMApActivity::doClient:: Adapter #children: "+client_adapter.getChildrenCount(0)+ 
         " ExpListView #children: "+client.getChildCount()); 

... 

, то я понимаю, что

I/TAG(692): WmsMApActivity::doClient::0 Adapter #children: 13 ExpListView #children: 0

Итак, мой адаптер получает данные, кроме переменная client (ExpandableListView) не понимают, у него есть данные, хотя я правильно их установил.


Мой код выглядит следующим образом

Основной код

// view = Item of my ListView. We come from a OnClickItem Method 
private void doClient(View view) { 

    ArrayList<String> titles = new ArrayList<String>(); 
    titles.add("fields"); 

    ArrayList allFields = transformFarmsResult(parentItems); 

    final Context context = getApplicationContext(); 

    RelativeLayout mainLayout = (RelativeLayout) view.findViewById(R.id.relLayoutDrawer); 

    // We check whether the first item (0th position) is expanded 

    View lin_layout_field_list = view.findViewById(R.id.lin_layout_field_list); 

    if (lin_layout_field_list != null) { // Hide Field_list Layout 

     ((ViewGroup) view).removeView(lin_layout_field_list); 


    } else { 

     mainLayout = (RelativeLayout) view.findViewById(R.id.relLayoutDrawer); 

     // ----------- Inflating ExpandableListView "Fields" 


     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row    = inflater.inflate(R.layout.field_list, null); 

     client     = (ExpandableListView)row.findViewById(R.id.field_expandable_list); 

     // Adapter Population       
     client_adapter = new ClientAdapter(context, titles, allFields);     
     client.setAdapter(client_adapter); 

     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 


     TextView title_tv = (TextView)view.findViewById(R.id.title); 
     params.addRule(RelativeLayout.ALIGN_LEFT, title_tv.getId()); 
     params.addRule(RelativeLayout.BELOW, title_tv.getId());     

     mainLayout.addView(row, params); 

     // Listview Group click listener 
     client.setOnGroupClickListener(new OnGroupClickListener() { 

      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
       Toast.makeText(context, "Group Clicked " + groupPosition, Toast.LENGTH_SHORT).show(); 
        return false; 
       } 
     }); 

     // Listview Group expanded listener 
     client.setOnGroupExpandListener(new OnGroupExpandListener() { 

       @Override 
       public void onGroupExpand(int groupPosition) { 
       Toast.makeText(context, "Group " + groupPosition + " Expanded", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

     // Listview Group collasped listener 
     client.setOnGroupCollapseListener(new OnGroupCollapseListener() { 

      @Override 
      public void onGroupCollapse(int groupPosition) { 
        Toast.makeText(context, "Group " + groupPosition + " Collapsed", Toast.LENGTH_SHORT).show();; 

       } 
      }); 

     // Listview on child click listener 
     client.setOnChildClickListener(new OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
       // TODO Auto-generated method stub 
       Toast.makeText(context, "Group " + groupPosition + " : Child " + childPosition, Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     });      

    } 

} 

адаптер

public class ClientAdapter extends BaseExpandableListAdapter{ 

private Context context; 
//private String header_title; 
private ArrayList<String> headers; 

private ArrayList fields; 

private int groupPosition = 0; 
private LayoutInflater inflater; 

public ClientAdapter(Context context, ArrayList<String> headers, ArrayList fields) { 

    this.context  = context; 
    this.headers  = headers; 
    this.fields  = fields; 
    inflater   = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    // Just to check, whether I get correct ArrayList "fields" 
    Log.i("TAG","ClientAdapter::Constructor:: fields.size: "+fields.size()); 
    for (int i=0;i<fields.size();i++) 
     Log.i("TAG","ClientAdapter::Constructor:: field["+i+"]: "+fields.get(i).toString()); 
} 

@Override 
public int getGroupCount() { 
    // Just 1 Group 
    return headers.size(); 
} 

@Override 
public String getGroup(int groupPosition) { 
    return headers.get(groupPosition); 
} 

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


@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View view = convertView; 
    GroupHolder holder=null; 

    String title_item = getGroup(groupPosition); 

    if (view == null) {   
     holder = new GroupHolder(); 
     view = inflater.inflate(R.layout.group_client, null); 
     holder.title = (TextView) view.findViewById(R.id.client_group_title); 

     view.setTag(holder); 
    } else { 
     holder = (GroupHolder) view.getTag(); 
    } 

    holder.title.setText(title_item); 

    return view; 
} 

// Children 
@Override 
public int getChildrenCount(int groupPosition) { 
    return fields.size(); 
} 


@Override 
public Object getChild(int groupPosition, int childPosition) { 
    // TODO Auto-generated method stub 
    return fields.get(childPosition); 
} 

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


@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View view = convertView; 
    ChildHolder holder; 

    Log.i("TAG", "ClientAdapter::getChildView:: 0 - parent.id: "+parent.getId()); 
    final HashMap<String,Object> field = (HashMap<String,Object>) getChild(groupPosition, childPosition); 


    if (view == null) { 
     holder = new ChildHolder(); 


     view = inflater.inflate(R.layout.child_client, null); 

     holder.title = (TextView) view.findViewById(R.id.client_child_title); 
     holder.map_icon = (ImageView) view.findViewById(R.id.client_child_icon); 
     holder.contour = (CheckBox) view.findViewById(R.id.client_child_checkbox); 

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

    // Handling of Title 
    String temp =(String)field.get(Constants._FIELDNAME); 
    Log.i("TAG", "ClientAdapter::getChildView:: title: "+temp); 
    holder.title.setText((String)field.get(Constants._FIELDNAME)); 

    // Handling of Contour - If it exists! 
    if (field.containsKey(Constants._BOUNDARY)) { 
     holder.contour.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if (((CheckBox)v).isChecked()) { 
        Toast.makeText(context, "Contour enabled", Toast.LENGTH_LONG).show(); 
        // TODO : Fade-in Contour in Graphics 
       } else { 
        Toast.makeText(context, "Contour disabled", Toast.LENGTH_LONG).show(); 
        // TODO : Fade-in Contour in Graphics      
       } 

      } 

     }); 
    } else { // If there is no Contour, remains the checkbox but it's not clickable, so, it's grey! 
     holder.contour.setFocusable(false); 
     holder.contour.setClickable(false); 
    } 

    // Handling of Map Icon 
    Bitmap bitmap_icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.gmaps_icon_24); 
    holder.map_icon.setImageBitmap(bitmap_icon); 

    // Applying gray filter if not referentiable 
    if (!field.containsKey(Constants._LATITUDE) || !field.containsKey(Constants._LONGITUDE))       
     holder.map_icon.setColorFilter(Color.GRAY);     

    holder.map_icon.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if (field.containsKey(Constants._LATITUDE) && field.containsKey(Constants._LONGITUDE)) {     
       Toast.makeText(context, "Center in Field Position", Toast.LENGTH_LONG).show(); 
      } else { 
       Toast.makeText(context, Constants.NOT_CENTRED, Toast.LENGTH_LONG).show(); 
      } 
     } 

    }); 

    return view; 
} 


@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    // TODO Auto-generated method stub 
    return true; 
} 

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

/** Events **/ 


/** Holder Classes **/ 
private static class ChildHolder { 
    TextView title; 
    ImageView map_icon; 
    CheckBox contour; 

} 

private static class GroupHolder { 
    TextView title; 
} 

}

Мои Layouts.

Главная xml. field_list.xml -> Макет для ExpandableListView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/lin_layout_field_list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ExpandableListView 
     android:id="@+id/field_expandable_list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" <- That's a SO advice, in order that children won't be croped. Originally wrap_content 
     android:layout_weight="1" 
     android:headerDividersEnabled="true" 
     android:divider="@color/metallic_silver" 
     android:dividerHeight="1dp"> 

    </ExpandableListView> 

</LinearLayout> 

Group_client.xml -> Разметка для каждой группы Пункт

<?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="wrap_content"> 
    <LinearLayout 
     android:id="@+id/lin_layout_client_group" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" >  
     <TextView 
      android:id="@+id/client_group_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingLeft="30dp" 
      android:text="Something" 
      android:textColor="@android:color/white" 
      android:textSize="16dp" 
      android:textStyle="normal" /> 
    </LinearLayout> 

</LinearLayout> 

child_client.xml -> Разметка для каждого дочернего элемента

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

    <LinearLayout 
     android:id="@+id/lin_layout_client_child" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <CheckBox 
      android:id="@+id/client_child_checkbox" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="0.05" 
      android:checked="true" 
      android:gravity="center_vertical|center" /> 

     <TextView 
      android:id="@+id/client_child_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_vertical" 
      android:padding="10dp" 
      android:text="FIELD_NAME" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textColor="@android:color/white" 
      android:layout_weight="0.90" /> 

     <ImageView 
      android:id="@+id/client_child_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="0.05" 
      android:layout_margin="5dp" 
      android:gravity="center_vertical|center" 
      android:src="@drawable/gmaps_icon_24" /> 

    </LinearLayout> 

</LinearLayout> 

EDIT (2): Как уже говорилось, это не проблема с макетами или ClientAdapter , Так что он отлично работает в этом примере

public class ClientActivity2 extends Activity{ 

private ExpandableListView client; 
private ClientAdapter client_adapter; 

private ArrayList children; 
private ArrayList<String> groups; 

private LayoutInflater inflater; 
private LinearLayout lin_layout_probe; 

private Context context; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.field_list); 
    context = getApplicationContext(); 

    prepareData();   
    ExpandableListView client = (ExpandableListView)findViewById(R.id.field_expandable_list); 
    client_adapter = new ClientAdapter(context, groups, children); 
    client.setAdapter(client_adapter); 
    client.expandGroup(0); 


} 

private void prepareData() { 
    children = new ArrayList(); 

    HashMap<String,Object> hm1 = new HashMap<String,Object>(); 
    hm1.put(Constants._FIELDNAME,"Child1"); 
    children.add(hm1); 

    HashMap<String,Object> hm2 = new HashMap<String,Object>(); 
    hm2.put(Constants._FIELDNAME,"Child2");  
    children.add(hm2); 

    HashMap<String,Object> hm3 = new HashMap<String,Object>(); 
    hm3.put(Constants._FIELDNAME,"Child3");  
    children.add(hm3); 


    groups = new ArrayList<String>(); 
    groups.add("Group1"); 

} 

}

Теперь приходит, что меня смущает ...

Если я проверяю все (адаптер и те же вызовы) с простой деятельности , который имеет clickable TextView на своем основном макете, тогда работает отлично!

Если я нажимаю эту TextView то ExpandableListView наводящие с должным заселена Group и Children и они expandable/collapsable.

Если я проверить, как мой последний пост hier, (ExpandableListView вложенного в ListView Пункте) Так, ExpandableListView подсказок, если я нажму, что ExpandableListView этого Item то группы заселены, но не дети. GetChildView никогда не звонил !!! (я использовал LogCat, так что я полностью уверен)

Я видел, у многих людей были проблемы с щелчками в ExpandableListViews в ListViewItems, мой случай отличается, я могу выполнить это, моя проблема это понимание, почему с TextView отлично работает, что ExpandableListView Population через мой пользовательский BaseExpandableListAdapter, но не в ListView Элемент.

+0

Я зарегистрировался, и я понял, вместо этого 'getChildView' регистрируется' getView' '' ListView' –

ответ

2

решаемые

В моем первом уровне адаптера (адаптер для первого уровня ExpandableListView) Я модифицировал getChildView Метод

... 
// Member Field 
private ExpandableListView expandable_field_list; 
... 

@Override 
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    if (groupPosition == 1) { 
     if (expandable_field_list == null) { 
      expandable_field_list = new CustomExpandableListView(context); 

      // Here I initialize the second and third Level 
      client_adapter  = new ClientAdapter(context, groups, children); 
      expandable_field_list.setAdapter(client_adapter); 

     } else { 
      client_adapter.notifyDataSetChanged(); 
     } 
     convertView = expandable_field_list; 
    } 


    return convertView; 
} 

... 

И CustomExpandableListView выглядит эта

public class CustomExpandableListView extends ExpandableListView { 

    private boolean expanded = true; 

    public CustomExpandableListView(Context context) { 
     super(context); 
    } 


    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     if (isExpanded()) { 
      // View.MEASURED_SIZE_MASK represents the largest height possible. 
      int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, 
       MeasureSpec.AT_MOST); 
      super.onMeasure(widthMeasureSpec, expandSpec); 

      ViewGroup.LayoutParams params = getLayoutParams(); 
      params.height = getMeasuredHeight(); 
     } else { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    }  


    @Override 
    protected void onDetachedFromWindow() { 
     try { 
      super.onDetachedFromWindow(); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void setIsExpanded(boolean expanded) { 
     this.expanded = expanded; 
    } 

    public boolean isExpanded() { 
     return expanded; 
    }  
} 

Это его аспект теперь по желанию

enter image description here

Смежные вопросы