【发布时间】: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>
-
首页: The home page
-
菜单: The menu :
-
当我参加另一项活动时: when i go to another activity :
-
当我通过菜单返回时: when i come back through menu :
【问题讨论】:
标签: java android android-studio android-layout android-recyclerview