Mohit Mohit - 3 months ago 21
Android Question

Remove flickering on actionbar hiding/unhiding

My app has a listview and I want to hide the actionbar when I scroll down and unhide the actionbar when I scroll up.The problem is not the hiding/unhiding of the action bar but the flickering that is happening due to this.

I googled a lot and the closet thing to a solution I found is this: StackOverflow Question

According to the solution given: I have to add a

paddingTop
of listview of height equal to actionbar's height, then add a header.

So, I set the padding at the top of the listview with height of
"?android:attr/actionBarSize"
but I am stuck at what to do next. What will be the content of the header.xml file.

My code:-

MyAdapter ma = new MyAdapter();
ListView lv = (ListView)findViewById(R.id.listView);
lv.setAdapter(ma);

ma.notifyDataSetChanged();

//setting onScrollListener on the listview
lv.setOnScrollListener(new OnScrollListener(){
private int mLast;
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
// TODO Auto-generated method stub

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
if(mLast<firstVisibleItem)
{
if(myactionbar.isShowing())
{
myactionbar.hide();
}
}
if(mLast>firstVisibleItem)
{
if(!myactionbar.isShowing())
{
myactionbar.show();
}
}
mLast=firstVisibleItem;
}

});


listview.xml:-

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:paddingTop="?android:attr/actionBarSize"

/>


onCreate():-

requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);


All this is doing is adding a permanent padding on top of listview so how will adding a header solve my flickering problem.

Or is there any other way to solve this problem?

Thanks.

Answer

I don't know why you need header to get rid from flickering. The idea is that when you add

 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

listview is drawn underneath the ActionBar, thats why its layout doesn't change when actionbar hiding. To prevent content from overlapping by ActionBar when screen is opened you can add clipToPadding attribute:

<ListView
    …
    android:paddingTop="?android:attr/actionBarSize"
    android:clipToPadding="false"
    android:scrollbarStyle="outsideOverlay" />

EDITED: I get it, you need header to simulate top padding.