RecyclerView没有显示任何东西,虽然看起来没有任何问题

RecyclerView没有显示任何东西,虽然看起来没有任何问题

问题描述:

我在这个时候有点舒服。为了节省时间,我使用了Android Studio的导航抽屉活动模板。我打算创建一个导航抽屉,并通过其余的活动。第一项活动确实进展顺利。问题在于第二个问题,因为我想要导航抽屉并创建一个RecyclerView。 所以我的代码是:RecyclerView没有显示任何东西,虽然看起来没有任何问题

Content_main2.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.sparrowred.sendcard.Main2Activity" 
    tools:showIn="@layout/activity_main2"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/imageRecycleView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical"> 
    </android.support.v7.widget.RecyclerView> 

</RelativeLayout> 

app_bar_main2.xml:

​​

nav_header_main2.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:src="@android:drawable/sym_def_app_icon" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="Android Studio" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]" /> 

</LinearLayout> 

activity_main2.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 
     layout="@layout/app_bar_main2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <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:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

image_list_row:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:focusable="true" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" 
    android:clickable="true" 
    android:background="?android:attr/selectableItemBackground"> 


    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="8dp" 
     android:contentDescription="@string/image_descrprtion" 
     android:layout_gravity="center_horizontal" 
     android:id="@+id/imageId" 
     android:src="@drawable/profile"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/title" 
     android:textStyle="bold" 
     android:padding="8dp" 
     android:layout_gravity="center_horizontal" 
     android:text="dummy"/> 

</LinearLayout> 

imageAdapter.java:

package com.sparrowred.sendcard; 

public class imageAdapter { 
    private String title; 
    private int imageId; 

    public imageAdapter() { 
    } 

    public imageAdapter(String title, int imageId) { 
     this.title = title; 
     this.imageId = imageId; 
    } 

    public void setTitle(String title){ 
     this.title = title; 
    } 

    public String getTitle(){ 
     return this.title; 
    } 

    public void setImageId(int imageId){ 
     this.imageId = imageId; 
    } 

    public int getImageId(){ 
     return this.imageId; 
    } 
} 

imageDescriptionArray.java(我需要更改名称:P):

package com.sparrowred.sendcard; 

import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 

import java.util.List; 

public class imageDescriptionArray extends RecyclerView.Adapter<imageDescriptionArray.MyViewHolder> { 

    private LayoutInflater layoutInflater; 
    private List<imageAdapter> imageAdapterList; 

    public imageDescriptionArray(List<imageAdapter> imageAdapterList) { 
     this.imageAdapterList = imageAdapterList; 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.image_list_row, parent, false); 

     return new MyViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     imageAdapter iva = imageAdapterList.get(position); 
     holder.title.setText(iva.getTitle()); 
     holder.image.setImageResource(iva.getImageId()); 
    } 

    @Override 
    public int getItemCount() { 
     return imageAdapterList.size(); 
    } 

    public class MyViewHolder extends RecyclerView.ViewHolder { 
     public TextView title; 
     public ImageView image; 

     public MyViewHolder(View view) { 
      super(view); 
      title = (TextView) view.findViewById(R.id.title); 
      image = (ImageView) view.findViewById(R.id.imageId); 
     } 
    } 

} 

终于Main2Activity.java :

package com.sparrowred.sendcard; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.widget.DefaultItemAnimator; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.ViewGroup; 

import java.util.ArrayList; 
import java.util.List; 

import jp.wasabeef.recyclerview.animators.SlideInUpAnimator; 

public class Main2Activity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 


    private RecyclerView mImageRecycleView; 
    private List<imageAdapter> imageAdapterList; 
    private imageDescriptionArray mAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 
     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); 

     mImageRecycleView = (RecyclerView) findViewById(R.id.imageRecycleView); 
     mImageRecycleView.setHasFixedSize(true); 
     imageAdapterList = new ArrayList<>(12); 
     mAdapter = new imageDescriptionArray(imageAdapterList); 
     LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); 
     mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     mImageRecycleView.setLayoutManager(mLayoutManager); 
     mImageRecycleView.setItemAnimator(new SlideInUpAnimator()); 
     mImageRecycleView.setAdapter(mAdapter); 
     prepareData(); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main2, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    private void prepareData(){ 
     List<imageAdapter> data = new ArrayList<>(); 
     int[] images = {R.drawable.birthday_image_1,R.drawable.birthday_image_2,R.drawable.birthday_image_3,R.drawable.birthday_image_4, 
       R.drawable.birthday_image_5,R.drawable.birthday_image_6,R.drawable.birthday_i mage_7,R.drawable.birthday_image_8, 
       R.drawable.birthday_image_9,R.drawable.birthday_image_10,R.drawable.birthday_ image_11,R.drawable.birthday_image_12}; 
     String[] description = {"ballons","a lot of cakes","ballons with cakes","presents","presents with hat","cup-cake", 
       "retro","perspective","flying presents","empty card","giving a present","tasty cake"}; 
     for(int i = 0; i<images.length && i<description.length; i++){ 
      imageAdapter current = new imageAdapter(); 
      current.setTitle(description[i]); 
      current.setImageId(images[i]); 
      data.add(current); 
     } 
     mAdapter.notifyDataSetChanged(); 
    } 
} 

这是我的第二个activity.It代码没有完全实现,但肯定recyclerView不起作用。任何人都可以帮助我解决这个问题。我相信这个问题在我的xmls中......但我找不到任何东西。 PS。我的代码,编译时显示没有错误....

请尝试下面的代码。用我的替换您的prepareData()

private void prepareData(){ 

    int[] images = {R.drawable.birthday_image_1,R.drawable.birthday_image_2,R.drawable.birthday_image_3,R.drawable.birthday_image_4, 
      R.drawable.birthday_image_5,R.drawable.birthday_image_6,R.drawable.birthday_i mage_7,R.drawable.birthday_image_8, 
      R.drawable.birthday_image_9,R.drawable.birthday_image_10,R.drawable.birthday_ image_11,R.drawable.birthday_image_12}; 
    String[] description = {"ballons","a lot of cakes","ballons with cakes","presents","presents with hat","cup-cake", 
      "retro","perspective","flying presents","empty card","giving a present","tasty cake"}; 
    for(int i = 0; i<images.length && i<description.length; i++){ 
     imageAdapter current = new imageAdapter(); 
     current.setTitle(description[i]); 
     current.setImageId(images[i]); 
     imageAdapterList.add(current); 
    } 
    mAdapter.notifyDataSetChanged(); 
} 
+0

我不工作。此外,我刚看到我得到了“I /编舞者:跳过了46帧!应用程序可能在其主线程中做了太多工作”消息... 我注意到的唯一事情是,当我开始新的活动,右上角有东西(像一个微小的灰色矩形),它在一秒钟后消失。 –

+0

然后,你可能有问题,以及让我检查它xml –

+0

在onBindViewHolder放置一个断点,并确保你得到的调用。 –