i Want to make a slideshow with viewpager where pages are changed automatically every 5 sec. Ive done the viewpager , but got blocked till here , any advise??
Adapter.class (its inner class in my fragment)
class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mResources.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.slide_show_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
}
Here is the function i call in my fragment
public void setSlideShow(){
mCustomPagerAdapter = new CustomPagerAdapter(getActivity());
mViewPager = (ViewPager)rootView.findViewById(R.id.slide_show);
mViewPager.setAdapter(mCustomPagerAdapter);
}
Well, android does not provide any function to automatically flip views inside ViewPager but it provides you with something called TimerTask class which you can use to achieve your goal.
You can look at this link to do it this way.
There is one more easy way to achieve this but for that you will have to drop the idea of using ViewPager. Instead you can use ViewFlipper.
ViewFlipper has its own methods to flip automatically. You just need to add these two lines to make your views automatically flippable:
mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(2000); // flip every 2 seconds (2000ms)
You can get ViewFlipper example here.
Related
I am working on an android application and using viewpager in a view. I am using PagerAdapter to show the views of tabs in viewpager.
I have to add/remove some Tabs dynamically. When I am adding any tab along with it's view then instantiateItem function is being called and tab is getting added successfully. But When I remove any tab from viewpager then destroyItem function should be called but it is not getting called and the view of Tab is not getting removed.
This the code which I am using to add/remove tabs.
public List<View> viewsList = new ArrayList();
public HashMap<Integer, View> GridViewList = new HashMap<>();
MyAdapter myAdapter = new MyAdapter();
viewPager.setAdapter(myAdapter);
viewPager.setOffscreenPageLimit(myAdapter.getCount() + 1);
private class MyAdapter extends PagerAdapter {
public MyAdapter() {
super();
}
#Override
public int getCount() {
return viewsList.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = viewsList.get(position).rootView;
container.addView(v, 0);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View)view);
}
#Override
public boolean isViewFromObject(View view, Object key) {
return key == view;
}
}
I am adding using the below method When I add any Tab along with it's view
View view = new View(); // I am making Custom View here
viewsList.add(view);
GridViewList.put(Id, view);
myAdapter.notifyDataSetChanged();
And When I remove any view, I use below method :
viewsList.remove(GridViewList.get(Id));
GridViewList.remove(Id);
myAdapter.notifyDataSetChanged();
But the destroyItem function is not being called when I remove any Tab and it's view from adapter in android. Please help me if someone have any idea about this.
Thanks a lot in advanced.
Late to the party, but: you need to implement getItemPosition in order for destroyItem to be called. In this case, something like this should work:
#Override public int getItemPosition(Object itemIdentifier) {
int index = viewsList.indexOf(itemIdentifiers);
return index == -1 ? POSITION_NONE : index;
}
I try to implement Fragment into ViewPager to make my app more flexible. My app contains TextView and I have a few phrases that will appear one by one if you swap the page from left to right. For now it works in this way like I posted but I want to add a button or maybe some more function, that's why I want to make it like constructor from Fragments.. I try to change my code watching on this post but can't totally understand how to do it. I created two layouts: one for text view another one to put there fragment. Could any one show me how to do it clear?
public class SwipeAdapter extends PagerAdapter{
private int[] car = {R.string.car1, R.string.car2,
R.string.car3, R.string.car4, R.string.car5};
private Context context;
private LayoutInflater layoutInflater;
public SwipeAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return car.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==(RelativeLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = layoutInflater.inflate(R.layout.carSwipe, container, false);
TextView textView = (TextView) itemView.findViewById(R.id.interTextView);
textView.setText(car[position]);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
You don't need ViewPager with Fragments to Achieve of showing a TextView.
You can try:
In your activity layout xml that you set in content View add a ViewPager with width and height of your preference.
Then you can set the adapter that you have posted above for that ViewPager and it will work.
Alternatively if you still need to inflate Fragments then you need to extend FragmentStateAdapter of FragmentAdapter. That will give you the ability to have in the ViewPager Fragment,
I am doing a slide view , and by mistake I have added the pageview and ViewPager in same xml and that caused problems , when I tried to fix it the slide stoped working , so I need help to make the slide working.( In this tutorials 1 and 2 explained the viewpage should be in another xml but honestly I didnt understand why .
This is SingleViewActivity.java
public class SingleViewActivity extends Activity {
private ImageView image1;
ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
//
slide mCustomPagerAdapter = new slide(this);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(mCustomPagerAdapter);
}}
and this is slide.java
public class slide extends PagerAdapter{
Context mContext;
LayoutInflater mLayoutInflater;
public slide(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.activity_main, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.SingleView);
imageView.setImageResource(mThumbIds[position]);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
public Integer[] mThumbIds =
{
R.drawable.sample2,
R.drawable.sample2,
R.drawable.sample2,
R.drawable.sample2,
R.drawable.sample2,
R.drawable.sample2,
R.drawable.sample2,
R.drawable.sample2
};
}
Single view.xml
<ImageView android:id="#+id/SingleView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittextm"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
As I understand view pager should be in another xml , please note that I want the images to slide in SingleViewactivity.
The ViewPager should be in the main Activity's XML.
The different pages in the ViewPager should be a separate XML layout.
I am in a need of nested viewPagers, I am using a custom vertical viewPager where its child is a viewPager.
My problem is when the user swipes in vertical direction (Y axis) parent viewpager should function and if he swipes in horizontal (X axis) child viewpager has to function.
I came across a lot of tutorial where I can disable either parent's swipe or child's swipe but I dont want to do that, instead according to the swipe direction either parent (vertical) should work and child (horizontal) has to work.
I have also attached parent's viewpager adapter code here
class VerticalPageAdapter extends PagerAdapter
{
Context mContext;
LayoutInflater mLayoutInflater;
public VerticalPageAdapter(Context context)
{
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount()
{
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
mVerticalViewPagerPosition = position;
View itemView = mLayoutInflater.inflate(R.layout.homelayout_verticalpager, container, false);
mPager = (ViewPagerTest)itemView.findViewById(R.id.pager);
mPager.setAdapter(new NewsPageAdapter(mContext));
mPager.setOnTouchListener(gestureListener);
mPager.getParent().requestDisallowInterceptTouchEvent(false);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((LinearLayout) object);
}
}
Please help to sort out this.
I am using PagerSlidingTabStrip to have a ViewPager with tabs in an Android layout.
My ViewPager custom adapter is the following:
private static class SlidingPagerAdapter extends PagerAdapter implements IconTabProvider {
private final List<Tab> mItems;
private final LayoutInflater mInflator;
public SlidingPagerAdapter(Context context, List<Tab> tabs) {
mItems = tabs;
mInflator = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mItems.size();
}
#Override
public boolean isViewFromObject(View v, Object obj) {
return v == (View) obj;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Tab data = mItems.get(position);
View child = mInflator.inflate(data.mLayoutResource, container, false);
container.addView((View) child, 0);
return child;
}
#Override
public int getPageIconResId(int position) {
return mItems.get(position).mIconResource;
}
}
The problem arises when I set a PageTransformer to the ViewPager. As a test I used ZoomOutPageTransformer provided in the Android docs.
When the child view from the ViewPager is first loaded, all works fine. I can click items and the views are updated. But after the first time I scroll through the ViewPager, the child views just stop rendering properly. If I click on an item, I can feel haptic feedback and click sounds but the view remains in its previous state.
I have tried invalidating the View every time there is a change in the ViewPager, but this hasn't helped.
There are similar problems when using a ListView inside a ViewPager, so I tried setting the views that are out of view as GONE, but this hasn't worked either.
I had a similar problem and I was able to fix it by following the answer on this SO question: apply PageTransformer to PagerView as soon as possible Except I used .0001f in fakeDragBy()
Hacky, but it worked.