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

时间:2018-11-14 11:32:07

标签: java android android-recyclerview listadapter

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

HomeFragment.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());
}

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

1 个答案:

答案 0 :(得分:2)

myDataset = Arrays.asList(response);

在片段中而不在适配器中覆盖引用。

尝试

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

insetad