I have ViewPager and when I open fragment I update my bd. But the previous and next Fragment update too.
I haven't more code for this just :
PagerAdapter pagerNewsAdapter = new PagerNewsAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(pagerNewsAdapter);
pager.setCurrentItem(getIntent().getExtras().getInt(ITEM_POSITION));
How to prevent the update Fragment n-1 and n + 1 ?
Fragment code :
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_new, container,
false);
TextView txtTitle = (TextView) view.findViewById(R.id.titleNew);
txtTitle.setTypeface(custom_font_bold);
txtTitle.setText(ac.getTitre());
TextView resume = (TextView) view.findViewById(R.id.res);
resume.setTypeface(custom_font_bold);
resume.setText(Html.fromHtml(ac.getResume()));
resume.setTextColor(getResources().getColor(R.color.grey_500_transparent));
BD_ACTUALITE.updateViewAct(ac);
return view;
}
My ViewPager :
ArrayList<Fragment> fragments = new ArrayList<>();
for(Act act : arListNews) {
FragmentNew frg = new FragmentNew(act);
fragments.add(frg);
}
PagerAdapter pagerNewsAdapter = new PagerNewsAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(pagerNewsAdapter);
pager.setCurrentItem(getIntent().getExtras().getInt(ITEM_POSITION));
Thx all for answers.
It's ok,
I implement the ViewPager.OnPageChangeListener and Override onPageSelected()
Related
Before i begin,let me say that i've already checked all the links regarding this and the inflater init method doesn't work for me unfortunately
Fragment.java
public View onCreateView(LayoutInflater inflater1, ViewGroup container, Bundle savedInstanceState) {
View view = inflater1.inflate(R.layout.tab, container, false);
context = getActivity();
findViews(view);
setBar();
setPie();
return view;//inflater1.inflate(R.layout.tab, container, false);
}
private void findViews(View view) {
inflater=LayoutInflater.from(context);
root=(ViewGroup)view.findViewById(R.id.root);
piechart= (PieChart)view.findViewById(R.id.pie_chart);
barChart = (BarChart)view.findViewById(R.id.barchart);
popupContentView=inflater.inflate(popupdashboard,root,false);
Activity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard2);
//Initializing viewPager
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setSelectedTabIndicatorColor(Color.MAGENTA);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setText("Dashboard"));
tabLayout.addTab(tabLayout.newTab().setText("Transaction Details"));
tabLayout.addTab(tabLayout.newTab().setText("Your Tab Title2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.simpleViewPager);
//Creating our pager adapter
pager adapter = new pager(getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
/// if (tabLayout.getSelectedTabPosition()==0){
init = new Tab1();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.linearLayout, init);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
Any help would be deeply appreciated
Please Try this....
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.nameoflayout, container, false);
piechart= (PieChart)view.findViewById(R.id.pie_chart);
barChart = (BarChart)view.findViewById(R.id.barchart);
return view;
refrence link..refer
You could try this one:
Create a variable of the type ViewGroup locally to the function onCreate, then initialize it.
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.nameoflayout, container, false);
Then, you could set you TextView using this:
TextView txtView = (TextView)root.findViewByID(R.id.nameoftextviewid);
Return variable root.
This is exactly what I'm doing in my projects.
I have this navigation drawer with different fragments, The image below gives a visual description. The issue i am having is when i visit another view like the profile view and return to the dashboard view, the dashboards' home tab shows up empty till i visit one of the other tabs and return to it, then it displays the items in it. Please see the second image. All i get in my monitor is a "E/RecyclerView: No adapter attached; skipping layout" error.
Below is my code for the dashboard home tab
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View x = inflater.inflate(R.layout.tablayout, null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
fragmentList = new ArrayList<>();
prepareDataResource();
titleList();
topLevel rta = new topLevel(getFragmentManager(),fragmentList,titleList);
viewPager.setAdapter(rta);
tabLayout.setupWithViewPager(viewPager);
setTabIcons();
return x;
}
*Code for the Dashboard Fragment that houses home,PTA, incidents tabs
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View x = inflater.inflate(R.layout.tablayout, null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
fragmentList = new ArrayList<>();
prepareDataResource();
titleList();
topLevel rta = new topLevel(getFragmentManager(),fragmentList,titleList);
viewPager.setAdapter(rta);
tabLayout.setupWithViewPager(viewPager);
setTabIcons();
return x;
}
I have a TabsFragment, that is used to display FragmentStatePagerAdapter. To display tabs, I firstly create a TabsFragment, which creates multiple fragments, stored in tabs. The problem is, when I destroy a TabsFragment and create a new one, tab fragments created by previous TabsFragment are kept in memory for some reason. So does look like my TabsFragment class:
public class TabsFragment extends AbstractFragment {
public static final String TAG = "Tabs_Fragment";
private ViewPagerParallax viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tabs, container, false);
viewPager = (ViewPagerParallax)view.findViewById(R.id.viewpager);
viewPager.set_max_pages(CustomFragmentPagerAdapter.PAGE_COUNT);
viewPager.setBackgroundAsset(R.drawable.background);
viewPager.setAdapter(new CustomFragmentPagerAdapter(getFragmentManager()));
PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
tabsStrip.setViewPager(viewPager);
return view;
}
...
}
Like this I replace my TabsFragment with some other fragment:
public void transactOver(AbstractFragment what, int direction) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_content, what, what.getClassTag());
ft.commit();
}
Where R.id.fragment_content is a framelayout, that stores my fragmens. CustomFragmentPagerAdapter is coded as usual.
Finished up changing
viewPager.setAdapter(new CustomFragmentPagerAdapter(getFragmentManager()));
with
viewPager.setAdapter(new CustomFragmentPagerAdapter(getChildFragmentManager()));
I have an android DrawerLayout and it has 2 items, one of them is a fragment with a PagerTabStrip and it load their fragments but when I change the item of the DrawerLayout and I come back to the PagerTabStrip it loads the tabs, but doesn't their fragments.
This is my PagerTabStrip class
public class maintabs extends Fragment {
FragmentPagerAdapter adapterViewPager;
PagerTabStrip pagerTabStrip;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tabs_main, container, false);
ViewPager vpPager = (ViewPager) rootView.findViewById(R.id.vpPager);
pagerTabStrip = (PagerTabStrip) rootView.findViewById(R.id.pager_header);
pagerTabStrip.setDrawFullUnderline(true);
adapterViewPager = new SampleFragmentPagerAdapter(getFragmentManager());
vpPager.setAdapter(adapterViewPager);
return rootView;
}
}
I've found a solution.
Just use a FragmentStatePagerAdapter instead FragmentPagerAdapter
Having problem showing Fragment inside a ViewPager.
I have a ViewPager which was hosted inside Fragment where this Fragment was inside the Activity.
This ViewPager has a pagerTabStrip. Each page in ViewPager are Fragment of special usage.
This is how I setup the viewpager with PagerAdapter
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pager_screen,
container, false);
ViewPager pager = (ViewPager) rootView.findViewById(R.id.vp_main);
pager.setAdapter(new MainTabAdapter(rootView.getContext(),getFragmentManager()));
return rootView;
}
This is fine, I can see the pager. Within the MainTabAdapter I have a getItem which I select differnt fragments.
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Log.w("ALBUM", types[arg0].toString());
Fragment fragment = FragmentType.getFragment(types[arg0]);
return fragment;
}
This is fine too. I can get the right fragment. When showing the Fragment of different page, none of them showing. Below is my code on one of the Fragment to inflating an xml
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_local_screen,
container, false);
Bundle data = new Bundle();
data.putStringArray("PROJECTION", new String [] {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME,
MediaStore.Video.VideoColumns.BUCKET_ID,
MediaStore.Video.VideoColumns.DATE_TAKEN,
MediaStore.Video.VideoColumns.DATE_ADDED,
MediaStore.Video.VideoColumns.DESCRIPTION,
MediaStore.Video.VideoColumns.DURATION});
data.putString("SELECTION", null);
data.putStringArray("SELECTIONARRAY", null);
data.putString("SORTORDER", MediaStore.Video.VideoColumns.BUCKET_DISPLAY_NAME + " LIMIT " + mLimit);
VideoCursorAdapter adapter = new VideoCursorAdapter(getActivity(),null,true);
LocalVideos videos = new LocalVideos(getActivity(), adapter);
getLoaderManager().initLoader(LocalVideos.LoaderID.THUMBNAIL.getID(), data, videos);
ListView listView = (ListView)rootView.findViewById(R.id.listVideos);
//listView.setAdapter(adapter);
videos.setView(listView);
return rootView;
This works, if I remove the viewpager. When I put the ViewPager code, this fragment doesn't show up.