Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any samples please? #5

Open
vijimscse opened this issue Apr 6, 2016 · 1 comment
Open

Any samples please? #5

vijimscse opened this issue Apr 6, 2016 · 1 comment

Comments

@vijimscse
Copy link

vijimscse commented Apr 6, 2016

Right now, only groups are being displayed. not the child list.On clicking group, corresponding children list should be displayed. When I debugged, getChildrenList() is not getting called at all. It is better if shared some samples for understanding.

@AndXorr
Copy link

AndXorr commented Apr 18, 2016

I think you need override @canExpand in your ViewHolder.
Example simple adapter for String(group) and String(child).
I have a worked project. If you have any questions you can ask me.

package biz.andxor.trains.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import biz.andxor.trains.R;
import biz.andxor.trains.views.RecyclerView.ExpandableRecyclerView;

public class CabinAdapter extends ExpandableRecyclerView.ExpandableAdapter<CabinAdapter.Holder, String> {

    private Context context;
    private List<String> listGroups;
    private Map<String, List<String>> listChildren;

    public CabinAdapter(Context context, List<String> listDataHeader, Map<String, List<String>> listChildData) {
        this.context = context;
        this.listGroups = listDataHeader;
        this.listChildren = listChildData;
    }


    @NonNull
    @Override
    protected Holder onCreateExpandableViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new Holder(LayoutInflater.from(context).inflate(R.layout.item_group, parent, false));
            case 1:
                return new Holder(LayoutInflater.from(context).inflate(R.layout.item_list, parent, false));
            default:
                throw new IllegalArgumentException("Unknown view type ="+viewType);
        }
    }

    @Override
    protected void onBindGroupView(Holder holder, int groupPosition) {
        holder.group.setText(listGroups.get(groupPosition));
    }

    public Object getChild(int groupPosition, int childPosition) {
        return listChildren.get(listGroups.get(groupPosition)).get(childPosition);
    }

    @Override
    protected void onBindChildView(Holder holder, int groupPosition, int childPosition) {
        final String childText = (String) getChild(groupPosition, childPosition);
        holder.item.setText(childText);
    }

    @Override
    protected int getGroupCount() {
        return listGroups.size();
    }

    @Override
    protected int getChildrenCount(int groupPosition) {
        return listChildren.get(this.listGroups.get(groupPosition)).size();
    }

    @Override
    protected int getGroupViewType(int groupPosition) {
        return 0;
    }

    @Override
    protected int getChildViewType(int groupPosition, int childPosition) {
        return 1;
    }

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

    public class Holder extends ExpandableRecyclerView.ExpandableViewHolder {

        private TextView group, item;

        public Holder(View itemView) {
            super(itemView);
            group = (TextView) itemView.findViewById(R.id.lblListHeader);
            item = (TextView) itemView.findViewById(R.id.lblListItem);
        }

        @Override
        protected boolean canExpand() {
            return true;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants