我正在尝试使用MvxRecyclerView。我直接从ViewModel绑定了ItemSource并将其列入了列表。但我希望ListItemDivider介于两者之间。所以我制作了一个自定义的ItemDecorator并将其与循环器视图一起使用。它适用于andorid 7.0,但不适用于棉花糖。我没有为MvxRecyclerView分别使用任何Holder,LinearLayoutManager或Adapter。请在下面找到代码
活动类 // ********* **************************** //
[Activity(Label = "EmployeeHub", Theme = "@style/MaterialTheme", ScreenOrientation = AndroidSystem.Content.PM.ScreenOrientation.Portrait)]
public class EmployeeHub : BaseView
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.EmployeeHub);
var reclycleView = this.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
AndroidSystem.Support.V7.Widget.RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecoration(this.ApplicationContext);
reclycleView.AddItemDecoration(dividerItemDecoration);
}
}
ItemDecorator // ********* ******************** //
public class DividerItemDecoration : RecyclerView.ItemDecoration
{
private AndroidSystem.Graphics.Drawables.Drawable divider;
private int[] attributes = new int[] {Attribute.ListDivider };
public DividerItemDecoration(Context context)
{
divider = context.GetDrawable(Resource.Drawable.noBorder);
TypedArray ta = context.ObtainStyledAttributes(attributes);
divider = ta.GetDrawable(0);
ta.Recycle();
}
public override void GetItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
{
if (parent.GetChildAdapterPosition(view) == 0)
{
return;
}
outRect.Top = divider.IntrinsicHeight;
}
public override void OnDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
{
int left = parent.PaddingLeft;
int right = parent.Width - parent.PaddingRight;
for (int i = 0; i < parent.ChildCount; i++)
{
View child = parent.GetChildAt(i);
var parameters = child.LayoutParameters.JavaCast<RecyclerView.LayoutParams>();
int top = child.Bottom + parameters.BottomMargin;
int bottom = top + divider.IntrinsicHeight;
divider.SetBounds(left, top, right, bottom);
divider.Draw(c);
}
}
}
//布局******************************************** ** //
<MvxRecyclerView
android:id="@+id/my_recycler_view"
android:layout_below="@+id/hlhrview1"
android:scrollbars="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxItemTemplate="@layout/employee_item_template"
local:MvxBind="ItemsSource Employees; ItemClick SelectedEmployee" />
答案 0 :(得分:0)
我也遇到了问题。通过here的提示和对代码的一些修改,我能够使其正常运行。具体来说,我使用 OnDrawOver 而不是 OnDraw
这是我修改过的DividerItemDecoration:
public class DividerItemDecoration : RecyclerView.ItemDecoration
{
private readonly Drawable _divider;
public DividerItemDecoration(Context context)
{
_divider = ResourcesCompat.GetDrawable(context.Resources, Resource.Drawable.recyclerview_line_divider, context.Theme);
}
public override void OnDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
{
var left = parent.PaddingLeft;
var right = parent.Width - parent.PaddingRight;
for (var i = 0; i < parent.ChildCount; i++)
{
var child = parent.GetChildAt(i);
var parameters = child.LayoutParameters.JavaCast<RecyclerView.LayoutParams>();
var top = child.Bottom + parameters.BottomMargin;
var bottom = top + _divider.IntrinsicHeight;
_divider.SetBounds(left, top, right, bottom);
_divider.Draw(c);
}
}
}
我还将绑定代码放在我的OnCreateView中,如下所示:
var reclycleView = _payformView.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecoration(Application.Context);
reclycleView.AddItemDecoration(dividerItemDecoration);
所有这些都在 KitKat 设备上。