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 ???