androidandroid-recyclerview

Android recycle view - created dynamic button stack


With my recycle view I have 4 different layouts, working fine. One layout I want to add x number of buttons in a stack depending on the data. For instance, one row may have 3 buttons, the next 5, the next 2 etc. I've done this programmatically from LayoutButtonViewHolder extends RecyclerView.ViewHolder class. However I can only get the data for each position at the public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) class as I need position to get the relevant data.

Problems is as the LayoutButtonViewHolder extends RecyclerView.ViewHolder class is called before the public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position), I'm unable to set the number of buttons as I need the position in order to get relevant data from the data List.

How do I get the ViewHolder class to create the view based on the data I get from the onBindHolder?

I'm probably going about this the wrong way but despite days of searching, can't see how I achieve this.

class LayoutButtonViewHolder extends RecyclerView.ViewHolder {
    public TextView question;
    public LinearLayout buttonStack;

    public LayoutButtonViewHolder(View itemView) {
        super(itemView);
        question = (TextView) itemView.findViewById(R.id.question);
        buttonStack = (LinearLayout) itemView.findViewById(R.id.buttonlayout);
        LinearLayout buttonStack = (LinearLayout) itemView.findViewById(R.id.buttonlayout);
        
        for (int f = 0; f < buttonNumber; f++) {
            Button btnTag = new Button(buttonStack.getContext());
            btnTag.setText(buttonOptions[f]);
            buttonStack.addView(btnTag);
        }

        }
    
    }

    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    
            final String dataType = calculatorFormList.get(position).getDataType();
            final String dataLabel = calculatorFormList.get(position).getDataLabel();
            final String dataOptions = calculatorFormList.get(position).getDataOptions();
            final String dataOptionsScores = calculatorFormList.get(position).getDataOptionScores();
            String[] options = dataOptions.split(Pattern.quote(","));
    
    
    
            switch (holder.getItemViewType()) {
                case 1:
                    LayoutButtonViewHolder viewHolder1 = (LayoutButtonViewHolder) holder;
                    ((LayoutButtonViewHolder) holder).question.setText(dataLabel);
                    buttonNumber = options.length;
                    buttonOptions = options;
                    break;

Solution

  • You could do it like this:

    Create another method in the viewholder, you can call it onBind or something similar, and call it from onBindViewHolder and passs the data at the position from calculatorFormList. The move this code from the view holder constructor to that new method. Remember to remove all children from buttonStack.

    buttonStack.removeAllViews()
    for (int f = 0; f < buttonNumber; f++) {
        Button btnTag = new Button(buttonStack.getContext());
        btnTag.setText(buttonOptions[f]);
        buttonStack.addView(btnTag);
    }
    

    Hope you can solve your issue. If you need more clarification, let me know.