想要在列表视图中添加两个浮动按钮

时间:2014-12-04 11:10:39

标签: android google-plus imagebutton floating

我正在开发一个应用程序,其中我想添加两个浮动按钮,如 google +

为此我使用makovkastar/FloatingActionButton

但实际上它只适用于一个按钮,如果我添加另一个按钮,然后添加的按钮不起作用(这意味着当滚动列表视图时按钮没有消失)。

我尝试过以下代码,但没有效果。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         FloatingActionButton floatingActionButton1 = (FloatingActionButton) findViewById(R.id.button_floating_action1);

         FloatingActionButton floatingActionButton2 = (FloatingActionButton) findViewById(R.id.button_floating_action2);

            floatingActionButton1.attachToListView(getListView());
            floatingActionButton2.attachToListView(getListView());

            ListAdapter listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                    getResources().getStringArray(R.array.planets));
            getListView().setAdapter(listAdapter);
    }

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

查看库代码,

    public void attachToListView(@NonNull AbsListView listView, ScrollDirectionListener listener) {
        AbsListViewScrollDetectorImpl scrollDetector = new AbsListViewScrollDetectorImpl();
        scrollDetector.setListener(listener);
        scrollDetector.setListView(listView);
        scrollDetector.setScrollThreshold(mScrollThreshold);
        listView.setOnScrollListener(scrollDetector);
    }

似乎它将 setOnScrollListener 设置为listview,它只能有一个实例用于单个listview。

因此,在您挖掘和使用库项目之前,它可能无效。

另一种选择是使用弹出浮动操作按钮,就像在这个应用程序中一样。

https://play.google.com/store/apps/details?id=com.evernote.skitch

弹出窗口的弹跳效果你可以使用来自facebook的很棒的Rebound Lib。

http://facebook.github.io/rebound/

答案 1 :(得分:0)

你也可以试试这个,

  List<Widget> float_buttons() { 
  List button_list = List<Widget>();
  int i = 0;
  List names  = ['first', 'second', 'third', 'fourth', 'fifth'];
  List P_type = [1,0,1,1,0];
  for (i = 0; i < 5; i++) {
    button_list.add(
      FloatingActionButton.extended(onPressed: (){}, label: Text(names[i]), icon: P_type[i]==1?Icon(Icons.star):Icon(Icons.panorama),)
    );
    button_list.add(
      SizedBox(width: 10,)
    );
  }
  return button_list;
}

并在 Expanded 中使用此功能以获得更好的结果

Expanded(child: ListView(
            scrollDirection: Axis.horizontal,
            padding: const EdgeInsets.all(20.0),
            children: [Container(
              child: Row(
                children:  float_buttons(),
              ),
            )],
          ))

This is how it looks like