I have struggled a lot with my ViewPagerAdapter for remove/disable pages. But nothing happens. Is there any ways to remove pages from a ViewPagerAdapter? Here is my code-
As you can see, I have three positions there. I just want to remove/disable position 1(paragraph 1).
public class ViewPagerAdapter2 extends PagerAdapter
{
private Context context;
private LayoutInflater layoutInflater;
String strings[] = {
"paragraph 0",
"paragraph 1",
"paragraph 2"
};
public ViewPagerAdapter2 (Context context) {
this.context = context;
}
#Override
public int getCount()
{
// TODO: Implement this method
return strings.length;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
// TODO: Implement this method
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.view_pager_lyrics,null);
final TextView textView = view.findViewById(R.id.view_pager_lyricsTextView);
textView.setText(Html.fromHtml("<!DOCTYPE html><html><head></head><body><div style='text-align: center'><p>"+strings[position]+"</p></div></body></html>"));
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
Related
I have implemented one view pager inside other. Requirement was to have tabs on parent view pager and show its item on child view pager. Example, parent view pager has two tabs (episode, videos) and we have number of episodes and vidoes(child view pager) which need to be displayed in respective tabs. Problem is when I swipe from the edges of the screen, it changes the tab even though current tab has more item to load. But it works fine when I swipe from center of the screen and shows next episode when i am on episode tab.
Here is a code :
public class ParentPagerAdapter extends FragmentStatePagerAdapter {
private List<String> tabList;
private boolean isPilot;
private ArrayList<?extends IVideoModel> videoList;
private ArrayList<?extends IEpisodeAndVideoModel> episodeList;
public ParentPagerAdapter(FragmentManager fm, List<String> tabList,
ArrayList<?extends IEpisodeModel> episodeList, ArrayList<?extends IVideoModel>
videoList) {
super(fm);
this.episodeList = episodeList;
this.videoList = videoList;
this.tabList = tabList;
}
#Override
public Fragment getItem(int position) {
Fragment fragment;
Bundle bundle = new Bundle();
if(tabList.get(position).contains(VIDEOS_TAB_TITLE)){
fragment = new VideoTabFragment();
return fragment;
}else if(tabList.get(position).contains(EPISODES_TAB_TITLE)){
fragment = new EpisodeTabFragment();
return fragment;
}
return null;
}
#Override
public int getCount() {
return tabList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabList.get(position);
}
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
}
Child view Pager 1:
public class ProjectVideoPagerAdapter extends PagerAdapter {
public final String TAG = ProjectVideoPagerAdapter.class.getCanonicalName();
private Context mContext;
private LayoutInflater mLayoutInflator;
private RowProjectLandingPagerItemBinding itemBinding;
private List<? extends IVideoModel> projectVideoList;
public ProjectVideoPagerAdapter(Context context, List<? extends IVideoModel> projectVideoList) {
mContext = context;
this.projectVideoList = projectVideoList;
mLayoutInflator = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
debugLog("Entering instantiateItem with position = " + position);
itemBinding =
DataBindingUtil.inflate(mLayoutInflator, R.layout.row_project_landing_pager_item, null,
false);
if (projectVideoList == null) return null;
container.addView(itemBinding.getRoot());
debugLog("Exiting instantiateItem()");
return itemBinding.getRoot();
}
#Override
public int getCount() {
if (projectVideoList != null && !projectVideoList.isEmpty()) return projectVideoList.size();
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((View)object);
//super.destroyItem(container, position, object);
}
}
Child view pager 2:
public class ProjectVideoPagerAdapter extends PagerAdapter {
public final String TAG = ProjectVideoPagerAdapter.class.getCanonicalName();
private Context mContext;
private LayoutInflater mLayoutInflator;
private RowProjectLandingPagerItemBinding itemBinding;
private List<? extends IEpisodeModel> projectEpisodeList;
public ProjectVideoPagerAdapter(Context context, List<? extends IEpisodeModel> projectList) {
mContext = context;
this.projectEpisodeList = projectList;
mLayoutInflator = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
debugLog("Entering instantiateItem with position = " + position);
itemBinding =
DataBindingUtil.inflate(mLayoutInflator, R.layout.row_project_landing_pager_item, null,
false);
if (projectEpisodeList == null) return null;
container.addView(itemBinding.getRoot());
debugLog("Exiting instantiateItem()");
return itemBinding.getRoot();
}
#Override
public int getCount() {
if (projectEpisodeList != null && !projectEpisodeList.isEmpty()) return projectEpisodeList.size();
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == (object);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((View)object);
//super.destroyItem(container, position, object);
}
}
With inner view pager, you should use "childfragmentmanager" instead of "fragmentmanager". It works fine.
I have a pageradapter that i am using to display a slideshow of images but it crashes when i set a lower off screen limit.
Here is my code
public class ProjectPicturesAdapter extends PagerAdapter {
private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;
public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
this.pictures = pictures;
this.glide = glide;
this.inflater = inflater;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View image = inflater.inflate(R.layout.slider_image, container, false);
ImageView myImage = (ImageView) image.findViewById(R.id.image);
glide.load(pictures.get(position))
.centerCrop()
.crossFade()
.placeholder(R.drawable.project_placeholder)
.into(myImage);
container.addView(myImage);
return myImage;
}
#Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
#Override
public int getCount() {
return pictures.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
The way i am initializing the adapter is this
ProjectPicturesAdapter picturesAdapter = new
ProjectPicturesAdapter(Glide.with(this), LayoutInflater.from(this),
images);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(picturesAdapter);
pager.setOffscreenPageLimit(1);
If there are more than 2 images then it crashes,i could always increase the off screen limit but what happens if there are 20 pictures,i cannot keep the offscreen limit as 20.
My crash log is gives an error saying The specified child already has a parent. You must call removeView() on the child's parent first.
Update ProjectPicturesAdapter as below:
public class ProjectPicturesAdapter extends PagerAdapter {
private List<String> pictures;
private LayoutInflater inflater;
private final RequestManager glide;
public ProjectPicturesAdapter(RequestManager glide, LayoutInflater inflater, List<String> pictures) {
this.pictures = pictures;
this.glide = glide;
this.inflater = inflater;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.slider_image, container, false);
ImageView myImage = (ImageView) view.findViewById(R.id.image);
glide.load(pictures.get(position))
.centerCrop()
.crossFade()
.placeholder(R.drawable.project_placeholder)
.into(myImage);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
#Override
public int getCount() {
return pictures.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
If your aim is to show the carousel?
Just use this library and your problem will be solved https://github.com/sayyam/carouselview
The problem is caused by this:
#Override
public void destroyItem(ViewGroup container, int position, Object view)
{
container.removeView((View) view);
}
I recommend to use FragmentStatePagerAdapter instead of PagerAdapter.
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
return ArrayListFragment.newInstance(position);
}
}
mAdapter = new MyAdapter(getFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
I have a regular ViewPager with an adapter:
public class MyDialogFragment extends DialogFragment {
private ViewPager viewPager;
private MyViewPagerAdapter myViewPagerAdapter;
private City city;
public static MyDialogFragment newInstance() {
MyDialogFragment fragment = new MyDialogFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
viewPager = (ViewPager) v.findViewById(R.id.viewPager);
city = Parcels.unwrap(getArguments().getParcelable("city"));
myViewPagerAdapter = new MyViewPagerAdapter(getContext());
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// Page changed
}
});
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class MyViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
public MyViewPagerAdapter(Context context) {
this.context = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.city_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.cityImage);
Glide.with(context)
.load(city.getImage())
.into(imageView);
}
container.addView(view);
return view;
}
#Override
public int getCount() {
return city.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);
}
}
}
As you can see, I have a page change listener set on my ViewPager. But I need to be able to notify the adapter when the page has changed so that I can modify the adapter's ImageView. How can I do this?
Add a method to your adapter class
public class MyViewPagerAdapter extends PagerAdapter {
...
public void pageChanged(int position) {
// Do something
}
...
}
After that in your OnPageChangeListener use that method
#Override
public void onPageSelected(int position) {
myViewPagerAdapter.pageChanged(position);
}
One approach is to create a data structure in your adapter to map the pager view to its position. A SparseArray is most efficient:
private SparseArray<View> pageMap = new SparseArray<>();
Then in the adapter's instantiateItem() method, add the view to the SparseArray:
public Object instantiateItem(ViewGroup container, int position) {
...
pageMap.put(position, view);
...
}
Now in your OnPageChangeListener you can do the following:
#Override
public void onPageSelected(int position) {
View view = adapter.getView(pos);
if (view != null) {
ImageView iv = (ImageView) view.findViewById(R.id.your_image_view);
// You now have the image view
}
}
Make sure you remove views from the SparseArray in the adapter's destroyItem() method:
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
pageMap.remove(position);
...
}
You can use the constructor with imagelist to pass and that automatically called on next page..In your code will be modified like :::
ArrayList imageList=new ArrayList<>();
imageList.add("ImagePath here");
myViewPagerAdapter = new MyViewPagerAdapter(getContext(),imageList);
viewPager.setAdapter(myViewPagerAdapter);
Then your adapter will be like ::
public class MyViewPagerAdapter extends PagerAdapter {
private Context context;
private ArrayList<String> imageList
private LayoutInflater layoutInflater;
public MyViewPagerAdapter(Context context,ArrayList<String> imageList) {
this.context = context;
this.imageList = imageList;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.city_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.cityImage);
Glide.with(context)
.load(imageList.get(position)) // This will be changes like
.into(imageView);
}
container.addView(view);
return view;
}
#Override
public int getCount() {
return city.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);
}
}
That done ..
After this, there are some suggestion that
- please use arraylist while using the viewpager ..
Now you just taking the parceble object from bundle..
you can do the same for the arraylist,
just receive the arraylist from the same bundle by passing the whole arraylist at once.
so you dont need to find the page change event..viewpager will automatically handle that..
Here's How to send & receiving the parceable arraylist
// while sending the list
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("citylist", new ArrayList<City>());
// while receiving the list
ArrayList<City> citylist = getIntent().getParcelableArrayListExtra("citylist");
After this just get the all image and add in the imagelist array
for (int i = 0; i < citylist.size(); i++) {
imageList.add(citylist.get(i).getImage());
}
And pass this imageList in
myViewPagerAdapter = new MyViewPagerAdapter(getContext(),imageList);
viewPager.setAdapter(myViewPagerAdapter);
Tell me if needed more assitance.
My code for this class is unable to show view at the 0th position and is also showing irrelevant data sometime. This view pager is attached with a tablayout.
I want to know why this is happening and if there is some other way to handle viewpager extending PagerAdapter.I will be happy if I will come to know about some another class relevant to PagerAdapter but without using Fragment.
public class JobcardListViewPagerAdapter extends PagerAdapter implements IWebServiceAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
List<TabLayoutDetails> tabLayoutDetailsList;
ActiveJobCardListResponse activeJobCardListResponse;
Activity activity;
View itemView;
WebService webService;
public JobcardListViewPagerAdapter(Context context, List<TabLayoutDetails> tabLayoutDetailsList) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.tabLayoutDetailsList = tabLayoutDetailsList;
webService = new WebService();
}
public JobcardListViewPagerAdapter() {
}
#Override
public int getCount() {
return tabLayoutDetailsList.size(); // value of tabLayoutDetailsList.size() returning 10;
}
#Override
public CharSequence getPageTitle(int position) {
return tabLayoutDetailsList.get(position).getDescription();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
itemView = mLayoutInflater.inflate(R.layout.active_job_card_list_view_pager_adapter, container, false);
GetActiveJobCardList(position);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
Similar question Access ViewPager views from onCreate?
I created a ViewPager where different layout can moves around. One layout has a button. My question is if a user click on this button, he will move another page. How to achieve it. Here is the viewpager adapter class:
public class SwipeAdapter extends PagerAdapter {
private LayoutInflater mInflater;
private static int[] mLayouts = { R.layout.view_layout1,
R.layout.view_layout2, R.layout.view_layout3, R.layout.view_layout4 };
SwipeAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ViewGroup pageView= (ViewGroup) mInflater.inflate(mLayouts[position],
container, false);
container.addView(pageView);
getItemPosition(pageView);
return pageView;
}
/**
* #return the mCurrentposition
*/
#Override
public int getCount() {
return mLayouts.length;
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
}