我在嘗試使用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>
你是不是模擬器上運行 –
在我的手機,我的意思是,你可指定S,我可以幫你 – Jois
這些 –