膨胀清单中的OnChildClick不起作用

时间:2016-06-08 15:40:16

标签: android mobile click expandablelistview expandablelistadapter

我创建了一个ExpandableListView,但OnClickListener无法正常工作。 已经尝试过其他解决方案,并且都遵循我所做的相同模式。发现一个这样的解决方案是How to get onChildClick value from expandablelistview in android?

我的活动:

public class GroupRoutesActivity extends AppCompatActivity {
public static ExpandableListView expandableListView;
private static AdapterExpandableListGroups expandableListViewAdapter;
ArrayList<Group> listProcess;
Context ctx;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_group_routes);

    ctx = GroupRoutesActivity.this;
    expandableListView = (ExpandableListView) findViewById(R.id.expandable_group_routes);
    expandableListView.setGroupIndicator(null);
    expandableListView.setChildIndicator(null);

    listProcess = new ArrayList<Group>();

    for(int i = 0; i<=2; i++){
        Group obj = new Group();
        obj.setId(String.valueOf(i));
        obj.setGroup(String.valueOf("Group "+i));
        obj.setCod("Cod Group "+String.valueOf(i));
        listProcess.add(obj);
    }

    HashMap<Group, List<Equipament>> allChildItems = returnGroupedChildItems();

    expandableListViewAdapter =
            new AdapterExpandableListGroups(GroupRoutesActivity.this,listProcess, allChildItems);


    expandableListView.setAdapter(expandableListViewAdapter);

    expandableListView.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            Equipament e = ((Equipament)expandableListViewAdapter.getChild(groupPosition, childPosition));
            Toast.makeText(ctx, e.getCodEquipament(), Toast.LENGTH_LONG).show();
            return true;
        }
    });


}

private HashMap<Group, List<Equipament>> returnGroupedChildItems() {

    HashMap<Group, List<Equipament>> childContent = new HashMap<Group, List<Equipament>>();

    List<Equipament> equipaments = new ArrayList<Equipament>();
    for(int i=0; i<=2; i++){
        Equipament e = new Equipament();
        e.setId(String.valueOf(i));
        e.setType("Equipamemt "+i);
        e.setCodEquipament("Cod Equip. "+String.valueOf(i));
        equipaments.add(e);
        childContent.put(listProcess.get(i), equipaments);
    }
    return childContent;
}

AdapterExpandableList:

public class AdapterExpandableListGroups extends BaseExpandableListAdapter{
private Context context;
private ArrayList<Group> list;
private HashMap<Group, List<Equipament>> childDataSource;

private static Activity act;

public static int groupPosition = 0, childPosition = 0;


public AdapterExpandableListGroups(Context context, ArrayList<Group> list, HashMap<Group, List<Equipament>> child) {
    this.context = context;
    this.list = list;
    this.childDataSource = child;
}

@Override
public int getGroupCount() {
    return this.list.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return this.childDataSource.get(this.list.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return list.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.childDataSource.get(list.get(groupPosition)).get(childPosition);
}

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

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

@Override
public boolean hasStableIds() {
    return false;
}
static class ViewHolderParentGroup {
    LinearLayout linearBackground;
    TextView textCod;
    TextView textDesc;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ViewHolderParentGroup viewHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.layout_row_group_expandable, null);
        viewHolder = new ViewHolderParentGroup();
        viewHolder.linearBackground = (LinearLayout) convertView.findViewById(R.id.linear_background_exapandable_group);
        viewHolder.textCod = (TextView) convertView.findViewById(R.id.text_cod_group);
        viewHolder.textDesc = (TextView) convertView.findViewById(R.id.text_desc_group);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolderParentGroup) convertView.getTag();
    }

    viewHolder.textCod.setText(list.get(groupPosition).getCod());
    viewHolder.textDesc.setText(list.get(groupPosition).getGroup());
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    ViewHolderParentGroup viewHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.layout_row_equipament_expandable, null);
        viewHolder = new ViewHolderParentGroup();
        viewHolder.linearBackground = (LinearLayout) convertView.findViewById(R.id.linear_background_exapandable_equipament);
        viewHolder.textCod = (TextView) convertView.findViewById(R.id.text_cod_equipament);
        viewHolder.textDesc = (TextView) convertView.findViewById(R.id.text_desc_equipament);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolderParentGroup) convertView.getTag();
    }
    Equipament e = ((Equipament)getChild(groupPosition, childPosition));

    viewHolder.textCod.setText(e.getCodEquipament());
    viewHolder.textDesc.setText(e.getType());

    viewHolder.linearBackground.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Equipament e = ((Equipament)getChild(AdapterExpandableListGroups.groupPosition, AdapterExpandableListGroups.childPosition));
            //Toast toast = Toast.makeText(context, e.getCodEquipament(),Toast.LENGTH_SHORT);
            //toast.show();
        }
    });

    this.groupPosition = groupPosition;
    this.childPosition = childPosition;
    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

}

和我的ExpandableList布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9">
    <ExpandableListView
        android:id="@+id/expandable_group_routes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:childDivider="#000"
        android:dividerHeight="0sp"
        android:divider="#000"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您的孩子通过viewHolder.linearBackground ClickListener单击侦听器重叠。

评论下面的代码,您将能够点击子项。

viewHolder.linearBackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Equipament e = ((Equipament)getChild(AdapterExpandableListGroups.groupPosition, AdapterExpandableListGroups.childPosition));
                //Toast toast = Toast.makeText(context, e.getCodEquipament(),Toast.LENGTH_SHORT);
                //toast.show();
            }
        });