代码之家  ›  专栏  ›  技术社区  ›  yozawiratama

如何正确使用recyclerview和include layout?

  •  0
  • yozawiratama  · 技术社区  · 9 年前

    我想创建一个新闻提要列表,比如简单的推特。我在谷歌上搜索了一下,建议使用recyclerview。但在我的例子中,我已经在我的主要活动布局中使用了include视图。我尝试修改代码,但总是出错。这是我的密码。

    activty_master.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
        <include
            android:id="@+id/layout_include"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/app_bar_master" />
    
        <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:menu="@menu/activity_master_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    

    app\u bar\u master.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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.bangun.halo.MasterActivity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
        <include
            android:id="@+id/layout_content"
            layout="@layout/content_master"
            />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email"
            android:visibility="gone"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    MasterActivity.java中的一些代码

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_master);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this,
                    drawer,
                    toolbar,
                    R.string.navigation_drawer_open,
                    R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
    
            FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
            if (currentUser == null) {
                startActivity(AuthActivity.createIntent(this));
                finish();
                return;
            }
            else{
                // TODO: 02/07/2017 check if data user dengan nomor telpon ini sudah ada atau belum, kalau belum didaftarkan ke data user
            }
    
            mIdpResponse = IdpResponse.fromResultIntent(getIntent());
            mSignedInConfig = getIntent().getParcelableExtra(EXTRA_SIGNED_IN_CONFIG);
    
    
    
    
            View headerLayout =
                    navigationView.inflateHeaderView(R.layout.nav_header_master);
            mUserDisplayName = ButterKnife.findById(headerLayout, R.id.tv_name);
            mUserPhoneNumber = ButterKnife.findById(headerLayout, R.id.tv_phone);
    
            populateProfile();
    
            handleInstanceState(savedInstanceState);
            setupFirebase();
            setupRecyclerview();
        }
    
    private void setupRecyclerview() {
            FrameLayout layoutInclude = (FrameLayout)findViewById(R.id.layout_include); //application always terminated here
            FrameLayout layoutContent = (FrameLayout)layoutInclude.findViewById(R.id.layout_content);
            RecyclerView recyclerView = (RecyclerView) layoutContent.findViewById(R.id.recyclerview);
            mNearbyAdapter = new NearbyAdapter(mQuery, mAdapterItems, mAdapterKeys);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setAdapter(mNearbyAdapter);
        }
    

    当步骤 setupRecyclerview()

    如何解决这个案件?

    如果我的问题仍然缺乏信息,我会给出更多。

    1 回复  |  直到 9 年前
        1
  •  1
  •   Pyrkosz    9 年前

    尝试使用协调器布局而不是框架

            CoordinatorLayout layoutInclude = (CoordinatorLayout)findViewById(R.id.layout_include); //application always terminated here
    
    推荐文章