I want to use a viewpager to display information from a stack of objects.
I have a viewpager with 4 fragments and I want that every time I swipe the viewpager to another fragment the non visible fragments pop/peek an object from the stack.
I already tried some stuff. I was trying to listen to page changes using OnPageChangeListener()and find some way to update the fragment content from inside the activity. I didn't manage to do that.
Here is my adapter:
public class PagerAdapter_Browse extends FragmentPagerAdapter {
FragmentManager mManager;
public PagerAdapter_Browse(FragmentManager fm) {
super(fm);
mManager = fm;
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new FragmentBrowseOne();
case 1:
return new FragmentBrowseTwo();
case 2:
return new FragmentBrowseThree();
case 3:
return new FragmentBrowseFour();
default:
break;
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
One of my fragments (all look more a less the same):
public class FragmentBrowseOne extends Fragment {
ImageView main_img;
View view;
public FragmentBrowseOne(){
setArguments(new Bundle());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
BitmapDrawable profilePic;
view = inflater.inflate(R.layout.fragment_browse_one, container, false);
main_img = (ImageView) view.findViewById(R.id.main_img);
Bitmap temp = bundle.getParcelable("object");
profilePic = new BitmapDrawable(getResources(), temp);
main_img.setBackground(profilePic);
main_img.setScaleType(ImageView.ScaleType.CENTER_CROP);
return view;
}
}
And here is part of my activity:
viewpager = (ViewPager) findViewById(R.id.pager_browser);
padapter = new PagerAdapter_Browse(getSupportFragmentManager());
PagerAdapter wrappedAdapter = new InfinitePagerAdapter(padapter);
viewpager.setAdapter(wrappedAdapter);
viewpager.setId(R.id.pager);
viewpager.setOffscreenPageLimit(1);
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
FragmentBrowseOne fragment = new FragmentBrowseOne();
Bitmap pic = BitmapFactory.decodeResource(getResources(), R.mipmap.samplegirl4);
fragment.getArguments().putParcelable("object", pic);
}
});
Use SetArguments and pass it in bundle for every fragment. Arguments must contain new object in which required changes are made.
Use constructor
public FragmentNumber(){
setArguments(new Bundle());
}
Return Fragment by using below method
FragmentNumber fragment = new FragmentNumber();
fragment.getArguments().putSerializable("object", newobject);
This will refresh data inside fragment whenever page changed.
Related
I am trying to refresh the current fragment in the tabbed layout of my viewpager. I have one fragment (UserProgressFragment) that creates the ViewPager and sets the adapter (UserProgressAdapter) along with creating tabbed layout.
In my UserProgressAdapter, in the getItem() method I am returning two other fragments, (UserWeightTrackerFragment and UserCalorieCounterFragment) based on which tab i am on.
My issue is how do i refresh the fragment and update its content/view from the UserCalorieCounterFragment on a button click, because access to the viewpager and adapter are set in the UserProgressFragment? I have considered notifyDataChange for the adapter but i dont know how to call this from this class as it is set up on the UserProgressFragment class. The purpose of wanting to refresh this page is to update a specific view which is a chart, if context is needed.
I have attached the code below:
UserProgressFragment
public class UserProgressFragment extends Fragment{
public static UserProgressFragment newInstance() {
return new UserProgressFragment();
}
public UserProgressFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_progress, container, false);
ViewPager viewPager = view.findViewById(R.id.progress_viewpager);
viewPager.setAdapter(new UserProgressTabsAdapter(getChildFragmentManager()));
TabLayout tabLayout = view.findViewById(R.id.progress_sliding_tabs);
tabLayout.setupWithViewPager(viewPager, true);
return view;
}
}
UserProgressTabsAdapter
public class UserProgressTabsAdapter extends FragmentStatePagerAdapter {
private static final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Weight Tracker", "Calorie Counter"};
public UserProgressTabsAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return UserWeightTrackerFragment.newInstance(position);
case 1:
return UserCalorieCounterFragment.newInstance(position + 1);
}
return null;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
UserCalorieCounterFragment (need to refresh this one)
public class UserCalorieCounterFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
public static final String ARG_PAGE = "ARG_PAGE";
public static UserCalorieCounterFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
UserCalorieCounterFragment fragment = new UserCalorieCounterFragment();
fragment.setArguments(args);
return fragment;
}
public UserCalorieCounterFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_calorie_counter, container, false);
Button mAddCalories = view.findViewById(R.id.btn_add_calorie);
mAddCalories.setOnClickListener(this);
return view;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_add_calorie:
//REFRESH/UPDATE HERE
break;
}
}
You can create a public method in your fragment which contains the view pager which will contain the notifyDataSetChanged() function. And in the view pager fragment/item, you can call getParentFragment() and type cast it to your parent/first fragment and access that public method to notifyDataSetChanged().
I am pretty new to ViewPager and adapter world in android. I have seen this issue asked many times but did not quite understand so hoping for a simplified answer :).
I have a ViewPager with 3 tabs and each tab has a fragment. By default the user first sees tab2 where he gives some imput to generate a string. this string then needs to be taken in tab3 which generates a qr code from it.
the string from tab 2 is updated to a class and tab3 takes the string from there.
My Problem is that when i move from tab2->tab3 the qr code is not generated. But when i go tab2->tab1->tab3 it is generated. I am guessing it retains my old fragment .
Below are my class codes. Could you please let me know why is this viewpager behaviour so.
my FragementActivity class:
public class ConfigExchangeMain extends FragmentActivity {
FragmentPagerAdapter adapterViewPager;
SampleFragementPagerAdapterExchange adapter;
String sent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config_exchange_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
adapter=new SampleFragementPagerAdapterExchange(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(1);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
}
}
My Pager Adapter looks like
public class SampleFragementPagerAdapterExchange extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[]{"CONTACTS", "HOME", "QR+SCAN"};
private ConfigDetail context;
public SampleFragementPagerAdapterExchange(FragmentManager fm) {
super(fm);
//this.context = context;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 1:
return new ConfigExchangeNFR();
case 0:
return new ConfigExchangeHome();
case 2:
return new ConfigExchangeQR();
default:
return new ConfigExchangeQR();
}
}
#Override
public int getCount() {
return PAGE_COUNT;
}
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
and my fragment in tab3 that is not updating:
public class ConfigExchangeQR extends Fragment {
ImageView iQRCode ;
int iQRDimension;
Button bScan;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
View view1 = inflater.inflate(R.layout.config_exchange_qr, container, false);
iQRCode = (ImageView)view1.findViewById(R.id.qrCode);
iQRDimension = 900;
String FinalQRCode = KeyValueDb.getPrefFinalVCard(getActivity());
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(FinalQRCode, null,
contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), iQRDimension);
try {
Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
iQRCode.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
bScan = (Button)view1.findViewById(R.id.scan_qr);
bScan.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramAnonymousView) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
});
return view1;
}
}
In ConfigExchangeMain class:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Fragment fragment= adapter.getItem(position);
if(fragment instanceof ConfigExchangeQR ){
((ConfigExchangeQR)fragment).update();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
#Override
public void onDestroy() {
super.onDestroy();
viewPager.clearOnPageChangeListeners();
}
Add this method to your ConfigExchangeQR fragment
public class ConfigExchangeQR extends Fragment {
...
public void update(){
String FinalQRCode = KeyValueDb.getPrefFinalVCard(getActivity());
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(FinalQRCode, null,
contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), iQRDimension);
try {
Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
iQRCode.setImageBitmap(bitmap);
} catch (WriterException e) {
}
}
}
When you are currently in your second tab. The first and the third tab are preloaded (because the animation is smoother). When you switch to the first tab the third tab is unloaded -> so after switching back to the third tab it is recreated and it works for you.
The 3rd tab onCreateView is called before the 2nd tab is done, thats why it is not working. Try implementing logic inside the onResume() - to make sure it is always called when the focus is back on the fragment.
Hope this helps...
Cheers
I am using ViewPager with my fragments. All fragments has animation and it does not work when switching back maybe because ViewPager saves somehow state of near Fragments, do you have some idea how to avoid it?
getItemPosition() doesn't make anything in this case
private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return null;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return GetStartedOneFragment.newInstance();
case 1:
return GetStartedTwoFragment.newInstance();
case 2:
return GetStartedThreeFragment.newInstance();
case 3:
return GetStartedFourFragment.newInstance();
case 4:
return GetStartedFiveFragment.newInstance();
case 5:
return GetStartedSixFragment.newInstance();
default:
return GetStartedOneFragment.newInstance();
}
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
I used a Fragment as a slider content, see the sample code below. Feel free to ask more details if required.
I have 4 flight modes in the example. The ViewPager is used with the associated adapter (ScreenSlidePagerAdapter).
You can see the app in the Play Store to see the slider (search "Flight Recorder 24").
// In the main fragment java code
private View headerView;
private ViewPager mPager;
public ScreenSlidePagerAdapter mPagerAdapter;
// ...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View mainView = inflater.inflate(R.layout.monitoring_fragment,container, false);
// The header of the list
View view = inflater.inflate(R.layout.header_monitoring_fragment, mListView, false);
headerView = view;
//...
mPager = (ViewPager) view.findViewById(R.id.pager);
FragmentManager fm = getChildFragmentManager();
mPagerAdapter = new ScreenSlidePagerAdapter(fm);
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(UserConfiguration.getUserConf().getPositionFromFlightMode());
// Attach the page change listener inside the activity
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
// This method will be invoked when a new page becomes selected.
#Override
public void onPageSelected(int position)
{
int flight_mode = UserConfiguration.getFlightModeFromPosition(position);
UserConfiguration.getUserConf().setFlightMode(flight_mode);
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
// Code goes here
}
// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
#Override
public void onPageScrollStateChanged(int state)
{
// Code goes here
}
});
// ...
}
// ...
// Slider adapter
private class ScreenSlidePagerAdapter extends FragmentPagerAdapter
{
public ScreenSlidePagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
// position to flight mode
int flight_mode = UserConfiguration.getFlightModeFromPosition(position);
FlightModeFragment flightModeFragment = FlightModeFragment.newInstance(flight_mode);
return flightModeFragment;
}
public int getItemPosition(Object object)
{
return POSITION_NONE;
}
#Override
public int getCount()
{
return 4;
}
}
Then you have to create a Fragment with your slider content (here the class FlightModeFragment), it will be instanciated with an integer to identify the page embedded into a Bundle savedInstanceState (I have 4 pages).
public class FlightModeFragment extends Fragment
{
LinearLayout mainLayout;
ImageView live15minImageView;
ImageView modeImageView;
ImageView imageViewLeft;
ImageView imageViewRight;
private TextView textViewRecord;
private TextView textViewGps;
int flight_mode = UserConfiguration.AC_LISTENER_TRAVEL_RECORDER_MODE;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// handle fragment arguments
Bundle arguments = getArguments();
if(arguments != null)
{
flight_mode = arguments.getInt("flight_mode");
}
}
// ...
}
I am using PagerSlidingTabs. I have two fragment and each fragment has one listview. I added TabListener, because i want to go first item in listview when user reselect the same tab. This is working only on the second fragment(tab). Problem is second listview's smoothScrollToPosition(0) method working but first listview's smoothScrollToPosition(0) method(all methods) not working. First listview is showing properly on screen but it's id seems to second listview's id.
PagerAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
private Context mContext;
private int mPageCount;
public PagerAdapter(Context context, FragmentManager fm, int pageCount) {
super(fm);
this.mContext = context;
this.mPageCount = pageCount;
}
#Override
public int getCount() {
return mPageCount;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return Fragment1.newInstance(position);
case 1:
return Fragment2.newInstance(position);
default:
return null;
}
}}
Fragment1:
public class Fragment1 extends Fragment {
private static final String ARG_POSITION = "position";
private ListView mListView1;
private ListAdapter mAdapter;
public static Fragment1 newInstance(int position) {
Fragment1 f = new Fragment1();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mRootView == null) {
mRootView = inflater.inflate(R.layout.fragment1, container, false);
mListView1 = (ListView) mRootView.findViewById(R.id.listview1);
.
.
if (mAdapter == null) {
mAdapter = new ListAdapter(getActivity(), mListView1, R.layout.list_item, mDatas);
mListView1.setAdapter(mAdapter);
} else {
mAdapter.notifyDataSetChanged();
}
.
.
((HomeActivity) getActivity()).getPagerSlidingTabStrip().setOnTabListener(new TabListener() {
#Override
public void onTabReselected(View tab, int postion) {
mListView1.smoothScrollToPosition(0);
//mListView1's id seems to mListView2's id
//when reselect first tab, application acting like i reselect second tab.
}
});
} else {
((ViewGroup) mRootView.getParent()).removeView(mRootView);
}
return mRootView;
}}
Fragment2:
public class Fragment2 extends Fragment {
private static final String ARG_POSITION = "position";
private ListView mListView2;
private ListAdapter mAdapter;
public static Fragment2 newInstance(int position) {
Fragment2 f = new Fragment2();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mRootView == null) {
mRootView = inflater.inflate(R.layout.fragment2, container, false);
mListView2 = (ListView) mRootView.findViewById(R.id.listview2);
.
.
if (mAdapter == null) {
mAdapter = new ListAdapter(getActivity(), mListView2, R.layout.list_item, mDatas);
mListView2.setAdapter(mAdapter);
} else {
mAdapter.notifyDataSetChanged();
}
.
.
((HomeActivity) getActivity()).getPagerSlidingTabStrip().setOnTabListener(new TabListener() {
#Override
public void onTabReselected(View tab, int postion) {
mListView2.smoothScrollToPosition(0); //this line is working fine
}
});
} else {
((ViewGroup) mRootView.getParent()).removeView(mRootView);
}
return mRootView;
}}
Fragment1 and Fragment2 have only listview(id:listview1, id:listview2).
HomeActivity have PagerSlidingTabs view and viewpager.
How can i fix this problem?
When you set the listener from the second fragment, that replaces the listener from the first. That's why all the calls seem to be going to the second tab: its listener was the last one added, so it's receiving all the callbacks. One potential solution is to declare an interface, let's call it OnTabReselctedListener, that has a method onTabReselected(into position). Have your activity maintain a list of those interfaces, have your fragments implement that interface, and register those fragments as listeners in the activity. When the activity receives a reselection event, it can pass that to its children fragments; those fragments check if the position matches their own position and scroll to the top if they do. I hope that made sense; I'm writing this from my phone so I can't easily wrote sample code. If you want me to write some, let me know!
I'm working with viewpager inside fragments. I'm using pagerAdapter to switch between two pages via XML. Here is the code:
public class DemoPagerAdapter extends FragmentPagerAdapter {
public DemoPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
Fragment f;
switch (index) {
case 0:
f = new FirstFragment();
break;
case 1:
f = new SecondFragment();
break;
}
return f;
}
#Override
public int getCount() {
return 2;
}
}
And I'm calling it like this:
public class DemoFragment1 extends Fragment {
private ViewPager viewPager;
private DemoPagerAdapter mAdapter;
public DemoFragment1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.security, container, false);
mAdapter = new DemoPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
showPages(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
return rootView;
}
Everything works fine. But if I move to DemoFragment2 and then come back to DemoFragment1, PagerAdapter is not called.
Your ViewPager is nested in a Fragment, you should use the ChildFragmentManager, modify:
mAdapter = new DemoPagerAdapter(getChildFragmentManager());
I also assume the ViewPager will keep the off screen fragment in memory, thus it might not call getItem(int index) again. Add
viewPager.setOffscreenPageLimit(0);
return rootView;
Well, I encontered this issue last week.
When I put the ViewPager in the fragment with a FragmentPagerAdapter, after I move forward to anoter Fragment and back, the pager' s view not exists.
I supposed is the FragmentPagerAdapter won' t retain its view in the ViewPager ' s host Fragment, Their life cycle is not the same. Though you put the VewPager' s host Fragment in the back stack, but the FragmentPagerAdapter' s fragment not. And Then, I gave up FragmentPagerAdapter and tried android.support.v4.view.PagerAdapter, (note, it wont' t use fragment to inject view!), everything is fine!
below is the sample:
private final PagerAdapter coverPager = new PagerAdapter() {
private LayoutInflater mLayoutInflater;
#Override
public int getCount() {
return 2;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (mLayoutInflater == null) {
mLayoutInflater = LayoutInflater.from(getActivity());
}
switch (position) {
case 0:
View frontPage = mLayoutInflater.inflate(R.layout.profile_cover, container, false);
ImageView avatar = (ImageView) frontPage.findViewById(R.id.avatar);
Picasso.with(getActivity())
.load(mAvatarUrl)
.placeholder(R.drawable.error)
.error(R.drawable.error)
.into(avatar);
TextView screenName = (TextView) frontPage.findViewById(R.id.screen_name);
screenName.setText("#" + mScreenName);
TextView remark = (TextView) frontPage.findViewById(R.id.remark);
remark.setText(TextUtils.isEmpty(mRemark) ? mScreenName : mRemark);
if (mVerified) {
frontPage.findViewById(R.id.verified).setVisibility(View.VISIBLE);
}
container.addView(frontPage);
return frontPage;
case 1:
View introPage = mLayoutInflater.inflate(R.layout.profile_intro, container, false);
CatnutUtils.setText(introPage, R.id.description, TextUtils.isEmpty(mDescription)
? getString(R.string.no_description) : mDescription);
CatnutUtils.setText(introPage, R.id.location, mLocation);
CatnutUtils.setText(introPage, R.id.profile_url, Constants.WEIBO_DOMAIN + mProfileUrl);
container.addView(introPage);
return introPage;
default:
return null;
}
}
};
and then in your onViewCreate or OnCreateView, put mViewPager.setAdapter(coverPager);
the instantiateItem is the place to put the page' s view, similar like the FragmentPagerAdapter!
It' s just like put the FragmentAdapter' s view int the ViewPager' s host Fragment, not the other fragment!
Hope it' s helpful!