在滚动列表视图的末尾如何使按钮消失?

时间:2016-03-02 14:31:30

标签: android listview button

我想将FAB用于项目列表视图以添加项目。虽然我想将发布/隐藏动画设置为滚动。 我是这样的:

MainActivity.java类:

public class MainActivity extends Activity  {
private ListView listView;

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(),AddActivity.class);
            startActivity(intent);
        }
    });
    listView = (ListView) findViewById(R.id.ListView);

 ..  
}

}

activity_main.xml中:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="vow_note.maxsoft.com.vownote.MainActivity">
<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_menu_add" />

content_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal"
    android:gravity="center_vertical|center"
    android:padding="3dp"
    android:background="#000000"
    android:id="@+id/ll">
   ...
</LinearLayout>

<ListView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ListView"
    android:layout_gravity="right"
    android:background="#000000"
    android:paddingBottom="50dp"
    >
</ListView>

最后,FABHideScroll.java:

public class FABHideScroll extends FloatingActionButton.Behavior {
public FABHideScroll(Context context, AttributeSet attrs) {
    super();
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

    //child -> Floating Action Button
    if (child.getVisibility() == View.VISIBLE && dyConsumed > 0) {
        child.hide();
    } else if (child.getVisibility() == View.GONE && dyConsumed < 0) {
        child.show();
    }
}

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}

}

我知道它们应该有问题,因为它没有运行。

我该如何解决?非常感谢!

2 个答案:

答案 0 :(得分:1)

您必须使用FloatingActionButton并在滚动或需要时隐藏它。在这里,当我尝试实现此行为时,我遵循本教程http://www.sitepoint.com/animating-android-floating-action-button/ https://guides.codepath.com/android/Floating-Action-Buttons 希望能帮到你。

答案 1 :(得分:1)

请检查您的应用主题。它必须是 interface can't be defined in an inner class http://www.xyzws.com/javafaq/why-an-interface-cant-be-defined-in-an-inner-class/56 public class Test{ interface stuff { public LinkedList.Node getNextNode(); } public class LinkedList<T> { Node head; public LinkedList() { head = new Node(); } private class Node { } } 。 祝福。