In my RecyclerView
adapter I want to set the top margin of the items depending on their position. I only want items after the first item to have a top margin. In my code I set the margin of the RecyclerView
item programmatically.
When I try to add an item to the RecyclerView
however, the item is not visible. I've looked here and here but my code is still not working. Does it make a difference that I'm setting the margin programmatically inside of a RecyclerView
? I tried using LinearLayout.LayoutParams
and RecyclerView.LayoutParams
but still nothing.
Any help would be appreciated.
UPDATE:
I was using the following code to convert dp
to px
int top = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
R.dimen.element_row_margin_top,
r.getDisplayMetrics()
);
I was passing the RAW dimension value instead of calling:resources.getDimen()
.
Here is my onBindViewHolder
code:
if (position != 0) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 10, 0, 0);
holder.linearLayout.setLayoutParams(params);
}
Here is my rec_list_item
(Contains an Image
and 2 TextInputlayouts
):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
ViewHolder code:
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
LinearLayout item = (LinearLayout) LayoutInflater.from(parent.getContext())
.inflate(R.layout.rec_list_item, parent, false);
MyViewHolder vh = new MyViewHolder(item);
return vh;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public LinearLayout linearLayout;
public MyViewHolder(LinearLayout item) {
super(contributorItem);
linearLayout = item;
}
}