I have a problem that, I have to implement horizontal swipe view as a paging system and each page shows data from the server, means each page shows dynamic data at run time. I think it is possible with View Pager but in View Pager there is no any fixed layout which is shown on particular page, So, firstly how can we do that, secondly in View Pager all views should be predesigned or preimplemented or we can say that Number of Views we must know. But when we want to take it dynamic when user swipe a page then it goes to next page but progressbar will appear to show the data for corresponding page. So, how can we do this also? Please suggest me any solution regarding the same.
View Pager:
View Pager Demohttp://android-developers.blogspot.in/2011/08/horizontal-view-swiping-with-viewpager.html
Thanks in advance.
Have you tried to implement
viewPager.setOnPageChangeListener(this);
to detect page changement and so loading the page content
The solution isn't in ViewPager but in your custom PagerAdapter. You must subclass the PagerAdapter and add a method to add content to your adapter and call in this method the method notifyDataSetChanged :
public void addPage(Object page) {
// Add page to the arraylist of adapter
notifyDataSetChanged();
}
Related
I am trying to create a main page in my android app in which there are multiple things a user can select to do. For this page, I am thinking of using a RecyclerView with a LinearLayoutManager. For each of the views in the RecyclerView (none of which are as big as the screen), I want a user to be able to swipe into a Fragment (which is as big as the screen).
I have already tried using multiple ViewPagers to solve this problem. When keeping the ViewPager as big as the screen (the default), the Fragment would be the correct size, except the view in the RecyclerView would be as big as the screen (I want it to only wrap_content). When shrinking the size of the ViewPager, the Fragment would not be as big as the screen, but the View in the RecyclerView would be the correct size.
I have heard that one can listen for a user's swipe on a view, except:
- I don't know if that is what I should do in my case, and
- I don't know how to do it for my situation
If listening for a user's swipe is the correct way to go, please give some direction and/or code on how to complete what I want to do. If it is not the way to go, please provide an alternate way.
Thanks in advance!
Update: Here is a picture describing what I mean:
// define your fragment with recycle view
class FragmentA extends Fragment {
// with recycler view
// in click on recycler item method as i posted
// 1) do check if pager fragment exist in pager adapter
// 2) if not create one
// 3) and move to it using pager method
}
// define second without
class FragmentB extends Fragment {
// without
}
// create pager
ViewPager vp = new ViewPager(Context);
// create list with fragments for pager adapter
// when you will swipe you will move from A to B
List<Fragment> // add FragmentA + FragmentB
// https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
// create pager adapter - implement own or user FragmentPagerAdapter
PagerAdapter ... = new MyPagerAdapter(List<Fragment>);
// set view pager with adapter
viewPager.setAdapter(PagerAdapter);
// add view pager to activity view - or some content view
I already tried this on my own; the viewpager would not allow for the
correct sizes. Either the full-screen fragment size was shrunk down to
the size of the View in the recyclerview, or the view in the
recyclerview was full-screen. – Flare Cat
its working:
Oh wait, read the code wrong. But still, I need to base FragmentB off
of what was swiped in FragmentA – Flare Cat
solution:
when you click on recycler item dynamic create new fragmentB and call move to fragmentB on pager (before create new one do a check if fragment not exist already!)
or if you don't use any click on item method but just constant recycler item content create such fragments corresponding to each entry in recycler
or create some sort of map which will map each entry in recycler to to its corresponding fragment content
As I understand it, I would need to create an onClickListener for each
view in the RecyclerView. But, would that onClickListener be called
when a user swipes the corresponding view? – Flare Cat
Actually, this might work with an onTouchListener. I'll have to try it out. – Flare Cat
once again:
it depends what u want to achieve - if recycler is a way of brief for each fragments and you know the content and want to present each entry for recycler item counting from top to bottom then you need add those fragments one by one in same order from left to right
#FlareCat but why using pager here ? not better to open new activity or add fragment with transition on recycler item click ?
I have a AsyncTask to get the web service data through JSON. I would like to have these items on pages with ViewPager and textsViews. My problem is how to do this without fragments, want it to be dynamic.
I do not understand very well where I put the setViewAdapter would be in onPostExecute?
To view the pager follow the following links:
View Pager Link 1
View Pager Link 2
I'm having trouble aligning these two things. Please help me!! Maybe my question was not clear.
Thank you.
What's problem while using fragments? They are just elements of ViewPager. So, create fragment which is consisted of textview(s).
To reload ViewPager use yourpager.setAdapter(adapter) inside onPostExecute() block.
That is , pipeline:
initialize ViewPager
Download data
Reload ViewPager's elements.
Hi,
I implemented sliding tab layout using Google's SlidingTabLayout. The position of tab changes on swiping View pager , but View pager does not refresh on scrolling Tabs like the way we see in contacts application where the user scrolls the tab and the View pager corresponding to particular tab also changes . Is there a way in which we can customize this in SlidingTablayout ? can anyone help me in sorting out this issue ?
Thanks in advance.
You need to update content of fragment in onResume to ensure that content is updated.
while using View pager, I face a strange problem.
In my project i have three fragment in a view pager, but while loading the second fragment by swiping from left to right , the OnCreate of third Fragment is automatically calling.in onCreate method i called an Api for getting some data.hence the Api is also called.Normal scenario is like that it only called while loading third fragment by swiping from left to right.
Thanks in Advance
View pager works same as listview.
It have same buffer of fragments to increase the UI experience.
so it creates fragments in advance and show to he user when asked.
so use http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html
for your purpose.
Thanks
Based on FragmentPagerAdapter, I have inflated my view pager and I can see my items in each page. How to know, which page is clicked by user to direct him/her see detail of information?
Thanks
As far as I know, there is no way to get the current displayed Fragment by the FragmentPagerAdapter, so what I can suggest is keep an position member variable for each Fragment that is added in the pager so you can use that to know which fragment is clicked.