Set gravity programmatically for item in RecycerView Android

元气小坏坏 提交于 2019-12-06 04:42:29

问题


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;
    }

回答1:


I did that by setting the gravity on the view inside the item's layout.

LinearLayout.LayoutParams paramsMsg = new LinearLayout.
            LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    if (position%2 == 0){
        holder.bodyView.setBackgroundResource(R.drawable.outgoing_messages);
        paramsMsg.gravity = Gravity.END;
    } else {
        holder.bodyView.setBackgroundResource(R.drawable.incoming_messages);
        holder.bodyView.setTextColor(0xf08888);
        paramsMsg.gravity = Gravity.START;
    }
    holder.bodyView.setLayoutParams(paramsMsg);

bodyView is the TextView inside the item's Layout and the item width is "match_parent".




回答2:


Problem solved. Simply, I using

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

instead of

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.llGrandParent.getLayoutParams();

And then set gravity for LinearLayout Params normally.



来源:https://stackoverflow.com/questions/29426101/set-gravity-programmatically-for-item-in-recycerview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!