初始化RecyclerView时遇到一个奇怪的问题。当我调试时,所有内容都正确填充,并显示在列表中。当我运行应用程序时,getItemCount()仍为0。
我知道数据已初始化,因为它已被记录。但看起来代码没有以正确的顺序运行。在从Contentful返回数据之前记录大小。
在没有调试的情况下运行时记录:
10-27 13:15:01.436 21978-21978/? I/art: Not late-enabling -Xcheck:jni (already on)
10-27 13:15:01.436 21978-21978/? W/art: Unexpected CPU variant for X86 using defaults: x86
10-27 13:15:01.449 21978-21983/? I/art: Debugger is no longer active
10-27 13:15:01.449 21978-21983/? I/art: Starting a blocking GC Instrumentation
10-27 13:15:01.550 21978-21978/? W/System: ClassLoader referenced unknown path: /data/app/com.x.x-1/lib/x86
10-27 13:15:01.560 21978-21978/? I/InstantRun: starting instant run server: is main process
10-27 13:15:01.664 21978-21978/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
10-27 13:15:01.738 21978-21978/? I/com.x.x.VideoListViewActivity: Size: 0
10-27 13:15:01.790 21978-21996/? I/OpenGLRenderer: Initialized EGL, version 1.4
10-27 13:15:01.790 21978-21996/? D/OpenGLRenderer: Swap behavior 1
10-27 13:15:01.790 21978-21996/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
10-27 13:15:01.791 21978-21996/? D/OpenGLRenderer: Swap behavior 0
10-27 13:15:01.961 21978-21995/? I/com.x.x.Videos$1: Contentful:Test
10-27 13:15:01.961 21978-21995/? I/com.x.x.Videos$1: Contentful:Test 1
使用debug运行时记录:
10-27 13:15:42.116 22660-22660/? I/art: Not late-enabling -Xcheck:jni (already on)
10-27 13:15:42.117 22660-22660/? W/art: Unexpected CPU variant for X86 using defaults: x86
10-27 13:15:42.130 22660-22667/? E/art: Failed sending reply to debugger: Broken pipe
10-27 13:15:42.130 22660-22667/? I/art: Debugger is no longer active
10-27 13:15:42.130 22660-22667/? I/art: Starting a blocking GC Instrumentation
10-27 13:15:42.149 22660-22660/? W/ActivityThread: Application com.httpk9fitclub.k9fitclub is waiting for the debugger on port 8100...
10-27 13:15:42.149 22660-22660/? I/System.out: Sending WAIT chunk
[ 10-27 13:15:42.158 1612: 1635 D/ ]
HostConnection::get() New Host Connection established 0x87c7f7c0, tid 1635
10-27 13:15:44.138 22660-22667/com.x.xI/art: Debugger is active
10-27 13:15:44.153 22660-22660/com.x.xI/System.out: Debugger has connected
10-27 13:15:44.154 22660-22660/com.x.xI/System.out: waiting for debugger to settle...
10-27 13:15:44.355 22660-22660/com.x.xI/System.out: waiting for debugger to settle...
10-27 13:15:44.555 22660-22660/com.x.xI/System.out: waiting for debugger to settle...
10-27 13:15:44.756 22660-22660/com.x.xI/System.out: waiting for debugger to settle...
10-27 13:15:44.957 22660-22660/com.x.xI/System.out: waiting for debugger to settle...
10-27 13:15:45.158 22660-22660/com.x.xI/System.out: waiting for debugger to settle...
10-27 13:15:45.359 22660-22660/com.x.xI/System.out: waiting for debugger to settle...
10-27 13:15:45.559 22660-22660/com.x.xI/System.out: debugger has settled (1403)
10-27 13:15:45.621 22660-22660/com.x.xW/System: ClassLoader referenced unknown path: /data/app/com.x.x-1/lib/x86
10-27 13:15:45.628 22660-22660/com.x.xI/InstantRun: starting instant run server: is main process
10-27 13:15:45.633 22660-22667/com.x.xI/art: Starting a blocking GC Instrumentation
10-27 13:15:45.783 22660-22663/com.x.xI/art: Do partial code cache collection, code=30KB, data=16KB
10-27 13:15:45.784 22660-22663/com.x.xI/art: After code cache collection, code=24KB, data=13KB
10-27 13:15:45.784 22660-22663/com.x.xI/art: Increasing code cache capacity to 128KB
10-27 13:15:45.824 22660-22660/com.x.xD/NetworkSecurityConfig: No Network Security Config specified, using platform default
10-27 13:15:45.892 22660-22663/com.x.xI/art: Do partial code cache collection, code=60KB, data=39KB
10-27 13:15:45.893 22660-22663/com.x.xI/art: After code cache collection, code=58KB, data=38KB
10-27 13:15:45.893 22660-22663/com.x.xI/art: Increasing code cache capacity to 256KB
10-27 13:15:46.320 22660-22663/com.x.xI/art: Do full code cache collection, code=122KB, data=81KB
10-27 13:15:46.321 22660-22663/com.x.xI/art: Starting a blocking GC JitCodeCache
10-27 13:15:46.321 22660-22663/com.x.xI/art: After code cache collection, code=95KB, data=57KB
10-27 13:15:46.341 22660-22731/com.x.xI/com.x.x.Videos$1: Contentful:Test
10-27 13:15:46.341 22660-22731/com.x.xI/com.x.x.Videos$1: Contentful:Test 1
10-27 13:15:46.341 22660-22731/com.x.xI/com.x.x.Videos$1: Contentful:Maggie Test
10-27 13:15:46.341 22660-22731/com.x.xI/com.x.x.Videos$1: Contentful:K9 Fit club Test
10-27 13:15:48.344 22660-22660/com.x.xI/com.x.x.VideoListViewActivity: Size: 4
10-27 13:15:48.431 22660-22769/com.x.xI/OpenGLRenderer: Initialized EGL, version 1.4
10-27 13:15:48.431 22660-22769/com.x.xD/OpenGLRenderer: Swap behavior 1
10-27 13:15:48.431 22660-22769/com.x.xW/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
10-27 13:15:48.431 22660-22769/com.x.xD/OpenGLRenderer: Swap behavior 0
VideoListViewActivity.java
public class VideoListViewActivity extends Activity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.video_list_activity);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
initializeAdapter();
super.onCreate(savedInstanceState);
}
private void initializeAdapter() {
VideoListAdapter adapter = new VideoListAdapter(this, new Videos());
Log.i("size", "" + adapter.getItemCount());
recyclerView.setAdapter(adapter);
}
}
Videos.java
public class Videos {
private static final String CDA_TOKEN = "x";
private static final String SPACE_ID = "x";
private final CDAClient client = CDAClient
.builder()
.setToken(CDA_TOKEN)
.setSpace(SPACE_ID)
.build();
private List<Video> videos;
public Videos() {
videos = new ArrayList<>();
client
.observe(CDAEntry.class)
.where("content_type", "video")
.all()
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<CDAArray>() {
CDAArray result;
@Override
public void onCompleted() {
for (final CDAResource resource : result.items()) {
CDAEntry entry = (CDAEntry) resource;
videos.add(new Video(entry.id(), entry.getField("videoTitle").toString(), entry.getField("videoDescription").toString(), R.drawable.dog));
Log.i("Contentful", entry.getField("videoTitle").toString());
}
}
@Override
public void onError(Throwable error) {
Log.e("Contentful", "could not request entry", error);
}
@Override
public void onNext(CDAArray cdaArray) {
result = cdaArray;
}
});
}
public List<Video> getVideos() {
return videos;
}
}
VideoListAdapter.java
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.VideoViewHolder> {
public class VideoViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView videoTitle;
TextView videoDescription;
ImageView videoPhoto;
VideoViewHolder(View itemView, final VideoListViewActivity activity) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.cv);
videoTitle = (TextView) itemView.findViewById(R.id.video_title);
videoDescription = (TextView) itemView.findViewById(R.id.video_description);
videoPhoto = (ImageView) itemView.findViewById(R.id.video_photo);
}
}
Videos videos;
VideoListViewActivity videoListViewActivity;
VideoListAdapter(VideoListViewActivity videoListViewActivity, Videos videos) {
this.videos = videos;
this.videoListViewActivity = videoListViewActivity;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public VideoViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.video_card, viewGroup, false);
VideoViewHolder videoViewHolder = new VideoViewHolder(v, videoListViewActivity);
return videoViewHolder;
}
@Override
public void onBindViewHolder(VideoViewHolder videoViewHolder, int i) {
Video video = videos.getVideos().get(i);
setOnClickListener(videoViewHolder.itemView, video);
videoViewHolder.videoTitle.setText(videos.getVideos().get(i).videoTitle);
videoViewHolder.videoDescription.setText(videos.getVideos().get(i).videoDescription);
videoViewHolder.videoPhoto.setImageResource(videos.getVideos().get(i).photoId);
}
@Override
public int getItemCount() {
if (videos.getVideos() == null) {
return 0;
} else {
return videos.getVideos().size();
}
}
private void setOnClickListener(View itemView, final Video video) {
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), VideoDetailActivity.class);
intent.putExtra(Constants.VIDEO, video);
v.getContext().startActivity(intent);
}
};
CardView cardView = (CardView) itemView.findViewById(R.id.cv);
TextView videoTitle = (TextView) itemView.findViewById(R.id.video_title);
TextView videoDescription = (TextView) itemView.findViewById(R.id.video_description);
ImageView videoPhoto = (ImageView) itemView.findViewById(R.id.video_photo);
itemView.setOnClickListener(onClickListener);
cardView.setOnClickListener(onClickListener);
videoTitle.setOnClickListener(onClickListener);
videoDescription.setOnClickListener(onClickListener);
videoPhoto.setOnClickListener(onClickListener);
}
}