onClick in Expandable RecyclerView (using bignerdranch library)

元气小坏坏 提交于 2019-12-08 09:23:31

问题


In the project I'm working, I get a bunch of "Projects"(parent) with "Issues" (child). As I have to show it in a list, I used RecyclerView to do it. To achieve it I used BigNerdRanch library (https://github.com/bignerdranch/expandable-recycler-view)

The problem come's that I need to click on Child to take all the information of the issue and send it to another activity.

The library don't have any method to handle the child click, so I tried to implement a work around and worked (I think is ok..), but here I'm blocked. I don't know how to take the clicked child to launch the activity with the necessary info.

Here is the ProjectAdapter:

public class ProjectAdapter extends ExpandableRecyclerAdapter<ProjectViewHolder, IssueViewHolder> {

    private LayoutInflater mInflator;
    private Context appContext ;

    public ProjectAdapter(Context context, @NonNull List<? extends ParentListItem> parentItemList) {
        super(parentItemList);
        this.mInflator = LayoutInflater.from(context);
        this.appContext = context;
    }

    @Override
    public ProjectViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
        View projectView = mInflator.inflate(R.layout.jira_project, parentViewGroup, false);
        return new ProjectViewHolder(projectView);
    }

    @Override
    public IssueViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
        View issueView = mInflator.inflate(R.layout.jira_issue, childViewGroup, false);
        return new IssueViewHolder(issueView,this.appContext);
    }

    @Override
    public void onBindParentViewHolder(ProjectViewHolder projectViewHolder, int position, ParentListItem parentListItem) {
        Project project = (Project) parentListItem;
        projectViewHolder.bind(project);
    }

    @Override
    public void onBindChildViewHolder(IssueViewHolder issueViewHolder, int position, Object childListItem) {
        Issue issue = (Issue) childListItem;
        issueViewHolder.bind(issue);
    }
}

IssueViewHolder (Child):

public class IssueViewHolder extends ChildViewHolder{

    private TextView mIssueTextView; // contendra de momento el "summary" del issue
    private Context appContext;

    public IssueViewHolder(View itemView, final Context appContext) {
        super(itemView);
        this.appContext = appContext;
        this.mIssueTextView = (TextView) itemView.findViewById(R.id.jir_issue_name);
        this.mIssueTextView.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Log.d("JIRA", "CLICk CHILD");
                Toast.makeText(appContext, "CHILD #"+getAdapterPosition() +"\n CHILD##"+getPosition(), Toast.LENGTH_LONG).show();
            }
        });
    }

    public void bind(Issue issue){
        mIssueTextView.setText(issue.getKey());
    }
}

As I said,I'm stucked on How to get the child information.

How can I solve it?


回答1:


  @Override
    public void onBindChildViewHolder(IssueViewHolder issueViewHolder, int position, Object childListItem) {
        Issue issue = (Issue) childListItem;
        issueViewHolder.bind(issue);

      issueViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //your code
                    }
                });
    }


来源:https://stackoverflow.com/questions/38181355/onclick-in-expandable-recyclerview-using-bignerdranch-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!