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:
- Just specify a static height (for example
50dp
) - 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):
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" />
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
What Is the Intent of the Methods Getitem and Getitemid in the Android Class Baseadapter
Lazy Load Images on Listview in Android(Beginner Level)
Detect When Recyclerview Reaches the Bottom Most Position While Scrolling
Compiling the Latest Openssl for Android
Proguard: Can't Find Referenced Class Com.Google.Android.Gms.R
Android O - Old Start Foreground Service Still Working
Android Runonuithread Explanation
The Import Android.Support.V7 Cannot Be Resolved
Android: How to Change Checkbox Size
Tabhost with Fragments and Fragmentactivity
Cancel Notification on Remove Application from Multitask Panel
Set Selected Item in Android Bottomnavigationview
Notificationcompat with API 26
Android - R Cannot Be Resolved to a Variable
Android: Under What Circumstances Would a Dialog Appearing Cause Onpause() to Be Called
Select + Copy Text in a Textview