【问题标题】:Xamarin.Android : ListView not scrolling correctly in Coordinator Layout and CollapsingToolbarLayoutXamarin.Android:ListView 在协调器布局和 CollapsingToolbarLayout 中无法正确滚动
【发布时间】:2017-11-13 10:02:06
【问题描述】:

大家早上好!

我的应用程序出现滚动问题。我有一个协调器布局和一个列表视图。当我在列表视图中滚动时,我希望顶部布局折叠。 我搜索并发现如果没有 NestedScrollView 这是不可能的,所以我添加了一个。

问题是当我滚动时,只有协调器布局在滚动。

例如,当我向下滚动时,列表视图会像这样卡住:

我还尝试将我的Listviewlayout_height 设置为match_parent,但这并没有改变任何东西。

这是我的代码:

ma​​in.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ActionBarNoShadowLight"
        android:fitsSystemWindows="true"
        app:elevation="0dp">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            android:fitsSystemWindows="true">
            <ImageView
                android:id="@+id/mainImage"
                android:layout_width="175dp"
                android:layout_height="175dp"
                android:layout_marginTop="50dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter"
                app:srcCompat="@drawable/ic_document"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />
            <android.support.v7.widget.Toolbar
                android:id="@+id/edit_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/BackgroundWhite"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:clipToPadding="false"
        android:scrollbars="none"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <ListView
                android:id="@+id/lstTask"
                android:layout_height="match_parent"
                android:layout_width="fill_parent"
                android:nestedScrollingEnabled="true"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:divider="@null"
                />
            <ImageView
                android:id="@+id/empty"
                android:layout_height="200dp"
                android:layout_width="200dp"
                android:layout_marginTop="50dp"
                android:scaleType="fitCenter"
                android:gravity="center"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                app:srcCompat="@drawable/bg_notasks" />
        </FrameLayout>
    </android.support.v4.widget.NestedScrollView>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_plus" />
</android.support.design.widget.CoordinatorLayout>

非常感谢您未来的帮助, 克莱门特。

【问题讨论】:

    标签: android listview xamarin android-coordinatorlayout android-collapsingtoolbarlayout


    【解决方案1】:

    创建一个 NonScrollListView 类,如下所示

    public class NonScrollListView extends ListView{
    
    public NonScrollListView(Context context) {
        super(context);
    }
    
    public NonScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    }
    

    并在您的 xml 添加布局行为

    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll_pro_profile_reviews"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
    <com.vanitee.services.home.customer.shops.detail.reviews.NonScrollExpandableListView
        android:id="@+id/rcv_pro_profile_reviews"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/spacing_8"/>
    
    </android.support.v4.widget.NestedScrollView>
    

    最后,在您初始化 listView 的代码中,您需要禁用嵌套滚动

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       listView.setNestedScrollingEnabled(false);
    } else {
       ViewCompat.setNestedScrollingEnabled(listView, false);
    }
    

    【讨论】:

    • 感谢您的回答,我会尝试将其转换为 C#,这对我来说有点复杂,因为我真的是 Java 新手
    【解决方案2】:

    只是帮助你将Ayush Khare的代码转换为C#,如果这对你有帮助,你可以标记 @Ayush Khare 的回答。

    public class NonScrollListView : ListView
    {
        public NonScrollListView(Context context) : base(context)
        {
        }
    
        public NonScrollListView(Context context, IAttributeSet attrs) : base(context, attrs)
        {
        }
    
        public NonScrollListView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
        {
        }
    
        protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int heightMeasureSpec_custom = MeasureSpec.MakeMeasureSpec(int.MaxValue >> 2, MeasureSpecMode.AtMost);
            base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
    
            ViewGroup.LayoutParams params2 = LayoutParameters;
            params2.Height = MeasuredHeight;
        }
    }
    

    禁用嵌套滚动:

    if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
    {
        listView.NestedScrollingEnabled = false;
    }
    else
    {
        ViewCompat.SetNestedScrollingEnabled(listView, false);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多