我几乎尝试了所有方法,但是Recycleview并未显示在屏幕上。我不断收到此错误'E / RecyclerView:未连接适配器;跳过布局”。几天来一直在尝试解决此问题。
作为旁注,我使用的是Fragment,这可能就是为什么这让我下地狱。
uploadsFragment.java
private RecyclerView mRecylcerView;
private ImageAdapter mAdapter;
private ProgressBar mProgressCircle;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_uploads, container,
false);
mRecylcerView =
(RecyclerView)view.findViewById(R.id.RecyclerView);
mRecylcerView.setHasFixedSize(true);
mRecylcerView.setLayoutManager(new
LinearLayoutManager(getActivity()));
mProgressCircle =
(ProgressBar)view.findViewById(R.id.progress_circle2);
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new ImageAdapter(getActivity(), mUploads);
mRecylcerView.setHasFixedSize(true);
mRecylcerView.setLayoutManager(new
LinearLayoutManager(getActivity()));
mRecylcerView.setAdapter(mAdapter);
mProgressCircle.setVisibility(View.INVISIBLE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(),databaseError.getMessage(),
Toast.LENGTH_SHORT).show();
mProgressCircle.setVisibility(View.INVISIBLE);
}
});
return view;
}
ImageAdapter.java
private Context mContext;
private List<Upload> mUploads;
public ImageAdapter(Context context, List<Upload> upload){
mContext = context;
mUploads = upload;
}
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.image_item, viewGroup, false);
return new ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
Upload uploadCurrent = mUploads.get(i);
imageViewHolder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getmImageUrl())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(imageViewHolder.imageView);
}
@Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
imageView = itemView.findViewById(R.id.image_view_upload);
}
}
答案 0 :(得分:0)
您正在从Firebase查询获得响应后,使用RecyclerView设置适配器。
答案 1 :(得分:0)
将LayoutInflater.from(mContext).inflate(R.layout.image_item, viewGroup, false);
替换为:
LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_item, viewGroup, false);
另外,将代码放在onViewCreated
中,而不要放在onCreateView