hi when i run my app , I cant see the correct Activity . I think I have a problem with my recycle view but when I checking my code , there is no wrong code . I’m inflated my layouts and return my views .
I don’t know what’s wrong with it.
i have 3 layout :
{activity_fragment , id:fragment_container}
{fragment_photo_gallery , id : photo_recycler_view}
{list_item_gallery , id : item_image_view}
public class PhotoGalleryFragment extends Fragment {
private static final String TAG = "PhotoGalleryFragment";
private RecyclerView mPhotoRecyclerView;
private List<GalleryItem> mItems = new ArrayList<>();
private ThumbnailDownloader<PhotoHolder> mThumbnailDownloader;
public static PhotoGalleryFragment newInstance(){
return new PhotoGalleryFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
new FetchItemsTask().execute();
Handler responseHandler = new Handler();
mThumbnailDownloader = new ThumbnailDownloader<>(responseHandler);
mThumbnailDownloader.setThumbnailDownloadListener(
new ThumbnailDownloader.ThumbnailDownloadListener<PhotoHolder>() {
@Override
public void onThumbnailDownloaded(PhotoHolder target, Bitmap thumbnail) {
Drawable drawable = new BitmapDrawable(getResources(),thumbnail);
target.bindDrawable(drawable);
}
}
);
mThumbnailDownloader.start();
mThumbnailDownloader.getLooper();
Log.i(TAG,"background thread start");
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery,container,false);
mPhotoRecyclerView=(RecyclerView) v.findViewById(R.id.photo_recycler_view);
mPhotoRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
setupAdapter();
return v;
}
public void setupAdapter() {
if (isAdded()){
mPhotoRecyclerView.setAdapter(new PhotoAdapter(mItems));
}
}
@Override
public void onDestroy() {
super.onDestroy();
mThumbnailDownloader.quit();
Log.i(TAG,"background thread destroy");
}
private static class PhotoHolder extends RecyclerView.ViewHolder{
private ImageView mItemImageView;
public PhotoHolder(View itemView) {
super(itemView);
mItemImageView=(ImageView) itemView.findViewById(R.id.item_image_view);
}
public void bindDrawable(Drawable drawable) {
mItemImageView.setImageDrawable(drawable);
}
}
private class PhotoAdapter extends RecyclerView.Adapter<PhotoHolder> {
private List<GalleryItem> mGalleryItems;
public PhotoAdapter (List<GalleryItem> galleryItems){
mGalleryItems=galleryItems;
}
@Override
public PhotoHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.list_item_gallery,viewGroup,false);
return new PhotoHolder(view);
}
@Override
public void onBindViewHolder(PhotoHolder holder, int position) {
GalleryItem galleryItem = mGalleryItems.get(position);
Drawable placeHolder=getResources().getDrawable(R.drawable.bill_up_close);
holder.bindDrawable(placeHolder);
mThumbnailDownloader.queueThumbnail(holder,galleryItem.getUrl());
}
@Override
public int getItemCount() {
return mGalleryItems.size();
}
}
private class FetchItemsTask extends AsyncTask<Void,Void,List<GalleryItem>> {
@Override
protected List<GalleryItem> doInBackground(Void... params) {
return new FlickrFetchr().fetchItem();
}
@Override
protected void onPostExecute(List<GalleryItem> Items) {
mItems = Items;
setupAdapter();
}
}
}