2

当我在屏幕底部显示 Snackbar 时,我需要将列表视图向上移动。

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

这是布局:

<?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>

4

1 回答 1

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. Snackbar当显示和0隐藏时,提升列表以减去偏移量。
于 2015-12-02T19:39:59.400 回答