gpt4 book ai didi

android - 滚动时如何修复滚动相关标题的闪烁?

转载 作者:行者123 更新时间:2023-11-30 02:03:02 26 4
gpt4 key购买 nike

我需要实现带有标题的 ListView,当用户向下滚动 ListView 时隐藏,当用户向上滚动时再次显示。但是我遇到了一个问题:当我缓慢向下滚动 ListView 时,标题正在闪烁。我知道这是因为 ListView 改变了大小。但是如何解决呢?

enter image description here

代码如下:

public class MainActivity extends ActionBarActivity implements AbsListView.OnScrollListener {

private static final int LIST_DATA_COUNT = 100;
private View mHeader;
private int mLastFirstVisibleItem;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mHeader = findViewById(R.id.textView);

String[] listData = new String[LIST_DATA_COUNT];
for (int i=0;i<LIST_DATA_COUNT;i++) {
listData[i]= String.valueOf(i);
}

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, listData));
listView.setOnScrollListener(this);
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem<mLastFirstVisibleItem) {
Log.d("tag", "up");
mHeader.setVisibility(View.VISIBLE);
} else if (firstVisibleItem>mLastFirstVisibleItem) {
Log.d("tag", "down");
mHeader.setVisibility(View.GONE);
}
mLastFirstVisibleItem = firstVisibleItem;
}
}

activity_main:

<RelativeLayout 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=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Header"
android:gravity="center"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textView"/>
</RelativeLayout>

日志:

D/tag? up
D/tag? down
D/tag? up
D/tag? down
D/tag? up
D/tag? down
D/tag? up
D/tag? down
D/tag? up
D/tag? down
D/tag? up
D/tag? down
D/tag? up
D/tag? down
D/tag? up
D/tag? down

最佳答案

尝试为 ListView 使用 TouchListener。这可能适合您。

 listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
startY = event.getY();//declare as global variable
return true;
} else if (action == MotionEvent.ACTION_MOVE) {
float y = event.getY();
if(startY-y<-60){
Log.w(TAG, "scroll down");
mHeader.setVisibility(View.VISIBLE);
}else if(startY-y>60){
Log.w(TAG, "scroll up");
mHeader.setVisibility(View.GONE);
}
}
return false;
}
});

关于android - 滚动时如何修复滚动相关标题的闪烁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31184790/

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