I've a gridview with clickable items. When I click on an item the gridview is became hidden and I show a ViewPager.
In my case this ViewPager should work like slideshow.
So I've tried to follow the Google example here: http://developer.android.com/training/animation/screen-slide.html
If I open a new activity on gridview item click, the viewpager is working well (like the Google example). But if I open the ViewPager hiding the gridview the slide animation become lagging.
Obviously I've tried to open the viewpager with same graphics/bitmaps etc..
Unfortunately my application need to work in the same activity, and I cannot open a new one.
Is there some limitations about ViewPager? Or I should give attention on something particular?
Quite Simple.
Unfortunately my application need to work in the same activity, and I cannot open a new one.
I presume when a user click one of the grid view of the item (maybe containing image), a view pagers shows up with the image and the user can slide the ViewPager for next image and so on.
In this case, use a Dialog Fragment.
Layout for ViewPager's Image :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<ImageView
android:id="#+id/image_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="fitCenter" />
</RelativeLayout>
Dialog Fragment Code :
public class SlideshowDialogFragment extends DialogFragment {
private String TAG = SlideshowDialogFragment.class.getSimpleName();
private ArrayList<Image> images;
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private TextView lblCount, lblTitle, lblDate;
private int selectedPosition = 0;
static SlideshowDialogFragment newInstance() {
SlideshowDialogFragment f = new SlideshowDialogFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_image_slider, container, false);
viewPager = (ViewPager) v.findViewById(R.id.viewpager);
lblCount = (TextView) v.findViewById(R.id.lbl_count);
lblTitle = (TextView) v.findViewById(R.id.title);
lblDate = (TextView) v.findViewById(R.id.date);
images = (ArrayList<Image>) getArguments().getSerializable("images");
selectedPosition = getArguments().getInt("position");
Log.e(TAG, "position: " + selectedPosition);
Log.e(TAG, "images size: " + images.size());
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
setCurrentItem(selectedPosition);
return v;
}
private void setCurrentItem(int position) {
viewPager.setCurrentItem(position, false);
displayMetaInfo(selectedPosition);
}
// page change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
displayMetaInfo(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
};
private void displayMetaInfo(int position) {
lblCount.setText((position + 1) + " of " + images.size());
Image image = images.get(position);
lblTitle.setText(image.getName());
lblDate.setText(image.getTimestamp());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
// adapter
public class MyViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public MyViewPagerAdapter() {
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.image_fullscreen_preview, container, false);
ImageView imageViewPreview = (ImageView) view.findViewById(R.id.image_preview);
Image image = images.get(position);
Glide.with(getActivity()).load(image.getLarge())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageViewPreview);
container.addView(view);
return view;
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((View) obj);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
Send the image link and its description/ title through this code:
Bundle bundle = new Bundle();
bundle.putSerializable("images", images);
bundle.putInt("position", position);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
newFragment.setArguments(bundle);
newFragment.show(ft, "slideshow");
The array List being used :
private ArrayList<Image> images = new ArrayList<>();
Image image = new Image();
image.setName("Image Title");
image.setLarge("ImageUrl");
Related
I had 2 activity in which one one activity_1 I have RecyclerView with images and activity_2 have view pager with image. I just want to perform image shared element transaction from activity_1 recycler view to activity_2 viewpager image as shown in image.
every thing was working fine except the transaction.
please help
activity_2 code
int position = getIntent().getIntExtra(TestActivity.EXTRA_POSITION, 1);
ArrayList<AnimalItem> filelist = (ArrayList<AnimalItem>) getIntent().getSerializableExtra(TestActivity.EXTRA_ANIMAL_ARRAYLIST);
ViewPager viewPager = findViewById(R.id.animal_view_pager);
MyCustomPagerAdapter myCustomPagerAdapter = new MyCustomPagerAdapter(PageViewerActivity.this, filelist);
// myCustomPagerAdapter.getItem(position).setTransitionName(getResources().getString(R.string.transition_contenet_topic));
viewPager.setAdapter(myCustomPagerAdapter);
viewPager.setCurrentItem(position);
CustomPageAdapter.class
public class MyCustomPagerAdapter extends PagerAdapter {
private Context context;
private ArrayList<AnimalItem> images;
private LayoutInflater layoutInflater;
public MyCustomPagerAdapter(Context context, ArrayList<AnimalItem> images) {
this.context = context;
this.images = images;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position) {
View itemView = layoutInflater.inflate(R.layout.test_image_card, container, false);
ImageView imageView =itemView.findViewById(R.id.id_main_image);
Picasso.with(context).load(images.get(position).imageUrl).into(imageView);
container.addView(itemView);
//listening to image click
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "you clicked image " + (position + 1), Toast.LENGTH_LONG).show();
}
});
return itemView;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((LinearLayout) object);
}
}
activity_1 (on recycler item click code)
Intent intent = new Intent(this, PageViewerActivity.class);
intent.putExtra(EXTRA_POSITION,pos);
intent.putExtra(EXTRA_ANIMAL_ARRAYLIST, Utils.generateAnimalItems(getApplicationContext()));
intent.putExtra(EXTRA_ANIMAL_IMAGE_TRANSITION_NAME, ViewCompat.getTransitionName(sharedImageView));
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this,
sharedImageView,
ViewCompat.getTransitionName(sharedImageView));
startActivity(intent, options.toBundle());
You may use one activity to achieve this target, And consider activity_1#RecyclerView view item as FlipView, and on top of flipView imageView and viewPager and on flip action animate imageView with viewPager.
Implementation 'eu.davidea:flipview:1.1.3'
<eu.davidea.flipview.FlipView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flip_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:animateDesignLayoutOnly="true" // This line is important
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Put ImageView on top to hide ViewPager until flip action -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</eu.davidea.flipview.FlipView>
Recently started with Android development and am in the process of creating a screen slide that would appear after selecting an image that is part of a gallery (grid view). I followed the Android Tutorial here however the problem that I am running into is the ViewPager doesn't seem to work (logs in the activity aren't printing out), and the only thing I see in the new fragment is some text.
Below I have included the code, and any assistance would be appreciated!
This is where the initial call from the gallery is made to show the slider goToGallerySlider()
PhotoGalleryFragment.java
public class PhotoGalleryFragment extends BaseFragment {
private static final String TAG = PhotoGalleryFragment.class.getSimpleName();
public static final String EVENT_ID_EXTRA = "EventDetailFragment.EventIdExtra";
private GridView grid;
public List<EventPhoto> eventDetailPhotoList;
#Bind(R.id.photoRoll)
GridView photoRoll;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_event_detail_photo_gallery, container, false);
grid = (GridView) view.findViewById(R.id.photoRoll);
Context aContext = getActivity().getBaseContext();
grid.setAdapter(new PhotoGalleryAdapter(aContext, eventDetailPhotoList));
grid.setOnItemClickListener((parent, v, position, id) ->
goToGallerySlider(parent, v, position, id));
return view;
}
#Override
public int getTitleResourceId() {
return R.string.photo_gallery;
}
private void goToGallerySlider(ViewGroup parent, View v, int position, long id) {
Log.d(TAG, "!!!We are in the goToGallerySlider() call function!");
GallerySliderFragment sliderFragment = new GallerySliderFragment();
sliderFragment.eventDetailPhotoList = eventDetailPhotoList;
getBaseActivity().setFragment(sliderFragment, true);
}
}
fragment_photo_gallery_slider.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView style="?android:textAppearanceMedium"
android:id="#+id/textView_in_slider_fragment"
android:padding="16dp"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lorem" />
</RelativeLayout>
activity_photo_gallery_slider.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
GallerySliderFragment.java
public class GallerySliderFragment extends BaseFragment {
private static final String TAG = GallerySliderFragment.class.getSimpleName();
public List<EventPhoto> eventDetailPhotoList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "!!!We are inside the onCreate in GallerySliderFragment");
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_event_detail_photo_gallery_slider, container, false);
((AngelPointsApplication) getActivity().getApplication()).getComponent().inject(this);
ButterKnife.bind(this, view);
return view;
}
public int getTitleResourceId() {return R.string.gallery_slider; }
}
PhotoGalleryActivity.java
public class PhotoGalleryActivity extends BaseActivity {
private static final String TAG = PhotoGalleryFragment.class.getSimpleName();
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private static final int NUM_PAGES = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "!!!We are in the onCreate in PhotoGalleryActivity");
setContentView(R.layout.activity_event_detail_photo_gallery_slider);
mPager = (ViewPager) findViewById(R.id.viewPager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
Log.d(TAG, "!!!In PhotoGalleryActivity user pressed BACK Button on 1st Image");
super.onBackPressed();
} else {
Log.d(TAG, "!!!In PhotoGalleryActivity user pressed BACK Button to see previous photo");
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
Log.d(TAG, "!!!In PhotoGalleryActivity get next item position");
return new GallerySliderFragment();
}
#Override
public int getCount() {
Log.d(TAG, "!!!In PhotoGalleryActivity get count: " + NUM_PAGES);
return NUM_PAGES;
}
}
}
I have used viewpager which displays view with an image and radiobutton below. Each swipe screen is not a fragment, they are layouts which just swipe the same view with different imageview. The problem is that, I am unable to deselect the radio button which is in left and right side of the current view after i select the radio button of the current screen. If I return POSITION_NONE in getItemPosition(), it refresh the screen and radio button also deselected but the view is flickered. If i am able to call instantiateItem(), my problem is solved . But this method is called only when view is destroyed based on the setOffsetScreenPageLimit(). How can I achieved such requirements, if view pager cannot help?
Fragment:
public class AnswerImageDialogFragment extends DialogFragment implements ViewPager.OnPageChangeListener {
//member declaration
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strQuestion = null;
View rootView = inflater.inflate(R.layout.activity_viewpager_demo, container,
false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
intro_images = (ViewPager) rootView.findViewById(R.id.pager_introduction);
pager_indicator = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
//do some stuff
//set the adapter
mAdapter = new ViewPagerAdapter(getContext(), map);
intro_images.setAdapter(mAdapter);
intro_images.setCurrentItem(clickPosition);
intro_images.setOnPageChangeListener(this);
setUiPageViewController();
bus = EventBus.getDefault();
bus.register(this);
return rootView;
}
//other method
}
PagerAdapter:
public class ViewPagerAdapter extends PagerAdapter {
// member declaration
public ViewPagerAdapter(Context mContext, HashMap<String, Object> map) {
//other initialization
}
#Override
public int getCount() {
return optionImages.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.pager_item, container, false);
final ImageView ivOption = (ImageView) itemView.findViewById(R.id.answerId); // this is used as radio button in this case
/*The code snippet is to select the answer option based on whether answer is choosen or not.
If choosen, select as default in the pop up answer.
*/
if (null != ans) {
Iterator iterator = ans.iterator();
if (ans.size() > 0) {
int value = (int) iterator.next();
if (value == position) {
ivOption.setImageResource(R.drawable.ic_radio_button_tick);
} else {
ivOption.setImageResource(R.drawable.ic_radio_button_nontick);
}
}
}
p = position;
/*The code snippet is to change the answer value based on the user selection.
This means if user choosen another answer then clear the earlier choosen answer
*/
ivOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
txtQuestion.setText(question);
txtOption.setText(optionsList.get(position));
imageView.setParseFile(optionImages.get(position));
imageView.loadInBackground();
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
LinearLayout ll = (LinearLayout) object;
/*RelativeLayout ivOption = (RelativeLayout) ll.getChildAt(2);
ImageView iv = (ImageView) ivOption.getChildAt(1);
iv.setImageResource(R.drawable.ic_radio_button_tick);*/
container.removeView(ll);
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
public void refresh(ArrayList object) {
this.object = new ArrayList();
this.object.addAll(object);
this.notifyDataSetChanged();
}
When i am using simple imageview or AspectRatioImageView I am able to load images fetched from server. But if I am using TouchImageView or GestureImageView it is not able to load images. I see a blank screen.
Apart from using UIL,I have tried some other ways of loading images also but same result.
However when i have opened a view in viewpager and its not showing image..then if i switch to landscape mode and then again to portrait mode then it is able to load image..If i call notifyDataSetChanged() anywhere inside adapter it crashes showing InflateException
xml
<com.polites.android.GestureImageView
android:id="#+id/imagefullscreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_gravity="center"/>
Activity
pager.setAdapter(imagePagerAdapter = new ImagePagerAdapter(ImagePagerActivity.this, posts));
pager.setCurrentItem(pos);
PageListener pageListener = new PageListener();
pager.setOnPageChangeListener(pageListener);
imagePagerAdapter.notifyDataSetChanged();
Adapter
public ImagePagerAdapter(Activity activity,
List<Post> object) {
this._activity = activity;
posts = object;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(android.R.color.transparent)
.showImageForEmptyUri(android.R.color.transparent)
.showImageOnFail(android.R.color.transparent)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(_activity));
}
#Override
public int getCount() {
return posts.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
final Post post = posts.get(position);
View viewLayout = inflater.inflate(R.layout.item_pager_image, container,
false);
visualizerView = (VisualizerView) viewLayout.findViewById(R.id.visualizerView);
image = (GestureImageView) viewLayout.findViewById(R.id.imagefullscreen);
mGDescription = (TextView) viewLayout.findViewById(R.id.guggu_pager_description);
image.setVisibility(View.VISIBLE);
mGDescription.setVisibility(View.VISIBLE);
play = (ImageView) viewLayout.findViewById(R.id.play);
pause = (ImageView) viewLayout.findViewById(R.id.pause);
imageLoader.displayImage(url,images, options);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO:pausing audio/video here
}
});
}
container.addView(viewLayout);
viewLayout.setTag("my" + position);
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
The issue was because TouchImageView or GestureImageView is taking time to load image from server and view was not updated after image is being loaded.
I added-
private class PageListener extends ViewPager.SimpleOnPageChangeListener
{
public void onPageSelected(int position)
{
imagePagerAdapter.notifyDataSetChanged();
}
}
inside activity class so that view is updated whenever user swipe to new page.But with this the first view that I was opening was still showing blank..so i added
h.postDelayed(r=new Runnable()
{
#Override
public void run() {
if (mPosition == position)
{
notifyDataSetChanged();
h.removeCallbacks(r);
}
}
}, 1000);
inside instantiateItem of adapter so that first view is updated after 1 second when view is created as by that time image is fetched from server.mPosition is the position of first view that is being opened.
This completely solves my issue.
I am trying to implement a scroller like show in the image below.
I have tried using viewpager but it only shows one item at a time. And I need to show 5 of them and of different sizes. The one in middle needs to be bigger.
Each Item is a frameLayout that contains an ImageView and a TexView, I dont have any problem implementing that part. The problem is it needs to be a scroller and have many items in scroller e.g upto 15 maybe. But should have only 5 items visible at any one time just like shown below. I have tried many implementations. Please some one give me a working example as I have already tried many examples none of them works perfectly. I have waisted more than a week on this one.
You can control it by overriding getPageWidth() in the PagerFragmentAdapter:
#Override
public float getPageWidth(int position) {
return(0.4f);
}
and making sure the size of your images is not too large, so that the page width fits multiple images.
Here are all the steps to set this up:
1) Add a fragment container to your activity layout, where you will load the PhotoPagerFragment:
<!-- PHOTO PAGER FRAGMENT -->
<FrameLayout
android:id="#+id/photoPagerFragmentContainer"
android:layout_width="match_parent"
android:layout_height="150dp"
android:tag="sticky"
android:layout_gravity="center_horizontal" >
</FrameLayout>
2) Inject the PhotoPagerFragment in your activity's onCreate():
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
//Insert the fragment
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.photoPagerFragmentContainer);
if (fragment == null) {
fragment = new PhotoPagerFragment();
fm.beginTransaction()
.add(R.id.photoPagerFragmentContainer, fragment)
.commit();
}
}
3) Create a layout for your PhotoPagerFragment:
<?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:background="#android:color/black"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/photoPager"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_marginTop="2dp"/>
</LinearLayout>
4) Create your PhotoPagerFragment:
public class PhotoPagerFragment extends Fragment {
private ViewPager mPhotoPager;
private PagerAdapter mPhotoAdapter;
public static final String TAG = "PhotoPagerFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_photo_pager, container, false);
mPhotoAdapter = new PhotoPagerFragmentAdapter(getActivity().getSupportFragmentManager());
mPhotoPager = (ViewPager) view.findViewById(R.id.photoPager);
mPhotoPager.setAdapter(mPhotoAdapter);
return view;
}
}
5) And the adapter:
public class PhotoPagerFragmentAdapter extends FragmentPagerAdapter {
private int[] Images = new int[] {
R.drawable.photo_1, R.drawable.photo_2,
R.drawable.photo_3, R.drawable.photo_4,
R.drawable.photo_5, R.drawable.photo_6
};
private int mCount = Images.length;
public PhotoPagerFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return PhotoDetailsFragment.newInstance(Images[position]);
}
#Override
public int getCount() {
return mCount;
}
#Override
public float getPageWidth(int position) {
return(0.4f);
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
6) And finally, your PhotoDetailsFragment that will show each image:
public final class PhotoDetailsFragment extends Fragment {
private int photoResId;
private static final String TAG = "PhotoDetailsFragment";
public static final String EXTRA_PHOTO_ID = "com.sample.photo_res_id";
public static PhotoDetailsFragment newInstance(int photoResId) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_PHOTO_ID, photoResId);
PhotoDetailsFragment fragment = new PhotoDetailsFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
photoResId = (Integer)getArguments().getSerializable(EXTRA_PHOTO_ID);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ImageView image = new ImageView(getActivity());
image.setImageResource(photoResId);
// Hook up the clicks on the thumbnail views
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
...
}
});
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(-1, -1));
layout.setGravity(Gravity.CENTER);
layout.addView(image);
return layout;
}
}