Kostas Drak
Kostas Drak

Reputation: 3260

Hide and Show the Floating Button While ListView Scrolls

I am creating a listView and a floating button in my application and I would like to have the effect of hiding and returning depending on the scroll state. When the ListView is being Scrolled the button hides fine, but when the scrolling stops the button does not return to its initial position.

Any ideas?

My Code:

public class MainActivity extends ActionBarActivity {
    private ImageButton btn;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12", "Item 13", "Item 14", "item 15"};
        lv = (ListView)findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "You have selected " + items[position].toString(), Toast.LENGTH_SHORT).show();

            }
        });
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                if(scrollState == SCROLL_STATE_TOUCH_SCROLL){
                    btn.animate().translationYBy(350);
                }else{
                    btn.animate().cancel();
                }

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        btn = (ImageButton)findViewById(R.id.add_button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Floating Button Pressed", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

Thanks in advance!!!

Upvotes: 3

Views: 10537

Answers (4)

Mstack
Mstack

Reputation: 321

@Override
    public void onScrollStateChanged(AbsListView view, int scrollState){
        int btnPosY = buttonFab.getScrollY();
        if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
            buttonFab.animate().cancel();
            buttonFab.animate().translationYBy(150);
        } else if (scrollState == SCROLL_STATE_FLING) {
            buttonFab.animate().cancel();
            buttonFab.animate().translationYBy(150);
        } else {
            buttonFab.animate().cancel();
            buttonFab.animate().translationY(btnPosY);
        }

    }

Upvotes: 2

Mohammad Tazehkar
Mohammad Tazehkar

Reputation: 271

for Android floating action button which reacts on scrolling events you can use this link :

https://github.com/makovkastar/FloatingActionButton

Upvotes: 6

rocklobster
rocklobster

Reputation: 126

This prevents the button to launch at short flinges:

public void onScrollStateChanged(AbsListView view, int scrollState) {
            int btn_initPosY=btn.getScrollY();
            if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                btn.animate().cancel();
                btn.animate().translationYBy(150);
            } else {
                btn.animate().cancel();
                btn.animate().translationY(btn_initPosY);
            }
        }

Upvotes: 11

user4346621
user4346621

Reputation:

This will do what you want

lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                if(scrollState == SCROLL_STATE_TOUCH_SCROLL){
                    btn.animate().cancel();
                    btn.animate().translationYBy(350);
                }else{
                    btn.animate().cancel();
                    btn.animate().translationYBy(-350);
                }

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        btn = (ImageButton)findViewById(R.id.add_button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Floating Button Pressed", Toast.LENGTH_SHORT).show();
            }
        });

Upvotes: 4

Related Questions