Android ListView onScroll 触发多次

Android ListView onScroll Fires Multiple Times

我的片段中有两个视图。在第一个视图中,我有一个线性布局,其中包含一些控件,在第二个视图中,我有一个列表视图。

我希望在向上/向下滚动列表视图时折叠/展开第一个视图中的线性布局。

我尝试在 OnScrollListener 中处理列表视图的向上/向下滚动事件并折叠/展开列表视图。

但是onScroll方法有一个问题,就是当我滚动listview的时候,滚动一次listview会触发很多次。所以线性布局的扩展和折叠过程是疯狂的。

这是 - 屏幕上正在发生的事情。 https://www.youtube.com/watch?v=U7KNwS6JlUk

我做错了什么? 处理 listview 的 scrol up/down 事件和折叠/展开线性布局的最佳方法是什么?

我的布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.training.mehmetyilmaz.mywallet.TransactionsActivity.TransactionsFragment"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/transactions_filter_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:orientation="vertical"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="@color/blue">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/account_type_label"
                    android:textSize="@dimen/transactions_filter_text"
                    android:textColor="@color/white"
                    />


                <RadioGroup
                    android:id="@+id/transactions_account_type_radio_group"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"

                    >

                    <RadioButton
                        android:id="@+id/transactions_rd_cash"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"
                        android:text="@string/cash"
                        android:checked="true"
                        android:textSize="@dimen/transactions_filter_text"
                        android:textColor="@color/white"
                        android:buttonTint="@color/white" />

                    <RadioButton
                        android:id="@+id/transactions_rd_bank"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"
                        android:text="@string/bank"
                        android:textSize="@dimen/transactions_filter_text"
                        android:textColor="@color/white"
                        android:buttonTint="@color/white"
                        />

                </RadioGroup>

            </LinearLayout><!-- Money Add Type-->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/currency_label"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="@dimen/transactions_filter_text"
                    android:textColor="@color/white"
                    />

                <Spinner
                    android:id="@+id/transactions_spinner_currency"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:entries="@array/entry_currency"
                    android:layout_gravity="right"
                    android:gravity="right|end"
                    android:textSize="@dimen/transactions_filter_text"
                    android:textColor="@color/white"
                    style="@style/DropDownColor"
                    android:background="@drawable/abc_spinner_mtrl_am_alpha"
                    >

                </Spinner>
            </LinearLayout><!-- Currency -->

        </LinearLayout><!-- First Line-->

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/cyan_ligth"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            >
        </View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:layout_marginTop="5dp"
            >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/money_type_label"
                    android:textSize="@dimen/transactions_filter_text"
                    android:textColor="@color/white"
                    />


                <RadioGroup
                    android:id="@+id/transactions_money_type_radio_group"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    >

                    <RadioButton
                        android:id="@+id/transactions_rd_add"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"
                        android:text="@string/add"
                        android:textSize="@dimen/transactions_filter_text"
                        android:textColor="@color/white"
                        android:buttonTint="@color/white" />

                    <RadioButton
                        android:id="@+id/transactions_rd_sub"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"
                        android:text="@string/subtract"
                        android:textSize="@dimen/transactions_filter_text"
                        android:textColor="@color/white"
                        android:buttonTint="@color/white" />

                </RadioGroup>

            </LinearLayout><!-- Money Type-->

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/entry_date_label_all_dates"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="@dimen/transactions_filter_text"
                    android:textColor="@color/white"
                    />

                <Spinner
                    android:id="@+id/transactions_spinner_date"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:entries="@array/entry_date"

                    android:layout_gravity="right"
                    android:gravity="right|end"
                    android:textSize="@dimen/transactions_filter_text"
                    style="@style/DropDownColor"
                    android:background="@drawable/abc_spinner_mtrl_am_alpha">
                </Spinner>
            </LinearLayout><!-- Date -->


        </LinearLayout><!-- Second Line -->


    </LinearLayout><!-- Filter -->



<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.training.mehmetyilmaz.mywallet.ScrollDetectingListView
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:id="@+id/transactions_list_view"
        ></com.training.mehmetyilmaz.mywallet.ScrollDetectingListView>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/transactions_total_textView"
        android:text="@string/total"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:textSize="@dimen/abc_text_size_medium_material"
        android:background="@color/green"
        android:textColor="@color/white"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        />

 </RelativeLayout>


</LinearLayout>

OnScrollListener

mTransactionListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //Log.d("Scroll State", "State : " + scrollState);
            }

            private int mInitialScroll = 0;

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                Log.d("Scroll", "Called");

                    int scrolledOffset = mTransactionListView.getVerticalScrollOffset();

                    if (scrolledOffset != mInitialScroll) {

                        boolean scrollUp = (scrolledOffset - mInitialScroll) < 0;

                        if (scrollUp) {
                            Log.d("Scroll", "Up");
                            expand(mTransactionsFilterLinearLayout);

                        } else {
                            Log.d("Scroll", "Down");
                            collapse(mTransactionsFilterLinearLayout);

                        }

                        mInitialScroll = scrolledOffset;
                    }
                }

        });

尝试将这样的布尔值设置为全局值。

boolean flag = true;

然后

if (scrollUp) {

    if(flag == false){
        Log.d("Scroll", "Up");
        expand(mTransactionsFilterLinearLayout);
    }
    flag = true;

} else {

    if(flag == true){
        Log.d("Scroll", "Down");
        collapse(mTransactionsFilterLinearLayout);
    }
    flag = false;

}

我认为 Android SDK 的经典 ListView 无法解决这个问题。

我找到了一个名为Android-ObservableScrollView的自定义ListView,并在我的项目中实现了它,结果是成功的。

你可以从这里找到它 https://github.com/ksoichiro/Android-ObservableScrollView/