yozawiratama
yozawiratama

Reputation: 4328

How to use recyclerview with include layout properly?

I want create a list of news feed like simple twitter. I googled it and I suggested use recyclerview. but in my case i already used include view in my main activity layout. I try modify my code but always getting error. here this my codes.

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>

some of codes in 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);
    }

code always terminated in : FrameLayout layoutInclude = (FrameLayout)findViewById(R.id.layout_include); //application always terminated here when step of setupRecyclerview()

how to solve this case? How to use recyclerview with include layout properly?

if my question still lack of information, i will give more. please

Upvotes: 0

Views: 2046

Answers (1)

Pyrkosz
Pyrkosz

Reputation: 48

Try to use coordinator layout instead of frame

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

Upvotes: 1

Related Questions