简体   繁体   中英

RecycleView works fine for the first time ,next time it shows nothing?

RecycleView works fine in the first time on my "home" Activity, but when i go to another activity (through the navigation menu) and comeback to "home" Activity the recycle view don't show anything?

the items in that my recycleview shows are a linearlayouts (user_row.xml), when i use log.ei can see that the data is there but the recyleview fails to list the items (in the second time)

  • The home activity:

package com.example.forum;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.navigation.NavigationView;

import java.util.HashMap;

public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    //Variables
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;

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

        //HOOKS OF NAVIGATION MENU
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        toolbar = findViewById(R.id.toolbar);


        //DATA AND RECYCLERVIEW
        recyclerView = findViewById(R.id.recycler_view);
        adapter = new  ForumAdapter(getApplicationContext());
        layoutManager = new LinearLayoutManager(this);

        //TOOL BAR
        setSupportActionBar(toolbar);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        //NAVIGATION MENU
        navigationView.bringToFront();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout , toolbar, R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);

    }


    @Override
    public void onBackPressed() {
        if(drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        }
        else{
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.nav_home:
                Intent intent33 = new Intent(Home.this,Home.class);
                startActivity(intent33);
                break;
            case R.id.nav_user:
                Intent intent = new Intent(Home.this,Myprofile.class);
                startActivity(intent);
                break;
            case R.id.nav_logout:
                SessionManager sessionManager = new SessionManager(Home.this);
                sessionManager.logout();
                Intent intent2 = new Intent(Home.this,Login.class);
                startActivity(intent2);
                break;
        }
        return true;
    }
}

  • The home xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    android:id="@+id/drawer_layout"
    tools:context=".Home"
    tools:ignore="ExtraText"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/header"
        app:menu="@menu/main_menu"
        android:layout_gravity="start"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="675dp"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.006"
                app:navigationIcon="@drawable/ic_action_menu"
                app:title="Home"
                app:titleMarginBottom="10dp"
                app:titleTextAppearance="@style/TextAppearance.AppCompat.Large"
                app:titleTextColor="#00BCD4" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="60dp"
                android:layout_marginLeft="60dp"
                android:layout_marginTop="24dp"
                android:layout_marginEnd="60dp"
                android:layout_marginRight="60dp"
                android:layout_marginBottom="24dp"
                android:fontFamily="@font/aldrich"
                android:text="@string/ensak_business_forum"
                android:textColor="#E4E4E4"
                android:textColorLink="#FFFFFF"
                android:textSize="24sp"
                app:layout_constraintBottom_toTopOf="@+id/linearLayoutxxx"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbar" />
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:translationY="130dp"
                android:id="@+id/recycler_view"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="175dp"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginRight="20dp"
                android:background="#00FFFFFF"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/recycler_view">

                <androidx.cardview.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    app:cardBackgroundColor="#00FFFFFF"
                    app:cardElevation="0dp">

                </androidx.cardview.widget.CardView>

                <androidx.cardview.widget.CardView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    app:cardBackgroundColor="#00FFFFFF"
                    app:cardCornerRadius="20dp"
                    app:cardElevation="0dp">

                </androidx.cardview.widget.CardView>
            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>

  • The view row (user_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayoutxxx"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:background="@drawable/rounding_relative_layouts"
    android:orientation="horizontal"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?android:attr/selectableItemBackground"
    android:padding="10dp">

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:id="@+id/firstcardxxx"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="1">

            <RelativeLayout
                android:layout_width="match_parent"
                android:id="@+id/imagelayoutxxx"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/imageView4xxx"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#FFFFFF" />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:id="@+id/secondcardxxx"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="1"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:id="@+id/infoslayoutxxx"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/textView4xxx"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:fontFamily="@font/aldrich"
                    android:textColor="#000000"
                    android:textSize="24sp" />

                <Button
                    android:id="@+id/button4xxx"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:layout_below="@id/textView4xxx"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/rounded_green_button"
                    android:fontFamily="@font/aldrich"
                    android:text="VIEW MORE"
                    android:clickable="true"
                    android:focusable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:textColor="#FFFFFF"
                    android:textColorHighlight="#FFFFFF"
                    android:textColorHint="#FFFFFF" />

                <Button
                    android:id="@+id/button5xxx"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:layout_below="@id/button4xxx"
                    android:layout_centerHorizontal="true"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginEnd="10dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:layout_marginRight="10dp"
                    android:layout_marginBottom="10dp"
                    android:background="@drawable/rounded_red_button"
                    android:fontFamily="@font/aldrich"
                    android:text="SUBSCRIBE"
                    android:textColor="#FFFFFF" />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>
    </LinearLayout>

This is happening because all of your logic is inside the onCreate() method of the Home activity. onCreate() is only invoked once when an activity is created.

Activities are kept on the activity stack, with the most recent activities being at the top of the stack.

In your case, the Home activity is placed into a stopped state while the other activity is active.

When you navigate back to the Home Activity, onCreate() is not called because it is already created. In this case onRestart() is called followed by onStart() .

My suggestion is to move the code for the RecyclerView in onCreate() to onStart() .

You should read about the Activity Lifecycle here .

And for future reference, there is also a Fragment Lifecycle which is slightly different. You can read about the Fragment Lifecycle here .

I found the response to my question, so i'll just share the answer in case any one faces the same problem, the problem i had is that the activity launches with no data in it, i thought the problem was from the recycler view or from the layout manager, but actually the problem was so simple i just have to wait for the data to download first,, if the activity life starts and the data hasn't been loaded yet it will show nothing. so i added a simple ----- WAIT() ----- function to dellay the data loading first then the data can be bined to recycler view and so on...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM