I have a question about RecyclerView. I was trying to learn RecyclerView but I stuck with a problem. I am creating all views dynamically. So, Let say I have a ContentLinearLayout. My problem starts with "refreshContent" method. I am calling this several times. But, even on the first call, the inside views of RecyclerView stays in their background image size (I guess there is some implicit WRAP_CONTENT somewhere). I couldn't find a way to equalize the heights and widths of the buttons to the height of the RecyclerView. I tried iterating over RecyclerView and setting layout params for every child, but it didn't work. Also, I tried to set layout params inside "onBindViewHolder" but that didn't work, too.
Any help will be greatly appreciated.
public class ContentLinearLayout extends LinearLayout {
private RecyclerView rvCon;
private LinearLayout llCon;
public ContentLinearLayout(Context context) {
super(context);
this.init(context);
}
public ContentLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.init(context);
}
private void init(Context c){
this.setOrientation(LinearLayout.VERTICAL);
this.setBaselineAligned(false);
rvCon = new RecyclerView(c);
LinearLayout.LayoutParams recyclerPrms = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f);
recyclerPrms.setMargins(0, 0, 0, 0);
rvCon.setLayoutParams(recyclerPrms);
LinearLayoutManager manager = new LinearLayoutManager(c, LinearLayoutManager.HORIZONTAL, false);
rvCon.setLayoutManager(manager);
addView(rvCon);
rvCon.setPadding(0, 0, 0, 0);
LinearLayout llCon = new LinearLayout(c);
llCon.setOrientation(LinearLayout.HORIZONTAL);
llCon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 3f));
llCon.setBaselineAligned(false);
addView(container);
...
}
private void refreshContent(Context ctx, Content content){
rvCon.setAdapter(new ContentAdapter(ctx, content));
}
}
public class ContentAdapter extends RecyclerView.Adapter<ImageButtonHolder>{
private Context mContext;
private Content mContent;
public ContentAdapter(Context c, Content content){
mContext = c;
mContent = content
}
@Override
public int getItemCount() {
return content.size();
}
@Override
public void onBindViewHolder(ImageButtonHolder buttonHolder, int pos) {
}
@Override
public ImageButtonHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
ImageButton b = new ImageButton(mContext);
b.setBackgroundColor(Color.RED);
ImageButtonHolder holder = new ImageButtonHolder(b);
return holder;
}
}
public class ImageButtonHolder extends RecyclerView.ViewHolder {
public final ImageButton imageButton;
public ImageButtonHolder(ImageButton button) {
super(button);
imageButton = button;
}
}