You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Xamarin.Android中使用MVVM-Cross创建水平MvxListView

要实现MvxListView的水平滚动,其实有两种可行方案,我更推荐用MvxRecyclerView来做(性能更优,实现也更简单),当然也可以基于现有MvxListView调整,下面分别说清楚:

方案一:换成MvxRecyclerView(推荐)

因为原生ListView本身对水平滚动的支持很有限,而RecyclerView天生支持任意方向的布局,MvvmCross也提供了MvxRecyclerView的封装,步骤如下:

  1. 修改布局文件:把原来的MvxListView替换成MvxRecyclerView,布局代码可以改成这样:
<MvxRecyclerView 
    android:id="@+id/RelatedProductItems" 
    app:MvxItemTemplate="@layout/related_product_item" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    android:layout_margin="10dp" 
    android:nestedScrollingEnabled="true" 
    app:layout_constraintTop_toBottomOf="@id/CompleteExperienceLayout" />
  1. 在View层设置水平布局管理器:在你的Activity或者Fragment的代码里(比如OnViewCreated方法中),找到这个RecyclerView实例,给它设置水平方向的LinearLayoutManager:
var relatedProductsRV = View.FindViewById<MvxRecyclerView>(Resource.Id.RelatedProductItems);
// 设置水平布局,第三个参数false表示不反转列表
relatedProductsRV.LayoutManager = new LinearLayoutManager(Context, LinearLayoutManager.Horizontal, false);
// 可选:如果需要给item之间加间距,可以添加ItemDecoration
relatedProductsRV.AddItemDecoration(new DividerItemDecoration(Context, DividerItemDecoration.Horizontal));
  1. 调整Item布局:确保你的related_product_item.xml根布局的宽度不要设为match_parent(否则每个item会占满整个RecyclerView宽度,没法滚动),改成wrap_content或者固定宽度,比如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp"
    android:minWidth="100dp"> <!-- 可选:设置最小宽度避免item过窄 -->

    <!-- 这里放你的item内容,比如图片、文字 -->
    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="centerCrop"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"/>
</LinearLayout>

方案二:基于现有MvxListView实现(不推荐)

如果一定要用MvxListView,因为原生ListView不支持水平布局,你需要用HorizontalScrollView包裹它,同时调整ListView和Item的布局:

  1. 修改布局为HorizontalScrollView包裹MvxListView:
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_margin="10dp"
    app:layout_constraintTop_toBottomOf="@id/CompleteExperienceLayout"
    android:nestedScrollingEnabled="true">

    <MvxListView 
        android:id="@+id/RelatedProductItems" 
        app:MvxItemTemplate="@layout/related_product_item" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:scrollbars="none"/> <!-- 隐藏ListView自身的滚动条,用外层的HorizontalScrollView控制 -->

</HorizontalScrollView>
  1. 同样调整Item布局:和方案一一样,把item根布局的宽度设为wrap_content或者固定值,不能是match_parent。

这种方式的缺点是ListView的复用机制在水平方向下表现不如RecyclerView,而且如果列表项很多的话,性能会差一些,所以更推荐方案一。

内容的提问来源于stack exchange,提问作者Venkata Swamy Balaraju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:33:52