Android支持设计:BottomNavigationView [英] Android Support Design: BottomNavigationView

查看:63
本文介绍了Android支持设计:BottomNavigationView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下 https://material.google.com/components/bottom-navigation.html#bottom-navigation-behavior

但是回收者视图隐藏在工具栏下方,并且对BottomNavigationView没有影响

But recycler view is hiding below toolbar and no effect is on BottomNavigationView

下面是我的代码

activity_main.xml

activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

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


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.design.widget.BottomNavigationView
            android:id="@+id/nm_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimaryDark"
            android:foregroundTint="@color/colorAccent"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:menu="@menu/nav_menu" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/nm_bottom"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </RelativeLayout>

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

item.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="4dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="2dp">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="?android:attr/listPreferredItemHeight"
            android:padding="4dp">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignParentBottom="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="6dip"
                android:contentDescription="TODO"
                android:src="@drawable/ic_storage" />

            <TextView
                android:id="@+id/secondLine"
                android:layout_width="fill_parent"
                android:layout_height="26dip"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@id/icon"
                android:ellipsize="marquee"
                android:text="Description"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/firstLine"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_above="@id/secondLine"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_alignWithParentIfMissing="true"
                android:layout_toRightOf="@id/icon"
                android:gravity="center_vertical"
                android:text="Example application"
                android:textSize="16sp" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>




public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

            private static final Logger logger = Logger.getLogger(MainActivity.class.getSimpleName());

            private BottomNavigationView navigationView;
            private RecyclerView mRecyclerView;
            private MyAdapter mAdapter;

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

            private void initViews() {
                navigationView = (BottomNavigationView) findViewById(R.id.nm_bottom);
                navigationView.setOnNavigationItemSelectedListener(this);
                mRecyclerView = (RecyclerView) findViewById(R.id.rv);

                // use this setting to improve performance if you know that changes
                // in content do not change the layout size of the RecyclerView
                mRecyclerView.setHasFixedSize(true);

                // use a linear layout manager
                LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
                mRecyclerView.setLayoutManager(mLayoutManager);

                // specify an adapter (see also next example)
                List<String> myDataset = new ArrayList<>();
                for (int i = 0; i < 100; i++) {
                    myDataset.add("Index #" + i);
                }
                mAdapter = new MyAdapter(myDataset);
                mRecyclerView.setAdapter(mAdapter);
            }

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                return false;
            }
        }

        public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
            private List<String> mDataset;

            // Provide a reference to the views for each data item
            // Complex data items may need more than one view per item, and
            // you provide access to all the views for a data item in a view holder
            public class ViewHolder extends RecyclerView.ViewHolder {
                // each data item is just a string in this case
                public TextView txtHeader;
                public TextView txtFooter;

                public ViewHolder(View v) {
                    super(v);
                    txtHeader = (TextView) v.findViewById(R.id.firstLine);
                    txtFooter = (TextView) v.findViewById(R.id.secondLine);
                }
            }

            public void add(int position, String item) {
                mDataset.add(position, item);
                notifyItemInserted(position);
            }

            public void remove(String item) {
                int position = mDataset.indexOf(item);
                mDataset.remove(position);
                notifyItemRemoved(position);
            }

            // Provide a suitable constructor (depends on the kind of dataset)
            public MyAdapter(List<String> myDataset) {
                mDataset = myDataset;
            }

            // Create new views (invoked by the layout manager)
            @Override
            public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                           int viewType) {
                // create a new view
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view, parent, false);
                // set the view's size, margins, paddings and layout parameters
                ViewHolder vh = new ViewHolder(v);
                return vh;
            }

            // Replace the contents of a view (invoked by the layout manager)
            @Override
            public void onBindViewHolder(ViewHolder holder, int position) {
                // - get element from your dataset at this position
                // - replace the contents of the view with that element
                final String name = mDataset.get(position);
                holder.txtHeader.setText(mDataset.get(position));
                holder.txtHeader.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        remove(name);
                    }
                });

                holder.txtFooter.setText("Footer: " + mDataset.get(position));

            }

            // Return the size of your dataset (invoked by the layout manager)
            @Override
            public int getItemCount() {
                return mDataset.size();
            }

        }

推荐答案

这是一个简单的示例,显示了如何实现滚动行为 https://github.com/sjthn/BottomNavigationViewBehavior

Here is a simple sample showing how to implement scrolling behavior https://github.com/sjthn/BottomNavigationViewBehavior

将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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

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


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:title="@string/app_name" />
    </android.support.design.widget.AppBarLayout>

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

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/nm_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:foregroundTint="@color/colorAccent"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:layout_anchor="@+id/rv"
        app:layout_anchorGravity="bottom"
        app:menu="@menu/nav_menu" />

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

这篇关于Android支持设计:BottomNavigationView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆