我需要实现带有标题的ListView,当用户向下滚动ListView时会隐藏它,并在用户向上滚动时再次显示。但是我遇到了一个问题:当我慢慢向下滚动ListView时,标题会闪烁。我知道这是因为ListView改变了大小。但是如何解决呢?
这是代码:
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>
logcat的:
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
答案 0 :(得分:1)
尝试将TouchListener
用于ListView
。这可能对你有用。
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;
}
});
答案 1 :(得分:0)
您可以将标题显示为叠加层。这将阻止ListView调整大小。对于第一个版本,只需尝试更改ListView和TextView顺序。
<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">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textView"/>
<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"/>
</RelativeLayout>