显示Snackbar时向上移动Listview

时间:2015-11-11 19:50:03

标签: android android-listview android-support-library android-coordinatorlayout android-snackbar

当我在屏幕底部显示Snackbar时,我需要移动listview。

小吃店运作完美,但隐藏了列表视图底部的内容。

这是布局:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<TextView
    android:id="@+id/lblInformacion"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/md_green_500"
    android:padding="3dip"
    android:textColor="@android:color/white"
    android:textStyle="bold" />

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/clClientes"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/lvClientes"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <TextView
            android:id="@+id/lblEmpty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="No Result"
            android:textSize="20sp"
            android:visibility="gone" />
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

class ListSnackbarBehavior extends AppBarLayout.ScrollingViewBehavior {

            @Override
            public boolean layoutDependsOn(CoordinatorLayout parent,
                                           View child, View dependency) {
                return super.layoutDependsOn(parent, child, dependency) && dependency instanceof Snackbar.SnackbarLayout;
            }

            @Override
            public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
                                                  View dependency) {
                if (dependency instanceof Snackbar.SnackbarLayout) {
                    updateListTranslationForSnackbar(parent, child, dependency);
                }
                return false;
            }

            private void updateListTranslationForSnackbar(CoordinatorLayout parent, View child, View dependency) {
                int translationY = (int) getListTranslationYForSnackbar(parent, child);
                if (translationY > -1) {
                    ViewCompat.animate(child).translationY(-translationY).setDuration(100)
                            .setInterpolator(new LinearOutSlowInInterpolator()).start();
                }
            }

            private float getListTranslationYForSnackbar(CoordinatorLayout parent,
                                                         View listView) {

                View lastVisibleView = listView instanceof RecyclerView ?
                        getLastViewFromRecyclerView(listView) : listView instanceof ListView ? getLastViewFromListView(listView) : null;
                float minOffset = 0;
                if (lastVisibleView != null) {
                    final List<View> dependencies = parent.getDependencies(lastVisibleView);
                    for (int i = 0, z = dependencies.size(); i < z; i++) {
                        final View view = dependencies.get(i);
                        if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(lastVisibleView, view)) {
                            minOffset = Math.min(minOffset,
                                    ViewCompat.getTranslationY(view) - view.getHeight());
                        }
                    }
                }
                return -1;
            }

            private View getLastViewFromListView(View view) {
                ListView listView = (ListView) view;
                if (listView.getAdapter() != null && listView.getAdapter().getCount() > 0) {
                    int lastVisiblePosition = listView.getLastVisiblePosition();
                    if (lastVisiblePosition == listView.getAdapter().getCount() - 1) {
                        listView.smoothScrollToPosition(lastVisiblePosition);
                        return listView.getChildAt(listView.getChildCount() - 1);
                    }
                }
                return null;
            }

            private View getLastViewFromRecyclerView(View listView) {
                RecyclerView recyclerView = (RecyclerView) listView;
                if (recyclerView.getAdapter() != null && recyclerView.getAdapter().getItemCount() > 0) {
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int lastPartiallyVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
                    if (lastPartiallyVisiblePosition == recyclerView.getAdapter().getItemCount() - 1) {
                        linearLayoutManager.scrollToPosition(lastPartiallyVisiblePosition);
                        return linearLayoutManager.getChildAt(lastPartiallyVisiblePosition);
                    }
                }
                return null;
            }
        }

我不确定我的翻译是否正确,但这是交易:

  1. 只有SnackbarLayout
  2. 发生变化时才会做出反应
  3. 查看我们是否接近列表中的最后一项
  4. 如果视图重叠,请滚动以使其完全可见。
  5. Snackbar显示时,将列表提升为负偏移量,隐藏时0