2017-01-19 49 views
1

我在尝试使用RecyclerView的项目列表,一切正常,但最后一项没有显示!列表的长度是和onBindView被称为两次和当我使用日志打印项目它打印最后一个项目。所以列表很好,但RecyclerView没有显示它。RecyclerView不显示最后一个项目,但正在调用BindView

这是我的代码RecyclerView适配器:

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片段:

<?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> 
+0

你是不是模拟器上运行 –

+0

在我的手机,我的意思是,你可指定S,我可以帮你 – Jois

+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_parentwrap_content父布局。其中设备

+0

不,我发现了这种营养!我所做的只是从drawerLayout中移除这个'android:fitsSystemWindows =“true”' – Jois

相关问题