Android ListView onScroll多次触发

时间:2015-05-17 15:14:00

标签: android listview scroll android-linearlayout onscroll

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

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

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

但是onScroll方法存在问题,即当我滚动列表视图时,当我滚动列表视图一次时它会多次触发。因此线性布局的扩展和折叠过程变得疯狂。

这是 - 屏幕上发生了什么。 https://www.youtube.com/watch?v=U7KNwS6JlUk

我做错了什么? 处理listview的向上/向下事件以及折叠/展开线性布局的最佳方法是什么?

我的布局

    <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;
                    }
                }

        });

2 个答案:

答案 0 :(得分:1)

我认为使用Android SDK的经典ListView无法实现解决方案。

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

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

答案 1 :(得分:0)

尝试将booolean值设置为global。

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;

}
相关问题