How to Use View Pager with Views (Not with Fragments)

Can I use view pager with views (not with fragments)

You need to override these two methods rather than getItem():

@Override
public Object instantiateItem(ViewGroup collection, int position) {
View v = layoutInflater.inflate(...);
...
collection.addView(v,0);
return v;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}

Saving the state of a viewpager using views as pages and not fragments

I was able to restore the state of my viewpager including it's current page when screen orientation changed by saving my arraylist and current position in

onSaveInstanceState method. later on i get the arraylist and position from the bundle in method onCreate(Bundle savedInstanceState)

public class myActivity extends AppCompactActivity{

private ArrayList<ImagePaths> listviewArray;
private int savedImagePosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);

if(savedInstanceState != null){
listviewArray = savedInstanceState.getParcelableArrayList("image_array");
savedImagePosition = savedInstanceState.getInt("savedImagePosition");

}else{
listviewArray = new ArrayList<>();
savedImagePosition =0;
}

ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);

ViewPagerAdapter adapter = new ViewPagerAdapter(this,listviewArray);

viewpager.setAdapter(adapter);
viewpager.setCurrentItem(savedImagePosition,true);
}

@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("image_array", listviewArray);
outState.putInt("savedImagePosition",savedImagePosition);

}

}

my viewpager adapter

public class ViewPagerAdapter extends PagerAdapter {

private Context context;
private LayoutInflater inflater;
private ArrayList<ImagePaths> imagesArray;

public ViewPagerAdapter(Context context,ArrayList<ImagePaths> array, ){
this.context = context;
inflater =LayoutInflater.from(context);
imagesArray = array;
}

public int getCount(){
return imagesArray.size();
}

public boolean isViewFromObject(View view,Object object){
return view == object;
}

public Object instantiateItem(ViewGroup viewGroup, int position){
View view = inflater.inflate(R.layout.photo_layout, viewGroup, false);
ImageView imageView =(ImageView) view.findViewById(R.id.photoView);

final String imagePath = imagesArray.get(position);

ImageLoader.getInstance().displayImage(imagePath, imageView);

viewGroup.addView(view);
return view;
}

public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((LinearLayout) object);
}
}

ViewPager of views (not fragments) as header of a gridview (Etsy's StaggeredGridView) in a fragment

Reason:

Basically the issue is caused by ViewPager android:layout_height="match_parent".

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

The match_parent is wrong here for sure, because you don't want the header to fill entire parent. But regardless of that - match_parent will not work here in StaggeredGridView.

This is the same as in standard ListView items etc. - match_parent is not allowed and it will be replaced with just wrap_content while performing layout.
Unfortunately wrap_content is not supported by ViewPager because ViewPager cannot wrap it's content (it is not aware of children size).

Solution:

You have two ways to properly display a ViewPager there:

  1. Just specify a static height (for example 50dp)
  2. Extend your ViewPager and perform your own measuring if you don't want static height.

Screenshots

Here is also a screenshots with match_parent. You can see that ViewPager is nor displayed (has zero height):

enter image description here

and with static height:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="50dp" />

enter image description here

Using page adapter without fragments

Yes its totally possible. Since ViewPager.setAdapter() takes any instance PageAdapter, you just need to create your own subclass of PagerAdapter (its kinda like subclassing BaseAdapter or ArrayAdapter that you use in a ListView). just remember, for PAgerAdapter the docs say:

When you implement a PagerAdapter, you must override the following
methods at minimum:

  • instantiateItem(ViewGroup, int)
  • destroyItem(ViewGroup, int, Object)
  • getCount()
  • isViewFromObject(View, Object)

Hope this helps

View Pager without Fragment not working

You are setting ur activity layout as activity_route_details.xml which does not contain viewpager item but pager_route_details

In RouteDetails.java change this line

setContentView(R.layout.activity_route_details);

with

setContentView(R.layout.route_details);

How to initialize my fragment in the main Activity, using ViewPager

It is very Important to use RecycleView instead of ListView when you have many Fragment and that fragment contains ListView.



Related Topics



Leave a reply



Submit