Hide and Show the Floating Button While ListView Scrolls

99封情书 提交于 2020-01-01 11:58:09

问题


Hey guys i aam 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!!!


回答1:


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);
            }
        }



回答2:


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

https://github.com/makovkastar/FloatingActionButton




回答3:


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();
            }
        });



回答4:


@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);
        }

    }


来源:https://stackoverflow.com/questions/27513255/hide-and-show-the-floating-button-while-listview-scrolls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!