RecyclerView不显示最后一项,但正在调用onBindView

时间:2017-01-19 03:08:56

标签: android android-recyclerview

我正在尝试使用 RecyclerView 的项目列表,一切正常但最后一项没有显示!列表的长度为 2 onBindView称为两次,当我使用Log打印项目时,它打印最后一项。所以列表很好,但RecyclerView没有显示它。

这是我的代码RecyclerView Adapter:

public class ModuleList  extends RecyclerView.Adapter<ModuleList.ViewHolder> {

private List<Module> moduleList;

public ModuleList(List<Module> moduleList){
    this.moduleList = moduleList;

}

public ModuleList(){
this.moduleList = new ArrayList<>();
}

public List<Module> getModuleList() {
    return moduleList;
}

public void setModuleList(List<Module> moduleList) {
    this.moduleList = moduleList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_modules,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
          Module module = moduleList.get(position);
         // function is called twice and the all items are logged works fine
          Log.d("ALL",module.getModuleName());
          Log.d("ALL", String.valueOf(module.getNumberOfLevels()));

          holder.moduleName.setText(String.valueOf(module.getModuleName()));
          holder.numberOfLevels.setText(String.valueOf(module.getNumberOfLevels()));


}

@Override
public int getItemCount() {
    //size is 2
    return moduleList.size();

}

public class ViewHolder extends RecyclerView.ViewHolder{
      public TextView moduleName;
      public TextView numberOfLevels;


    public ViewHolder(View itemView) {
        super(itemView);
        moduleName = (TextView) itemView.findViewById(R.id.list_module_name);
        numberOfLevels = (TextView) itemView.findViewById(R.id.list_no_of_levels);
    }
  }
 }

这是我的xml,其中的是recyclerview:

     <?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="match_parent">


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

            />

    </LinearLayout>

这是我的xml,其中显示了RecyclerView Fragment:

   <?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"

     android:id="@+id/main_screen"
     android:layout_height="match_parent"
     android:layout_width="match_parent"
    android:fitsSystemWindows="true"

    >





    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_screen_container"
        android:orientation="vertical"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/main_screen_toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/fragment_container">


        </FrameLayout>

    </LinearLayout>


    <ListView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:id="@+id/navigation_items"
        android:layout_gravity="start"
        android:background="@android:color/white"
        >

    </ListView>



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

编辑:的 list_module.xml

  <?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="match_parent">


     <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:id="@+id/list_module_name"
         />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_no_of_levels"

        />


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

嘿,你能尝试下面的解决方案吗?

list_module.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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/list_module_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/list_no_of_levels"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

我已将match_parent替换为wrap_content为父版式。