Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这是recycleView android 7.0+;的正确工作吗;?_Android_Android Recyclerview - Fatal编程技术网

这是recycleView android 7.0+;的正确工作吗;?

这是recycleView android 7.0+;的正确工作吗;?,android,android-recyclerview,Android,Android Recyclerview,我有一个数据列表,数据的大小=7,但是当我试图显示所有元素时,它只显示6,并且在方法onBindViewHolder的调试模式下,最后一个绑定位置是5。我认为这是我的列表中的一些错误,所以我添加了第8个元素,但它仍然只显示6个元素 在低于7.0的android上,一切正常 适配器 public class BottomNavigationAdapter extends RecyclerView.Adapter<BottomNavigationAdapter.MyViewHolder>

我有一个数据列表,数据的大小=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;
}
我在android 7.0+上的输出:

低于7.0时的输出:

如你所见,我最后还有一项。 代码完全相同。我怎样才能修好它

编辑 添加了适配器的完整代码

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

my_fragment.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>
<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>

代码看起来不错,但我确信这不是解决问题的方法

  • 您应该在活动本身中对arrayList进行null检查,然后继续设置适配器

  • 对于适配器,您应该提供activityContext而不是applicationContext,适配器通常容纳侦听器以打开活动或显示祝酒词,在这种情况下,它应该是activityContext而不是applicationContext

  • 对于您的问题,我建议在设置适配器之后设置布局管理器。这样,您就不需要调用notifyDatasetChang

  • 更新部分

    在xml中,您正在水平滚动视图中添加RecyclerView,这就是它出现问题的原因。将您的recyclerview置于水平滚动视图之外,因为您正在代码中设置水平方向的LinearLayoutManager。然后将recyclerview的高度和宽度设置为与父项匹配。此外,如果不想在页脚视图中添加任何其他内容,可以删除linearLayout,并将recyclerview设置为页脚,宽度=match_parent,高度=60dp


    别忘了删除horizontalscrollview代码。

    @Override public int getItemCount(){return bottomNavigationDatas.size();}@vishAdave我的代码也一样,我只是添加了检查列表是否为空实际上你的代码看起来很完美。这就是我提出这个建议的原因。甚至您也可以尝试使用而不是getActivity编写类。此或context@VishvaDave我原以为问题出在我的设备上,但后来我在emulator上运行我的应用程序,问题在7.0和8.0上重复出现too@Toper你能为你的适配器添加一个完整的代码吗?这是一个很好的建议。我按照你的建议修改了我的代码,但我仍然有同样的问题在你的代码中,你不应该初始化任何你要从构造函数计算的对象。不要初始化arrayList,即不要调用arrayList arrayList=new arrayList。只需使用ArrayList ArrayList全局声明它,并让您的构造函数初始化ArrayList。哦,我错过了这个错误,谢谢,但它不会像您所要求的那样帮助共享XML