【问题标题】:How to use recyclerview with include layout properly?如何正确使用包含布局的recyclerview?
【发布时间】:2017-07-20 17:48:00
【问题描述】:

我想创建一个新闻提要列表,比如简单的 twitter。我用谷歌搜索了它,我建议使用recyclerview。但就我而言,我已经在我的主要活动布局中使用了包含视图。我尝试修改我的代码,但总是出错。这是我的代码。

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_bar_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);
    }

代码总是以 : FrameLayout layoutInclude = (FrameLayout)findViewById(R.id.layout_include); //当 setupRecyclerview()

的步骤时,应用程序总是在这里终止

如何解决这种情况? 如何正确使用包含布局的recyclerview?

如果我的问题仍然缺乏信息,我会提供更多信息。 请

【问题讨论】:

    标签: android android-recyclerview navigation-drawer


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-02-12
      • 1970-01-01
      • 2013-12-07
      • 2011-08-03
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多