GetChildView未被调用

时间:2014-10-15 09:36:49

标签: android-listview expandablelistview android-custom-view expandablelistadapter

我搜索过类似的帖子,但他们没有帮助我,他们的情况有所不同,或者他们仍然没有答案,所以我会描述我的麻烦。

我有ExpandableListView custom ExpandableListAdapter,我可以显示Groups但不能显示ChildrenGetChildView无论如何都不会被打电话。

我会感谢您的帮助。

修改


如果我记录本节

...
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;
}   

}

我的布局。

主xml。 field_list.xml - &gt; 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 - &gt;每个组项的布局

<?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 - &gt;每个子项的布局

<?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>

修改(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");

}

}

现在它让我感到困惑......

如果我使用一个简单的Activity测试所有(适配器和相同的调用),它的主要布局有一个可点击的TextView,那么工作正常!

如果我单击此TextView,则ExpandableListView会提示正确填充GroupChildren,并且expandable / collapsable

如果我的测试类似于上一篇文章hier,(ExpandableListView项目中嵌入了ListView),那么ExpandableListView会提示,如果我点击{{1}那个ExpandableListView 然后这些组被填充但不是孩子。永远不会调用Item !!!(我使用LogCat,所以我完全确定)

我已经看到,很多人在ListViewItems中的ExpandableListViews中遇到麻烦,我的情况有所不同,我可以执行,我的麻烦是理解,为什么用TextView工作正常通过我的自定义GetChildView ExpandableListView人口但不在BaseExpandableListAdapter项目中。

1 个答案:

答案 0 :(得分: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