本文介绍了隐藏和显示浮动按钮,滚动的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我AAM创造了我的应用程序列表视图和浮动按钮,我想有隐藏的效果,并根据不同的滚动状态回来了。当的ListView 正在滚动按钮隐藏很好,但是当滚动停止按钮不会返回到初始位置。

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.

任何想法?

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

}

在此先感谢!

推荐答案

这prevents按钮在短期flinges启动:

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

这篇关于隐藏和显示浮动按钮,滚动的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:53