gpt4 book ai didi

Android ListView onScroll 触发多次

转载 作者:行者123 更新时间:2023-11-29 01:32:48 25 4
gpt4 key购买 nike

我的 fragment 中有两个 View 。在第一个 View 中,我有一个线性布局,其中包含一些控件,在第二个 View 中,我有一个 ListView 。

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

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

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

});

最佳答案

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

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

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

关于Android ListView onScroll 触发多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com