【问题标题】:RecycleView works fine for the first time ,next time it shows nothing?RecycleView 第一次工作正常,下次什么都没有?
【发布时间】:2021-05-14 15:00:54
【问题描述】:

RecycleView 第一次在我的“主页”Activity 上运行良好,但是当我转到另一个 Activity(通过导航菜单)并返回“主页”Activity 时,回收视图不显示任何内容?

我的 recycleview 显示的项目是线性布局 (user_row.xml),当我使用 log.e 时,我可以看到数据在那里,但 recyleview 无法列出项目(第二次)

  • 家庭活动:

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

  • 主页 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>

  • 视图行(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>

【问题讨论】:

    标签: java android android-studio android-layout android-recyclerview


    【解决方案1】:

    我找到了对我的问题的答复,所以我将分享答案,以防有人遇到同样的问题,我遇到的问题是活动启动时没有数据,我认为问题出在回收器视图或从布局管理器,但实际上问题是如此简单,我只需要先等待数据下载!,如果活动生命开始并且数据尚未加载但它不会显示任何内容,所以我添加了一个简单的 ----- WAIT() ----- 函数首先延迟数据加载,然后可以将数据绑定到回收器视图等...

    【讨论】:

      【解决方案2】:

      发生这种情况是因为您的所有逻辑都在 Home 活动的 onCreate() 方法中。 onCreate() 仅在创建活动时调用一次。

      Activity 保存在 Activity 堆栈中,最近的 Activity 位于堆栈顶部。

      在您的情况下,Home 活动处于停止状态,而其他活动处于活动状态。

      当您导航回 Home Activity 时,不会调用 onCreate(),因为它已经创建。在这种情况下,onRestart() 被调用,然后是 onStart()

      我的建议是将onCreate()RecyclerView 的代码移动到onStart()

      您应该阅读有关活动生命周期的信息here

      为了将来参考,还有一个片段生命周期略有不同。你可以阅读 Fragment Lifecycle here

      【讨论】:

      • 感谢@RStevoUK 的帮助,我检查并阅读了活动生命周期,我将代码移至 onStart (因为它是当用户再次导航到活动时)但它仍然没有显示视图,奇怪的是数据就在那里,当我调用回收器视图时,我会自动调用适配器(即使第二次我仍然会获取数据,但是 onCreateViewHolder 和 onBindViewHolder 在我第二次访问 Home 活动时它们都没有被调用)
      • 另外@RStevoUK 我这样做是为了检查活动当前状态,我创建了所有函数(onStart,onResum ...)然后我将日志消息放入每个函数中,例如:在 onStart 中,我放了一条日志消息,上面写着:我在 onStart...etc 中,然后我发现当我从菜单中退出家庭活动并返回它实际上应用程序是 onStop 然后它返回到 onCreate 这意味着“应用程序进程被杀死”,这意味着它不会去 onRestart 然后去 onStart .... 我想问题是我的架构不好:(
      • 是的,听起来很像。从查看您的代码来看,似乎没有问题,但是当我无法调试它时,这很困难。尝试按照教程进行操作,但仍使用您的数据。我仍然不明白为什么每次都不会填充数据。继续尝试,这就是编程的全部内容。这就像一个连续的循环,在一个问题上卡住几天,解决问题,再在另一个问题上卡住几天。
      • XD 确实是这样,我会继续阅读文档,非常感谢您的帮助
      猜你喜欢
      • 2011-08-29
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 2015-12-25
      • 2012-10-25
      相关资源
      最近更新 更多