首页 > 解决方案 > Android视觉效果隐藏和显示项目

问题描述

您好,我正在尝试在我的活动中重现此操作,当我将列表向下移动时,它会动画地隐藏一些项目。

请注意,此示例中的搜索字段未累加。

任何人都知道我如何在下面的 GIF 中完成这项工作?

例子

感谢您的帮助解决方案已放入android: nestedScrollingEnabled = "true"ListView

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview1"
    android:nestedScrollingEnabled="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways" />


</android.support.design.widget.AppBarLayout>

<alexandreapp.com.br.pensouchegoucliente.CustomView
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_scrollFlags="scroll|enterAlways"
    android:layout_gravity="bottom"
    android:background="?colorAccent"
    android:gravity="center"
    android:text="My Custom View"
    android:textAppearance="?android:attr/textAppearanceLargeInverse" />

标签: androidandroid-layout

解决方案


为此,您可以使用 Coordinator 布局

例如。

主要活动

public class MainActivity extends AppCompatActivity {
private List<String> mList = new ArrayList<>();
private RecyclerView mRecyclerView;

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

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    prepareData();
    setUpRecyclerView();
}

private void prepareData() {
    Random random = new Random();
    for (int i = 0; i < 100; i++)
        mList.add(String.valueOf(random.nextInt(100)));
}

private void setUpRecyclerView() {
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager
        .VERTICAL, false));
    mRecyclerView.setHasFixedSize(true);
    ListAdapter listAdapter = new ListAdapter(mList);
    mRecyclerView.setAdapter(listAdapter);
}

}

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/color_primary"
        android:title="@string/app_name"
        app:layout_scrollFlags="scroll|enterAlways"
        app:titleTextColor="@android:color/white"/>

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>

基本上,将使您的视图隐藏和显示的行是:

app:layout_scrollFlags="scroll|enterAlways"应该在要隐藏/显示的视图中添加

app:layout_behavior="@string/appbar_scrolling_view_behavior"在您的列表或滚动视图中

在上面的示例中,这些行位于 Toolbar 和 RecyclerView 中,因此当您滚动 recyclerview 时,工具栏应该隐藏


推荐阅读