I have this Layout:
1) An AppCompatActivity with a ToolBar and a fragment container
2) Some simple Fragments
3) A Fragment that coantains TabLayout with ViewPager
When i use the fragments that are supposed to be used on the tabs the fragments are showing nicely.
One of the fragments that are supposed to be on the tabs has inside a ViewPager.
This is how i try to implement the TabLayout inside the Fragment
The Fragment with the TabLayout and the ViewPager:
public class StaffProfileFragment : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
var view = inflater.Inflate(Resource.Layout.StaffProfilesWithTabs, container, false);
var fragments = new Fragment[]
{
new StaffList(),
new MessagesFragment(),
new SuggestionsMessagesFragment()
};
var titles = CharSequence.ArrayFromStringArray(new[]
{
"Profile",
"Thanks",
"Suggestions"
});
var viewPager = view.FindViewById<ViewPager>(Resource.Id.ProfileViewpager);
viewPager.Adapter = new TabsFragmentPagerAdapter(ChildFragmentManager, fragments, titles);
//var myActivity = (MainActivity)this.Activity;
//myActivity.SetActionBarTitle("Select Your Location");
// Give the TabLayout the ViewPager
var tabLayout = view.FindViewById<TabLayout>(Resource.Id.SlidingTabsTabLayout);
tabLayout.SetupWithViewPager(viewPager);
return view;
}
}
}
And the Adapter i use:
public class TabsFragmentPagerAdapter : FragmentPagerAdapter
{
private readonly Fragment[] fragments;
private readonly ICharSequence[] titles;
public TabsFragmentPagerAdapter(FragmentManager fm, Fragment[] fragments, ICharSequence[] titles) : base(fm)
{
this.fragments = fragments;
this.titles = titles;
}
public override int Count
{
get
{
return fragments.Length;
}
}
public override Fragment GetItem(int position)
{
return fragments[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return titles[position];
}
}
(Im putting only this code for now if it is enough for the problem to be solved, else i will put the xmls and the Fragments code.)
The problem is that the fragment appears but it doesnt load the data correctly, tha tabs cannot be swyped and if i click on the fragment somewhere the app crash with crushing in Debugger.
What am i doing wrong? is it bad idea to implement tablayout inside a fragment? maybe i should implement it on the AppCompatActivity and show/hide the elements i want to use on every occasion?
Two solutions that work as i have test them are:
Create a seperate Activity with toolbar and tabhost.
In the activity with the toolbar to add tabhost and when i want to show only one fragment, i would use tabhost and set visibility to gone.
But im trying to understand which is the best way to do this,
Thanks
Related
so i'm using ViewPager to make my app much faster , instead of using an activity for each layout.
the problem is , in activity you can write your code inside onCreate
and it will only starts when you start the activity , right?
but when you go with fragments and fragmentPagerAdapter and use ViewPager for them.
all your fragments going to start their (onCreateView) together even if your ViewPager only showing the First Fragment!
if you are you playing a sound or animation on the start of a fragment , it will starts in the background !
here's my fragmentPagerAdapter class:
public class PagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitle = new ArrayList<>();
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitle.add(title);
}
}
and i have this (second Fragment) that i don't want it to starts without being seleceted!
public class GameFragment extends Fragment{
private View gameLayout
private Animation showBtn;
private Button button;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
gameLayout = inflater.inflate(R.layout.game_layout, container, false);
button = gameLayout .findViewById(R.id.button);
// loading my anim xml file
showBtn = AnimationUtils.loadAnimation(getContext(),R.anim.btn_show);
button.startAnimation(showBtn);
loadLevel();
return gameLayout
}
finally this is my Activity that have ViewPager :
public class ControllerActivity extends AppCompatActivity {
public CustomPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controller);
viewPager = findViewById(R.id.viewPager);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
adapter.addFragment(new HomeFragment(),"Home");
adapter.addFragment(new GameFragment(),"Game");
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);
}
i need your help to show me my mistakes here and what is the correct way
Using an OnPageChangeListener in your View Pager, you can detect which fragment is currently shown in the View Pager. You'll then need to check which fragment is shown and then call a method on that fragment's class to start any sounds for example that you don't want to start until the fragment is the fragment in view.
You should use an interface for this.
Here you can find an example of using an OnPageChangeListener.
Here you can find an example of Activity to Fragment communication using interfaces. This example has a lot of code that won't be relevant to your use case, but demonstrates the use of interfaces.
One way to do it "Might" be to just initialize the view of the fragment in onCreateView :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_media_viewer, container, false);
return view;
}
Then in the override the setUserVisibleHint() function and do the rest of the initialization there. This function is called when ever the fragment is in view.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// do the rest of the code here.
}
I haven't used setuserVisibleHint for this purpose yet. But you can try it. Let me know too.
I set up a tab slider in my activity calling 3 separate fragments depending on the fragment selected. The problem is that when the first tab is selected, "Science" is shown - which was set on the onCreateView() method of the first fragment. However, WITHOUT clicking on the other tabs, "Technology" and "Sports" are then shown.
To my knowledge, the way I implemented it the last two should only show once the corresponding tab is selected.
This is my Page adapter class
public class PagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3; //Represents the number of tabs
private String tabTitles[] = new String[]{"Technology", "Science", "Sports"}; //Tab values defined in an array
private Context context;
public PagerAdapter(FragmentManager fm, Context context)
{
super(fm);
this.context = context;
}
#Override
public int getCount()
{
return PAGE_COUNT;
}
//determines the fragment depending on the selected tab
#Override
public Fragment getItem(int position)
{
if(position==0)
return TechnologyFragment.newInstance(position+1);
else if(position==1)
return ScienceFragment.newInstance(position+1);
else if(position==2)
return SportsFragment.newInstance(position+1);
else
return null;
}
//displays the title for each tab
#Override
public CharSequence getPageTitle(int position)
{
// Generate title based on item position
return tabTitles[position];
}
}
This is the news class - where the view pager and tab layout are set up
public class News extends AppCompatActivity {
//Here the viewPager is connected to the PagerAdapter class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
//This component will be used to page between the various fragments created.
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), News.this));
//the tabLayout is given the viewPager to connect to the pager with the tabs
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
//the following was used so the tabs fill up the width of the screen being responsive for all devices and orientations
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
}
}
And these are the fragment classes - (the other two are identical but have a different message represented using the Toast)
public class ScienceFragment extends Fragment {
//other methods
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_science_fragment, container, false);
toShow=getAll();
GridView gridView = (GridView)view.findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getContext(),toShow));
Toast.makeText(getContext(), "Science", Toast.LENGTH_SHORT).show();
return view;
}
}
Any help is appreciated
I've been trying something..unique..recently. Due to hierarchical levels of the app requiring multiple nav drawers, which isn't very good UX, I've been trying to add a ViewPager in the nav drawer which pages to show the list for the lower level in the hierarchy
Unfortunately, this doesn't seem to be working. Neither does it page, nor does it show an overscroll, indicating more fragments. Its not an Adapter problem, since all ViewPagers in the app are using the same adapter
Adapter
class AdapterClass extends FragmentStatePagerAdapter
{
List<Fragment> mFragments;
public AdapterClass(FragmentManager mFragManager, List<Fragment> mFragment)
{
super(mFragManager);
this.mFragments = mFragment;
}
#Override
public int getCount()
{
return mFragments.size();
}
#Override
public Fragment getItem(int position)
{
return mFragments.get(position);
}
}
Fragments (both fragments are same, save for the list text)
public class NavPagerFragMain extends SherlockFragment
{
View view;
ListView mList;
private ArrayList<String> mItems = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.nav_pager_frag_main, container, false);
mList = (ListView)view.findViewById(R.id.nav_pager_frag_main);
mItems.add("Read");
mItems.add("Implement");
mItems.add("Design");
mItems.add("Download");
mItems.add("Connect");
mItems.add("Watch");
UserAdapter mAdapter = new UserAdapter(getActivity().getApplicationContext(), mItems);
mList.setAdapter(mAdapter);
return view;
}
Activity where I initialize and setup the ViewPager
mDrawer = (DrawerLayout)findViewById(R.id.app_drawer);
mDrawerItem = (ViewPager)findViewById(R.id.app_drawerpager);
List<Fragment> mInitFrags = new ArrayList<Fragment>();
mInitFrags.add(Fragment.instantiate(getApplicationContext(), NavPagerFragMain.class.getName()));
mInitFrags.add(Fragment.instantiate(getApplicationContext( ), NavPagerFragSub.class.getName()));
mAdapter = new AdapterClass(getSupportFragmentManager(), mInitFrags);
mDrawerItem.setAdapter(mAdapter);
mDrawerItem.setPageTransformer(true, new DepthPageTransformer());
Is your main Activity where you setup the ViewPager also a Fragment? If so, rather than pass the result of getSupportFragmentManager() to your adapter's constructor, pass it the result of the base fragment's getChildFragmentManager() call. In this scenario, you are effectively embedding Fragments within other fragments and the pager needs the child fragment manager to transition and deal with the children. Good luck!
I have an Activity with a ViewPager containing multiple fragments. how can i now access a TextView in one of that fragments to change its text from the main activity? I tried multiple ways and they all ended in a NullPointerException.
Activity:
public class SummonerOverview extends SherlockFragmentActivity implements TabListener, OnPageChangeListener {
private ViewPager mPager;
private PagerAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.summoner_overview);
initialize();
}
private void initialize() {
// initialize Pager
mPager = (ViewPager) findViewById(R.id.viewpager);
mAdapter = new PagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(1);
mPager.setOnPageChangeListener(this);
}
}
PagerAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
frags = new Fragment[3];
frags[0] = new StatisticsFragment(0);
frags[1] = new RatingsFragment(1);
frags[2] = new HistoryFragment(2);
}
private final int NUM_PAGES = 3;
Fragment[] frags;
#Override
public Fragment getItem(int arg0) {
if (arg0 == 0)
return frags[0];
else if (arg0 == 1)
return frags[1];
else
return frags[2];
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
The Fragment:
public class StatisticsFragment extends SherlockFragment {
public StatisticsFragment(int fragNr) {
this.fragNr = fragNr;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_overview_statistics, container, false);
return v;
}
}
The Textview in the StatisticsFragment is labeled with an id in the fragment_overview_statistics.xml, but when i try
TextView tv = (TextView) findViewById(R.id.id_of_the_textview)
tv.setText("text");
from within the onCreate() Method of the Activity after the initialize() Method, i get an Exception.
Okay, after another hour of googling, i think i solves my own question (although i'm sure there is a better method to solve that)
I now hold all the Data that is displayed by the fragments in the MainActivity and make the Fragmets consume this Data in their onCreateView() Method by using public Methods of the Activity.
In the PagerAdapter, i overwrote getItemPosition() to:
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Now, everytime i update some of the Data, i also call mAdapter.notifyDataSetChanged() which leads into a recreation of all fragments.
It works, although it looks like a poor hack for me. Im sure there must be a better solution because now i have to recreate all fragments for changing one TextView of one Fragment, what is surely not the way it is intended to be done.
I'm new to Android developing and I would really appreciate some help here.
I'm using a fragment that contains a TextView and I'm using 5 instances of the same MyFragment class.
In the activity, i got a button and a ViewPager, and I need the button to update all the fragment instances content, whenever its clicked.
Here's the Activity
public class MainActivity extends FragmentActivity {
final static String[] CONTENT = {"a", "b"};
ViewPager pager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<MyFragment> fragments = new Vector<MyFragment>();
for(int i = 0; i < 5; i++){
MyFragment fragment = new MyFragment(CONTENT);
fragments.add(fragment);
}
PagerAdapter adapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//method that isn't working
PagerAdapter adapter = (PagerAdapter)pager.getAdapter();
for(int i = 0; i < 5; i++){
MyFragment fragment = (MyFragment) adapter.getItem(i);
fragment.textView.setText(fragment.content[1]);
}
}
});
}
}
The Fragment
public class MyFragment extends Fragment{
String[] content;
TextView textView;
public MyFragment(String[] content) {
this.content = content;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(content[0]);
return view;
}
}
And the FragmentPagerAdapter
public class PagerAdapter extends FragmentPagerAdapter{
List<MyFragment> fragments;
public PagerAdapter(FragmentManager fm, List<MyFragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int arg0) {
return fragments.get(arg0);
}
#Override
public int getCount() {
return fragments.size();
}
}
The OnClick method gives me a NullPointerException whenever i try to access a fragment from the adapter which is less than adapter.getCurrentItem() - 1, or more than adapter.getCurrentItem() + 1.
Any idea on how to update all the fragments at the same time?
Thanks in advance.
The easiest way to update those fragments is to use your code and set the number of fragments that the ViewPager holds in memory to the number of total fragments - 1(so all fragments are valid no matter at what page you are). In your case:
pager.setOffscreenPageLimit(4); // you have 5 elements
You can still use the method from my comment with the method onPageScrollStateChanged(so the update will start the moment the user starts swiping) to see when the user is starting to swipe the pager and update the fragments to the left and right of the currently visible fragment, but this will be a bit difficult to get right so I recommend to go with the first option.
Some points regarding your code containing fragments:
If you nest the fragment class make it static so you don't tie it to the activity object.
Don't create a constructor for a Fragment besides the default one. If the framework needs to recreate the fragment it will call the default constructor and if it is not available it will throw an exception. For example, try to change the orientation of the phone/emulator and see what happens(this is one of the cases when Android will recreate the fragments). Last, use a custom name for the ViewPager's adapter, you use PagerAdapter which is the name of the super class of FragmentViewPager and it's very confusing for someone reading your code.
If you need to pass data to the Fragment you could use a creation method like the one below:
public static MyFragment newInstance(String text) {
MyFragment f = new MyFragment();
Bundle b = new Bundle();
b.putString("content", text);
f.setArguments(b);
return f;
}
The text will be available in MyFragment by using getArguments().getString("content");