public Object instantiateItem(ViewGroup container, int position) {
Context context = FullImageActivity.this; // i get error in this line that its not an enclosing class
I simply want to call FullImageActivity to ImageAdapter class
ImageAdapter class
public class ImageAdapter extends PagerAdapter {
public String mImages[] = {("http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg"),
("https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300"),
("https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg"),
("http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg"),
("http://i.imgur.com/4IZMjx3.jpg")};
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = FullImageActivity.this;
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(context).load(mImages[position]).placeholder(R.drawable.loading2).error(R.drawable.nointernet).into(imageView);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
This is my FullImageActivity class
public class FullImageActivity extends AppCompatActivity {
TouchImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("ViewPager");
setContentView(R.layout.activity_full_image);
Intent i = getIntent();
TouchImageView img = new TouchImageView(this);
int position = i.getExtras().getInt("id");
ImageAdapter adapter = new ImageAdapter();
img = (TouchImageView) findViewById(R.id.img);
img.setMaxZoom(4f);
Picasso.with(getApplicationContext()).load(adapter.mImages[position]).into(img);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
}
Your Adapter Class
public class ImageAdapter extends PagerAdapter {
Context activity_context;
public String mImages[] = {("http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg"),
("https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300"),
("https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg"),
("http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg"),
("http://i.imgur.com/4IZMjx3.jpg")};
public ImageAdapter(Context context)
{
activity_context=context;
}
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(activity_context);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(activity_context).load(mImages[position]).placeholder(R.drawable.loading2).error(R.drawable.nointernet).into(imageView);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Your Activity class
public class FullImageActivity extends AppCompatActivity {
TouchImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("ViewPager");
setContentView(R.layout.activity_full_image);
Intent i = getIntent();
TouchImageView img = new TouchImageView(this);
int position = i.getExtras().getInt("id");
ImageAdapter adapter = new ImageAdapter(FullImageActivity.this);
img = (TouchImageView) findViewById(R.id.img);
img.setMaxZoom(4f);
Picasso.with(getApplicationContext()).load(adapter.mImages[position]).into(img);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
}
Related
I've made a image slider with help of viewpager and picasso, now i want to add next, prev button on the image, how can i do that?
I saw different answers of this but all of them are showing error
This is main activity where image slider is shown:
public class MainActivity extends AppCompatActivity {
private int[] imageUrls = new int[]{
R.drawable.after_cookie,
R.drawable.before_cookie
};
private WebBackForwardList viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
ViewPageAdapter adapter = new ViewPageAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
}
}
This is ViewPageAdapter:
public class ViewPageAdapter extends PagerAdapter {
private Context context;
private int[] imageUrls;
ViewPageAdapter(Context context, int[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return imageUrls.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Picasso.get()
.load(imageUrls[position])
.fit()
.centerCrop()
.into(imageView);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
add this method to your code
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
then use this for next button
Button NextButton = (Button)findViewById(R.id.button1);
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(getItem(+1), true); //getItem(+1) for next
}
});
and use this for the previous button
Button PreviousButton = (Button)findViewById(R.id.button2);
PreviousButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(getItem(-1), true); //getItem(-1) for previous
}
});
it will work fine ;)
How can I implement an Activity with page_ two? What can I do?
Here's an example:
So far, my activity code looks like this:
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new CustomPagerAdapter(this));
CustomPagerAdapter.java
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
#Override
public int getCount() {
return ModelObject.values().length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
And ModelObject.java
public enum ModelObject {
BLUE(R.string.blue, R.layout.page_one),
GREEN(R.string.green, R.layout.page_two),
RED(R.string.blue, R.layout.page_tree);
private int mTitleResId;
private int mLayoutResId;
ModelObject(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}
}
How can I solve this ?
I changed list but I didn't;
So how ?
I need to get the vales passed from ThePagerAdapter.java inside SliderHome.java. How can I implement this? I have tried a lot, but no success.
This is my code:
ThePagerAdapter.java
public class ThePagerAdapter extends PagerAdapter {
Context context;
#Override
public int getCount() {
return list.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
public ThePagerAdapter(Context context,List<Slider> list) {
this.list = list;
this.context = context;
}
#Override
public Object instantiateItem(final ViewGroup container, int position) {
// Declare Variables
ImageView imgflag;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.image_item, container, false);
// Capture position and set to the TextViews
final String id = list.get(position).getId();
imgflag = (ImageView) itemView.findViewById(R.id.imageView);
imgflag.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(context, SliderMore.class);
i.putExtra("slider_id", id);
context.startActivity(i);
}
});
((ViewPager) container).addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
From the above PagerAdapter I am send values to another Fragment described below using Intent.putExtra
SliderHome.java
public class SliderHome extends Fragment {
ViewPager viewPager;
ImageView iv2,iv3;
String slider_id;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.slider_more, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewPager);
iv2 = (ImageView) rootView.findViewById(R.id.imageView2);
iv3 = (ImageView) rootView.findViewById(R.id.imageView3);
return rootView;
}
Is there a solution? Thanks in advance.
In this cases I like to use a Factory Method Pattern.
Example:
public static MatchDetailSummaryFragment newInstance(String matchIdString, int position) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putString(ARGS_MY_ID, matchIdString);
args.putInt(ARG_POSITION,position);
fragment.setArguments(args);
return fragment;
}
public MyFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mPosition = getArguments().getInt(ARG_POSITION);
this.id = getArguments().getString(ARGS_MY_ID) != null)
}
You can invoke this:
public void fillMyPagerAdapter()
{
mChildFragments = new Hashtable<Integer,GxuBaseFragment>();
int count = 0;
MyFragment a = MyFragment.newInstance(Integer.toString(my.getId()),count);
mChildFragments.put(count,a);
count += 1;
mViewPagerAdapter.setTabHolderScrollingContent(this);
mPager.setAdapter(mViewPagerAdapter);
mPagerSlidingTabStrip.setViewPager(mPager);
mPagerSlidingTabStrip.setOnPageChangeListener(this);
}
This code may not work because is a couple of extracts of my production code but it will explain the main ideas.
I have a PagerAdapter that displays images gathered from a string array of locations passed from the MainActivity class. I wish to make it so that I can refer to this PagerAdapter in the future, to create more views dynamically. I am curious as to how one goes about to doing this? My code for the PagerAdapter (the converted ArrayList is the one passed from the MainActivity class):
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
ArrayList<String> converted;
ArrayList<View> views = new ArrayList<View>();
LayoutInflater inflater;
public ImageView imgzoomer;
public ViewPagerAdapter(Context context, ArrayList<String> converted) {
this.context = context;
this.converted = converted;
}
#Override
public int getCount() {
return converted.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
if(position==converted.size()-1){
// Declare Variables and Views
// Capture position and set to the ImageView
Bitmap bitmap = BitmapFactory.decodeFile(converted.get(position));
bitmap.recycle();
imgzoomer.setImageBitmap(bitmap);
// Add layout_fullscreen_image.xml to ViewPager
container.removeAllViewsInLayout();
container.removeAllViews();
container.addView(container, position+1);
notifyDataSetChanged();
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables and Views
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.layout_fullscreen_image, container,false);
imgzoomer = (ImageView) itemView.findViewById(R.id.zoomer);
// Capture position and set to the ImageView
Bitmap bitmap = BitmapFactory.decodeFile(converted.get(position));
imgzoomer.setImageBitmap(bitmap);
// Add layout_fullscreen_image.xml to ViewPager
container.addView(itemView, 0);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
container.removeView(imgzoomer);
}
}
The part where I want to refer to the ViewPager again in my MainActivityClass:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get the view
setContentView(R.layout.viewpager_main);
ImageView imgzoomer;
//Doinbackground ASYNCTASK (returns the converted arraylist, which is the location of the pics in the storage)
new XMLParse().execute(url);
//Locate ViewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//PageChangeListener
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
//THIS PART HERE<------------
public void onPageScrolled(int position, float v, int i2) {
if(position>=converted.size()-1){
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
}
I follow some tutorial "How to use GridView in android" and try to make Gallery....
but why?
I already did same with tutorial did....
this file GridActivity.java :
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
}
and this for file ImageAdapter.java :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapater(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId (int position) {
return 0;
}
public View getView(int position, View converView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70,70));
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.image1
};
}
there's no error...
but when I tried to run with emulator...
"Unfotunetly, ........... has stop"
please....help...me..
new ImageAdapter(this) Needs a constructor that takes context as the param.
Change this
public void ImageAdapater(Context c) { //remove void for constructor
to
public ImageAdapater(Context c) {
More info
http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html