Answer the question
In order to leave comments, you need to log in
How to fix fixed title twitching when scrolling through a list?
Task: we need to make a fixed header over the ListView, which will hide when scrolling down and return when scrolling up.
Problem: When scrolling down slowly, the title starts blinking. It is clear that this is due to the fact that the list changes in height when the title is removed and the scroll listener is called with different positions. But how to get around it?
It looks like this:
Source code in the archive: sderni.ru/280526
Activity:
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;
}
}
<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
Answer the question
In order to leave comments, you need to log in
Did it like this:
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();
} else if (action == MotionEvent.ACTION_MOVE) {
float y = event.getY();
if(startY-y<-60){
mHeader.setVisibility(View.VISIBLE);
}else if(startY-y>60){
mHeader.setVisibility(View.GONE);
}
}
return false;
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question