2
votes

So I have a horizontal scrollview with recyclerView inside it. I want item rows which is inflated in onCreateViewHolder to be dynamic and not static xml as I don't know how many columns are gonna be there and I also need to set some text on them from webservice which i can't figure out how to achieve in onBind method from viewholder

Here is my adapter

@NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        RecyclerView.ViewHolder holder;
      //  View v = inflater.inflate(R.layout.exec_category_sales_row,parent, false);
        CustomView itemView = new CustomView(parent.getContext());

        itemView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        ));
        return new ViewHolder(itemView);


    }
 @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        final ViewHolder viewHolder = (ViewHolder) holder;

        ((ViewHolder) holder).customView.setUser(arylstCategoryWiseSales.get(position));

    }

private class ViewHolder extends RecyclerView.ViewHolder {

private CustomView customView;
        public ViewHolder(View v) {
            super(v);

            customView = (CustomView) v;
        }

My customview class

public class CustomView extends RelativeLayout {
    private CategoryWiseSalesModel user;
    private TextView textView;

    // override all constructors to ensure custom logic runs in all cases
    public CustomView(Context context) {
        this(context, null);
    }
    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomView(
            Context context,
            AttributeSet attrs,
            int defStyleAttr,
            int defStyleRes
    ) {
        super(context, attrs, defStyleAttr, defStyleRes);


        inflate(getContext(), R.layout.custom_layout, this);
        textView =  findViewById(R.id.textView);
    }
//called in onBindViewHolder
    public void setUser(CategoryWiseSalesModel newUser) {
        user = newUser;
        textView.setText(user.getLights());
    }
}

Different view Types won't work for me as there is as item row is completely dynamic. I also thought of using tablelayout but I have lazy loading logic onScroll going on here.

How to go about this ???

1
Do you want different layout for every row because you don't know the columns count on each row ?Syed Hamza Hassan
No,Each row is gonna have same column count depending on webservice which could change for instance today it can have 3 textviews inside row and next day 5 textview in row depending on data from service as I said..Thanks for your quick replycoderBox
Ok, Have you tried with generating Text views using programming ?Syed Hamza Hassan
Do I need to generate these textview in OnbindViewHolder it could mess up on scroll??please correct me if I'm wrongcoderBox
No, You will create TextViews onCreateViewHolder. After inflating XML i.e blank layout. You will then Generate Textview and add in to your layout.Syed Hamza Hassan

1 Answers

1
votes

Dynamic TextViews Generation

Here is your Sample Code

Main Class code for calling and setting Adapter.

public class MainActivity extends AppCompatActivity {
        RecyclerView mRecycleview;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            List<String> mList = new ArrayList<>();
            mList.add("Hello");
            mList.add("World");

            mRecycleview = findViewById(R.id.recycleview);


            mRecycleview.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false));
            PlannerAdapter mPlannerAdapter = new PlannerAdapter(MainActivity.this, mList, 5);//Contuctor having context, datalist and number of textviews. i.e you can get it from your data on runtime
            mRecycleview.setAdapter(mPlannerAdapter);
            mPlannerAdapter.notifyDataSetChanged();

        }
    }

Here is Adapter code to generate Dynamic TextViews

    public class PlannerAdapter extends RecyclerView.Adapter<PlannerAdapter.RecyclerViewHolder> {
    private Context context;
    LinearLayout mainlayout;
    List<String> strings;
    List<String> tvIds;
    int mTextviewCount;

    public PlannerAdapter(Context context, List s, int mTexviewQuantity) {
        this.context = context;
        this.strings = s;
        this.mTextviewCount = mTexviewQuantity;
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapterlayout, viewGroup, false);
        mainlayout = view.findViewById(R.id.mainlayout);
        tvIds = new ArrayList<>();

        for (int ind = 0; ind < mTextviewCount; ind++) {
            LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                    130, 120);
            TextView tv = new TextView(context);
            tv.setLayoutParams(lparams);
            tv.setText("test");
            tv.setTag("mytv" + ind);
            tvIds.add("mytv" + ind);

            mainlayout.addView(tv);
        }
        RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
        return recyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position) {

        for (int index = 0; index < tvIds.size(); index++) {
            recyclerViewHolder.mTextViewList.get(index).setText(strings.get(position) + "  " + index + "  ");
        }

    }

    @Override
    public int getItemCount() {

        return strings.size();
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public android.widget.TextView tvShopName, tvDateTime;
        List<TextView> mTextViewList = new ArrayList<>();


        public RecyclerViewHolder(View itemView) {
            super(itemView);

            for (int ind = 0; ind < tvIds.size(); ind++) {
                TextView mTextView = new TextView(context);
                mTextView = itemView.findViewWithTag(tvIds.get(ind));
                mTextViewList.add(mTextView);
            }
        }

        @Override
        public void onClick(View view) {

        }
    }

}

It is your question about dynamic textView generation done and Sample Code has attached. I hope it will help you, please ask if query