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;
}
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 implemented Pager Sliding Strip and it is working totally fine.
I have 5 tabs each containing different fragment. When I click on 2nd tab to load respective fragment, it also load details of 3rd fragment, it showing the toast I coded in 3rd tab's fragment.
this is totally ruined the apps performance, How come I solve this problem I don't have any idea whats actually going on.
I am working on a really big project I cannot post the whole code.
Please Help me with this, I will post the respective code on demand.
Regards.
EDIT
Notification Fragment which is loading another fragment
ListView notification_listview;
ArrayList<FragmentNotificationDTO> arraylist;
NotificationAdapter adapter;
private static View notificationView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (notificationView != null) {
ViewGroup parent = (ViewGroup) notificationView.getParent();
if (parent != null)
parent.removeView(notificationView);
}
notificationView = inflater.inflate(R.layout.fragment_notification,
container, false);
initLayout(notificationView);
loadData();
return notificationView;
}
private void initLayout(View view) {
notification_listview = (ListView) view.findViewById(R.id.notification_listview);
arraylist = new ArrayList<FragmentNotificationDTO>();
adapter = new NotificationAdapter(mContext, arraylist);
notification_listview.setAdapter(adapter);
}
This is Food Fragment which is loaded also when we click on Notification Tab
private static View foodlistView;
RelativeLayout rl_foodshare, rl_foodrequested, rl_foodkitchen;
TextView tv_foodshare, tv_foodrequested, tv_foodkitchen;
ImageView img_foodshare, img_foodrequested, img_foodkitchen;
Fragment fr;
FragmentManager fm;
FragmentTransaction fragmentTransaction;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (foodlistView != null) {
ViewGroup parent = (ViewGroup) foodlistView.getParent();
if (parent != null)
parent.removeView(foodlistView);
}
foodlistView = inflater.inflate(R.layout.fragment_location_list,
container, false);
initLayout(foodlistView);
ChangeFragment(1);
return foodlistView;
}
MainActivity.java which contains the PagerSlidingStrip & View Pager
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()));
PagerSlidingTabStrip PGSTRIP = (PagerSlidingTabStrip) findViewById(R.id.tabs);
PGSTRIP.setViewPager(viewPager);
viewPager.setOffscreenPageLimit(0);
}
I solved my problem by overriding this method in each of my fragment and then calling the loading method when fragment's visibility is true to user.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// load data here
}else{
// fragment is no longer visible
}
}
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
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()
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.