将回收站视图放置在AppBarLayout下方时未显示内容

时间:2016-12-24 18:29:27

标签: java android

我正在制作一个应用程序,其中活动具有此布局(我从指南中获取)并且它非常完美,因为这是我想要的。但是因为我无法在其中显示数据 - 正如您可以看到放置在布局文件中的回收器视图。

这是布局

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

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

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        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>

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />

<android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/navheader"
    app:menu="@menu/menu_navigation" />

这是运行一切的活动:

    import android.support.design.widget.NavigationView;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.DefaultItemAnimator;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.view.MenuItem;
    import java.util.ArrayList;
    import java.util.List;

    public class basics_list extends AppCompatActivity {
        private DrawerLayout mDrawerLayout;
        private List<Basic> basicList = new ArrayList<>();
        private RecyclerView recyclerView;
        private BasicsAdapter mAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setTitle(R.string.basics_list_name);
            setContentView(R.layout.activity_basics_list);

            // Adding Toolbar to Main screen

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


            // set recycler view

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

            mAdapter = new BasicsAdapter(basicList);
            final RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setAdapter(mAdapter);

            prepareBasicsData();

            // Create Navigation drawer and inflate layout
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);

    // Adding menu icon to Toolbar
            ActionBar supportActionBar = getSupportActionBar();
            if (supportActionBar != null) {
                supportActionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
                supportActionBar.setDisplayHomeAsUpEnabled(true);
            }

    // Set behavior of Navigation drawer
            navigationView.setNavigationItemSelectedListener(
                    new NavigationView.OnNavigationItemSelectedListener() {
                        // This method will trigger on item Click of navigation menu
                        @Override
                        public boolean onNavigationItemSelected(MenuItem menuItem) {
                            // Set item in checked state
                            menuItem.setChecked(true);
                            // TODO: handle navigation
                            // Closing drawer on item click
                            mDrawerLayout.closeDrawers();
                            return true;
                        }
                    });


        }

        private void prepareBasicsData() {
            Basic basic = new Basic("one");
            basicList.add(basic);

            basic = new Basic("two");
            basicList.add(basic);

            basic = new Basic("three");
            basicList.add(basic);

            mAdapter.notifyDataSetChanged();
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试将layout_behavior添加到RecyclerView

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

有关在CoordinatorLayouts

中放置观看次数的详情,请参阅this问题