编程设置重力在RecycerView的Android项目 [英] Set gravity programmatically for item in RecycerView Android
问题描述
我使用的Android RecyclerView使左/右消息框,聊天线。我想重力设置为RecyclerView项目。在通常的方式,我投给ItemView控件和的LinearLayout然后为它ParamLayout重力。但是,如果RecyclerView,似乎不对投给的LinearLayout。它将返回RecyclerView.LayoutParams。因为:
的LayoutParams子类RecyclerView的孩子。自定义布局
鼓励管理人员创造自己的这个子类
的LayoutParams类来存储任何额外要求每个子视图
元数据的布局。
块引用>和我无法找到设置重力与RecyclerView.LayoutParams方式:|
总之,可能有人发现RecyclerView方式重力项目?请给我建议。----------- POST解决方案------------------结果
// 1。 RecyclerView儿童<的LinearLayout
机器人:ID =@ + ID / id_grandparent
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@机器人:彩色/透明
机器人:分=@机器人:彩色/透明
机器人:重力=左
机器人:方向=垂直>
<的LinearLayout
机器人:ID =@ + ID / id_llparent
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ bg_customer_feedback_left
机器人:重力=左
机器人:方向=垂直>
< / LinearLayout中>
// 2。 OnBindView()
LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
PARAMS = setLayoutParamsForParent(参数,可以item.isPosition());
holder.llParentLayout.setLayoutParams(PARAMS); //对应id_llparent ID// 3。
私人LinearLayout.LayoutParams setLayoutParamsForParent(LinearLayout.LayoutParams参数,可以布尔位置){
params.gravity =持何立场? Gravity.RIGHT:Gravity.LEFT;
返回PARAMS;
}
解决方案问题解决了。
简单地说,我用LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
而不是
RecyclerView.LayoutParams PARAMS =(RecyclerView.LayoutParams)holder.llGrandParent.getLayoutParams();
然后设置重力的LinearLayout PARAMS正常。
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; }
解决方案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.
这篇关于编程设置重力在RecycerView的Android项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!