I am using RecyclerView Android to make a chat line with left/right message box. I want to set gravity to item of RecyclerView. In normally way, I cast itemView to LinearLayout and then set ParamLayout gravity for it. But if RecyclerView, it seems not right to cast to LinearLayout. It will return RecyclerView.LayoutParams. Because of:
LayoutParams subclass for children of RecyclerView. Custom layout managers are encouraged to create their own subclass of this LayoutParams class to store any additional required per-child view metadata about the layout.
And i can't find the way to set gravity with RecyclerView.LayoutParams :| Anyway, could anybody find the way to gravity item in RecyclerView? Pls suggest me.
-----------POST SOLUTION------------------
//1. Children of RecyclerView
<LinearLayout
android:id="@+id/id_grandparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:divider="@android:color/transparent"
android:gravity="left"
android:orientation="vertical">
<LinearLayout
android:id="@+id/id_llparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_customer_feedback_left"
android:gravity="left"
android:orientation="vertical">
</LinearLayout>
//2. OnBindView()
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params = setLayoutParamsForParent(params, item.isPosition());
holder.llParentLayout.setLayoutParams(params); //corresponding to id_llparent ID
//3.
private LinearLayout.LayoutParams setLayoutParamsForParent(LinearLayout.LayoutParams params, boolean position) {
params.gravity = position ? Gravity.RIGHT : Gravity.LEFT;
return params;
}