Android RecyclerView不显示项目

Android RecyclerView不显示项目,android,arraylist,android-recyclerview,Android,Arraylist,Android Recyclerview,我在Android中使用RecyclerView和详细信息的模型类。什么都没有显示。即使在log内部Adapter之后,Android监视器中也不会显示任何内容 MainActivity.class public ArrayList<Menu> menu_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt

我在
Android
中使用
RecyclerView
详细信息的
模型类
。什么都没有显示。即使在
log
内部
Adapter
之后,
Android监视器中也不会显示任何内容

MainActivity.class

public ArrayList<Menu> menu_list;

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

        initUi();
    }

    public void initUi(){
        RecyclerView menu_recycler = (RecyclerView) findViewById(R.id.main_menu_recycler_view);

        //set the layout manager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation( linearLayoutManager.VERTICAL );
        menu_recycler.setLayoutManager( linearLayoutManager );

        //set adapter
        MainMenuAdapter mainMenuAdapter = new MainMenuAdapter( getMenulist() );
        menu_recycler.setAdapter( mainMenuAdapter );
    }

    public ArrayList<Menu> getMenulist() {

        menu_list = new ArrayList<Menu>();

        menu_list.add( new Menu("About Us", " Learn more about our values, mission and vision ") );
        menu_list.add( new Menu("Services ", " In implementing its mandate, the Agency will provide the following to its external and internal customers:\n"));
        menu_list.add( new Menu("Stakeholders", " Who are the key players, Find out more") );
        menu_list.add( new Menu("Learn about Counterfeits", "Gain more insight on counterfeit products"));
        return menu_list;
    }
public class Menu {
    public String title, details;

    public Menu(String title, String details){
        this.details = details;
        this.title = title;
    }

}
和布局文件
主要活动

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.maos.aca.MainMenuActivity">

    <android.support.v7.widget.RecyclerView
            android:layout_width="334dp"
            android:layout_height="453dp" app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp" android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent" android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent" android:id="@+id/main_menu_recycler_view"
            android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>

最后一行

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:layout_width="wrap_content"
              android:layout_height="wrap_content">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/menu_title"
            android:textStyle="bold"
    />
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:id="@+id/menu_details"/>
</LinearLayout>


我知道以前有人问过这个问题,但没有一个答案对我有帮助。我会感谢任何线索。

您需要将
getItemCount()
方法更改为:

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

如果报告其大小为零,
RecyclerView
将不显示任何元素。

您需要将
getItemCount()
方法更改为:

@Override
public int getItemCount() {
    return list_of_menus.size();
}
如果报告其大小为零,
RecyclerView
将不显示任何元素。

更改此选项:

   @Override
   public int getItemCount() {
        return 0;
   }
为此:

   @Override
   public int getItemCount() {
       return list_of_menus.size();
   }
更改此项:

   @Override
   public int getItemCount() {
        return 0;
   }
为此:

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

您在GetItemCount上返回0在GetItemCount上返回0感谢@Sachin提供的解决方案和精彩的解释。感谢@Sachin提供的解决方案和精彩的解释。