gpt4 book ai didi

java - 未调用 RecyclerView 重写方法并且 getItemCount 始终返回 0

转载 作者:行者123 更新时间:2023-11-29 18:39:40 25 4
gpt4 key购买 nike

我知道我的问题已被问过好几次,但我已经搜索了一段时间,但没有一个答案能解决我的问题。问题是,当我在我的“HomeFragment”中创建 recyclerview 适配器和方法“onViewCreated”中的所有内容时,我没有任何编译错误,但我的 recyclerview 没有加载任何图像,因为没有任何方法在 recyclerview 适配器中被覆盖即使在我调用“mAdapter.notifyDataSetChanged()”时也会被调用

首页 fragment .java

public class HomeFragment extends Fragment implements Response.Listener<GalleryItem[]>, Response.ErrorListener {

RequestQueue requestQueue;
List<GalleryItem> myDataset = new ArrayList<>();
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
CustomJsonObjectRequest jsonObjectRequest;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
mRecyclerView = view.findViewById(R.id.my_recycler_view);

mRecyclerView.setHasFixedSize(true);
mAdapter = new GalleryAdapter(myDataset);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(mAdapter);

jsonObjectRequest = new CustomJsonObjectRequest(Constants.HOME_URL, GalleryItem[].class, this, this);
requestQueue = VolleySingleton.getInstance(this.getContext()).getRequestQueue();
VolleySingleton.getInstance(this.getContext()).addToRequestQueue(jsonObjectRequest, "headerRequest");
}

@Override
public void onResponse(GalleryItem[] response) {

myDataset = Arrays.asList(response);
mAdapter.notifyDataSetChanged();
}

@Override
public void onErrorResponse(VolleyError error) {
Log.d(Constants.API_ERROR_TAG, error.getMessage());
}

画廊适配器.java

public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder> {
private List<GalleryItem> mDataset;

public static class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
Log.d("tagdetest2","myviewholder");
imageView = itemView.findViewById(R.id.gallery_image);
}
}

public GalleryAdapter(List<GalleryItem> myDataset) {
mDataset = myDataset;
}

@NonNull
@Override
public GalleryAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
Log.d("tagdetest2","onCreateViewHolder");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gallery, parent, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull GalleryAdapter.MyViewHolder myViewHolder, int i) {
Log.d("tagdetest2", mDataset.get(i).link);
Glide.with(myViewHolder.itemView.getContext()).load(mDataset.get(i).link).into(myViewHolder.imageView);
}

@Override
public int getItemCount() {
Log.d("tagdetest", "size = " + mDataset.size());
return mDataset.size();
}

item_gallery.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="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/gallery_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

home_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

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

</android.support.constraint.ConstraintLayout>

最佳答案

myDataset = Arrays.asList(response);

覆盖引用,在你的 fragment 中而不是在你的适配器中。

试试

mDataset.addAll(Arrays.asList(response));

插入

关于java - 未调用 RecyclerView 重写方法并且 getItemCount 始终返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com