我正在使用 RecyclerView Android 创建带有左/右消息框的聊天行。我想将重力设置为 RecyclerView 的项目。通常,我将 itemView 转换为 LinearLayout,然后为其设置 ParamLayout 引力。但是如果是RecyclerView,投给LinearLayout好像不太对。它将返回 RecyclerView.LayoutParams。因为:
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.
而且我找不到使用 RecyclerView.LayoutParams 设置重力的方法:| 不管怎样,有人能在 RecyclerView 中找到引力项的方法吗?请建议我。
------------后期解决方案----------------
//1。 RecyclerView 的 child
<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;
}
最佳答案
问题解决了。 简单地说,我使用
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
代替
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.llGrandParent.getLayoutParams();
然后正常设置LinearLayout Params的重力。
关于android - 以编程方式为 RecyclerView Android 中的项目设置重力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426101/