9

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.

Community
  • 1
  • 1
Mohit
  • 1,045
  • 4
  • 18
  • 45

2 Answers2

6

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.

Bracadabra
  • 3,609
  • 3
  • 26
  • 46
  • Flickering is removed. But there is one problem, the action bar is now kinda invisible, so when I scroll to the bottom and try to scroll up, the action bar unhides and since it is invisible, the contents are shown inside it. – Mohit Dec 23 '14 at 08:27
  • You can make it opaque with background color, for example. – Bracadabra Dec 23 '14 at 08:43
  • Yeah was just doing that, it works, thanks a lot. But please can you explain how cliptopadding and scrillbarstyle fixed the flickering? – Mohit Dec 23 '14 at 08:54
  • 1
    It's not, flickering was fixed by requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);. cliptopadding needs to prevent overlaping content by action bar. – Bracadabra Dec 23 '14 at 09:02
0

Sorry for late reply.

I have faced same problem. But i solved this problem this way:

You should declared your toolbar below of your RecyclerView or ListView and put this line in your RecyclerView or ListView:

          "android:paddingTop="?attr/actionBarSize"

          android:clipToPadding="false"

         android:scrollbarStyle="outsideOverlay""