android - intent in child ExpandableListView

时间:2016-11-07 19:31:54

标签: android expandablelistview

我需要为我的expandablelistview中的每个孩子提供一个意图, 每个孩子都有不同的意图,我很难将其包含在onclick事件中。像我这样做?我的代码是:

public class MenuActivity extends ActionBarActivity {

private AnimatedExpandableListView listView;
private ExampleAdapter adapter;


@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_main);


    //Inicia a biblioteca de universal-image.jav
    ImageLoader imageLoader = ImageLoader.getInstance();
    if (!imageLoader.isInited()) {
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
    }

    List<GroupItem> items = new ArrayList<GroupItem>();
    items = fillData(items);


    getSupportActionBar().setTitle("AD Sergipe Mobile");

    adapter = new ExampleAdapter(this);
    adapter.setData(items);

    listView = (AnimatedExpandableListView) findViewById(R.id.list_view);
    listView.setDividerHeight(0);
    listView.setAdapter(adapter);

    // A fim de mostrar animações, é necessário usar um manipulador de cliques personalizados
    // para o ExpandableListView.
    listView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            // We call collapseGroupWithAnimation(int) and
            // expandGroupWithAnimation(int) to animate group
            // expansion/collapse.
            if (listView.isGroupExpanded(groupPosition)) {
                listView.collapseGroupWithAnimation(groupPosition);
            } else {
                listView.expandGroupWithAnimation(groupPosition);
            }
            return true;
        }

    });

    // Set indicator (arrow) to the right
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    Resources r = getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            50, r.getDisplayMetrics());
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        listView.setIndicatorBounds(width - px, width);
    } else {
        listView.setIndicatorBoundsRelative(width - px, width);
    }

}

private static class GroupItem {
    String title;
    String imageUrl;
    List<ChildItem> items = new ArrayList<ChildItem>();
}

private static class ChildItem {
    String title;
}

private static class ChildHolder {
    TextView title;
    LinearLayout layout;
    //  TextView icon;
}

private static class GroupHolder {
    TextView title;
    ImageView image;
    TextView button;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menuLogout) {
        logout();
    }
    return super.onOptionsItemSelected(item);
}

private List<GroupItem> fillData(List<GroupItem> items) {
    GroupItem item = new GroupItem();
    item.title = "Cadastro";
    item.imageUrl = "https://blink.ucsd.edu/_images/homepage/landing-pages/hr-touch.png";
    ChildItem child;
    child = new ChildItem();
    child.title = "Relação de Membros";
    item.items.add(child);

    child = new ChildItem();
    child.title = "Dados da Congregação";
    item.items.add(child);

    items.add(item);

    item = new GroupItem();
    item.title = "Batismo";
    item.imageUrl = "http://patrickschreiner.com/wp-content/uploads/2015/07/water-wave.jpg";
    child = new ChildItem();
    child.title = "Cadastrar Candidatos";
    item.items.add(child);

    child = new ChildItem();
    child.title = "Enviar Fichas de Batismo";
    item.items.add(child);

    child = new ChildItem();
    child.title = "Relação de Candidatos";
    item.items.add(child);

    items.add(item);

    item = new GroupItem();
    item.title = "Prestação de Contas";
    item.imageUrl = "http://contabilidaderevisa.com.br/wp-content/uploads/2015/09/CONTABILIDADE31.jpg";
    child = new ChildItem();
    child.title = "Semanas Fechadas";
    item.items.add(child);

    child = new ChildItem();
    child.title = "Semanas em Aberto";
    item.items.add(child);

    child = new ChildItem();
    child.title = "Pendências";
    item.items.add(child);

    items.add(item);

    item = new GroupItem();
    item.title = "Calendário";
    item.imageUrl = "http://clubedoconcurseiro.com.br/wp-content/uploads/2016/01/Calend%C3%A1rio.jpg";
    child = new ChildItem();
    child.title = "Eventos";
    item.items.add(child);

    child = new ChildItem();
    child.title = "Marcar Eventos";
    item.items.add(child);

    items.add(item);

    return items;
}

private class ExampleAdapter extends AnimatedExpandableListAdapter
        implements OnClickListener {
    private LayoutInflater inflater;

    private List<GroupItem> items;

    public ExampleAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public void setData(List<GroupItem> items) {
        this.items = items;
    }

    @Override
    public ChildItem getChild(int groupPosition, int childPosition) {
        return items.get(groupPosition).items.get(childPosition);
    }

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

    @Override
    public View getRealChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder;
        ChildItem item = getChild(groupPosition, childPosition);
        if (convertView == null) {
            holder = new ChildHolder();
            convertView = inflater
                    .inflate(R.layout.list_item_expandable_shop_child,
                            parent, false);
            holder.title = (TextView) convertView
                    .findViewById(R.id.list_item_expandable_shop_child_title);
            holder.layout = (LinearLayout) convertView
                    .findViewById(R.id.list_item_expandable_shop_child_layout);
            holder.layout.setOnClickListener(this);
            //          holder.icon = (TextView) convertView
            //                  .findViewById(R.id.list_item_expandable_shop_child_icon);
            //          holder.icon.setOnClickListener(this);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        //      holder.icon.setTag(childPosition);
        holder.layout.setTag(childPosition);
        holder.title.setText(item.title.toUpperCase());

        return convertView;
    }

    @Override
    public int getRealChildrenCount(int groupPosition) {
        return items.get(groupPosition).items.size();
    }

    @Override
    public GroupItem getGroup(int groupPosition) {
        return items.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupHolder holder;
        GroupItem item = getGroup(groupPosition);
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = inflater.inflate(
                    R.layout.list_item_expandable_shop, parent, false);
            holder.title = (TextView) convertView
                    .findViewById(R.id.list_item_expandable_shop_title);
            holder.button = (TextView) convertView
                    .findViewById(R.id.list_item_expandable_shop_button);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.list_item_expandable_shop_image);
            holder.button.setOnClickListener(this);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.button.setTag(groupPosition);
        ImageUtil.displayImage(holder.image, item.imageUrl, null);
        holder.title.setText(item.title);

        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        int position = (Integer) v.getTag();
        switch (v.getId()) {
            case R.id.list_item_expandable_shop_button:
                // click on explore button
                Toast.makeText(getApplicationContext(), "BUY " + position,
                        Toast.LENGTH_SHORT).show();
                break;
            case R.id.list_item_expandable_shop_child_layout:
                // click on explore button
                Toast.makeText(getApplicationContext(),
                        "Pay option:  " + position, Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

}

0 个答案:

没有答案