我有数据列表,数据大小= 7,但是当我尝试显示所有元素时,它只显示6并且在方法onBindViewHolder的调试模式中,最后一个绑定位置是5.我认为这是一些错误我的列表,所以我添加了第8个元素,但它只显示了6个元素。
在低于7.0的android上一切正常
适配器
public class BottomNavigationAdapter extends RecyclerView.Adapter<BottomNavigationAdapter.MyViewHolder> {
private ArrayList<BottomNavigationData> bottomNavigationDatas = new ArrayList<>();
private Context mContext;
public BottomNavigationAdapter(ArrayList<BottomNavigationData> bnl, Context context) {
bottomNavigationDatas = bnl;
mContext = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView txtview;
public MyViewHolder(View view) {
super(view);
imageView = (ImageView) view.findViewById(R.id.ivBottomItemIcon);
txtview = (TextView) view.findViewById(R.id.tvBottomTitle);
}
}
@Override
public BottomNavigationAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.bottom_navigation_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(BottomNavigationAdapter.MyViewHolder holder, int position) {
BottomNavigationData currentNavigationData = bottomNavigationDatas.get(position);
String image_url = Constants.SERVER_HOST + "/" + currentNavigationData.image;
Glide.with(mContext).load(image_url).into(holder.imageView);
holder.txtview.setText(currentNavigationData.name);
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return (bottomNavigationDatas != null) ? bottomNavigationDatas.size() : 0;
}
回收视图初始化
rvBottomNavigation = (RecyclerView) view.findViewById(R.id.rvBottomNavigation);
LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
rvBottomNavigation.setLayoutManager(horizontalLayoutManager);
bottomNavigationAdapter = new BottomNavigationAdapter(bottomNavigationDatas, getActivity().getApplicationContext());
rvBottomNavigation.setAdapter(bottomNavigationAdapter);
bottomNavigationAdapter.notifyDataSetChanged();
我在android 7.0+上的输出:
输出低于7.0:
所以你可以看到,我最后还有一个项目。代码完全一样。我该如何解决?
EDIT添加了适配器的完整代码
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_marginTop="7dp"
android:layout_marginLeft="@dimen/small_margin"
android:layout_marginRight="@dimen/small_margin"
>
<ImageView
android:id="@+id/ivBottomItemIcon"
android:layout_gravity="center_horizontal"
android:layout_width="30dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/tvBottomTitle"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
my_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.GiftsActivity">
<mfp.avdm.chudobox.custom.HeaderGridView
android:id="@+id/gvGoods"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/footer"
android:fitsSystemWindows="true"
android:horizontalSpacing="3dp"
android:numColumns="2"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="3dp" />
<LinearLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="@drawable/toolbare">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvBottomNavigation"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
代码似乎很好,但我确信这不是这样做的,
更新的部分
在你的xml中,你在水平滚动视图中添加了RecyclerView,这就是它给它带来麻烦的原因。将您的recyclerview带到水平滚动视图之外,因为您无论如何都要在代码中设置水平方向的LinearLayoutManager。然后将recyclerview的高度和宽度设置为match_parent。此外,如果您不想在页脚视图中添加任何其他内容,则可以删除linearLayout并将recyclerview设置为footer,其中width = match_parent和height = 60dp。
不要忘记删除horizontalscrollview代码。