本文介绍了GetChildView被不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索类似的主题,但他们并没有帮助我,他们是光不同,或者他们仍然没有答案,所以我会形容我的麻烦了。

I have searched similar threads but they didn't help me and they were light different or they still are unanswered, so I'll describe my trouble.

我有一个 ExpandableListView 自定义 ExpandableListAdapter 而且我可以告诉我的但不是我的孩子 GetChildView 不要让反正叫。

I have an ExpandableListView with a custom ExpandableListAdapter and I can show my Groups but not my Children. GetChildView don't get called anyway.

我会感谢你的帮助。

编辑:

如果我登录本节

...
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适配器小童人数:13 ExpListView小童人数:0

所以,我的适配器获得的数据,但变量客户端(ExpandableListView)没有意识到,有数据,虽然我正确设置它。

So, my Adapter gets Data but the variable client (ExpandableListView) don't realize, it has data although I properly set it.

我的code是以下

主要code

// 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 - >为 ExpandableListView

Main xml. field_list.xml -> Layout for 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 - >布局每个组项目

Group_client.xml -> Layout for each Group Item

<?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 - >布局为每个子项

child_client.xml -> Layout for each Child Item

<?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 ,使其工作在这个例子中细

EDIT(2):As said, it's no problem cause of Layouts or ClientAdapter, so that it works fine in this example

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");

}

}

现在谈到是什么让我困惑...

Now it comes what confuses me...

如果我测试全部用一个简单的活动,并在其主要布局有一个可点击的TextView 然后工作正常(电源适配器和相同的调用)!

If I test all(Adapter and the same calls) with a simple Activity, which has a clickable TextView at its main layout then works fine!

如果我点击这个TextView的那么 ExpandableListView 提示与正确填充儿童,他们是扩展 / 可折叠

If I click this TextView then the ExpandableListView prompts with properly populated Group and Children and they are expandable/collapsable.

如果我测试像我的最后一个职位hier,(<$c$c>ExpandableListView嵌入在的ListView 项目),因此,在 ExpandableListView 提示,如果我点击了 ExpandableListView 的是项目
然后这些组被填充,但不是儿童。 GetChildView 被永远不会被调用!!!(我用LogCat中,所以我完全确定)

If I test like my last post hier,(ExpandableListView embedded in a ListView Item) so, the ExpandableListView prompts, if I click that the ExpandableListView of that Itemthen the groups are populated but not the children. GetChildView gets never called!!!(I used LogCat, so I'm completely sure)

我所看到的,很多人不得不在ListViewItems在ExpandableListViews点击的烦恼,我的情况是不同的,我可以执行,我的麻烦是理解,为什么用一个TextView正常工作的ExpandableListView人口通过我的自定义 BaseExpandableListAdapter 而不是在一个的ListView 项目。

I have seen, many people had troubles with clicks in ExpandableListViews in ListViewItems, my case is different, I can perform that, my trouble is understanding, why with a TextView works fine that ExpandableListView Population via my custom BaseExpandableListAdapter but not in a ListView Item.

推荐答案

解决

在我的第一级适配器(第一级 ExpandableListView 适配器)我修改了 getChildView 方法

In my first level Adapter (An adapter for the first level ExpandableListView) I modified the getChildView Method

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

这是它现在的方面根据需要

That is its aspect now as desired

这篇关于GetChildView被不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:14