Xamarin.Android:ListView无法在Coordinator Layout和CollapsingToolbarLayout中正确滚动

问题描述 投票:1回答:2

大家,早安!

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

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

举个例子,当我向下滚动时,listview就像那样:

enter image description here

我也尝试将我的layout_heightListview设置为match_parent,但它没有改变任何东西。

这是我的代码:

main.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>

非常感谢您将来的帮助,Clément。

android listview xamarin android-coordinatorlayout android-collapsingtoolbarlayout
2个回答
0
投票

创建一个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);
}

0
投票

只是帮助您将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);
}
© www.soinside.com 2019 - 2024. All rights reserved.